# Thread: trouble with assignment, vectors..

1. ## trouble with assignment, vectors..

Im trying to do a population vector assignment. I tried to comment my word so you know waht is going on .
I am having trouble putting coefficents into a two dimensional vector array. So if you could give me hints on that it would be great. The code only compiles until that two demesional array which is where it asks for the coeffiecents.
Code:
```#include <iostream>
#include <vector>
using namespace std;

int main()
{
int numN = 0, years, print;
double population;
vector <double> nzones(numN);
vector <vector <double> > coeff (numN, vector<double>(numN));//2d array
vector<double>zones(numN);
cout << "Enter the number of N areas in the area." << endl;//number or areas
cin >> numN;

if (numN <= 0 )//test statement for areas greater than 0.
{
cout << "That is not a valid number." << endl;
return(-1);
}
for ( int i = 0; i < zones.size(); i++ )//enter populations into a 1-d array.
{
cout << "Enter the populations for the given number of areas." << endl;
cin >>  zones[i];
}

cout << "How many years do you want to calculate?" << endl;//number of years to calculate
cin >> years;

cout << "How often do you want to print the calculated population data?" << endl;//how often to print every so many years
cin >> print;

for ( int j = 0; coeff.size(); j++ )// puts coeffiecients into a 2d array.
{
for ( int f = 0; coeff.size(); f++ )

{
cout << "Enter the population coefficients for the given number of areas." << endl;
cin >>  coeff[j][f];
}
}
for ( int z = 0; z <= years; z++ )//loop to calculate area populations for so many years.
{
for ( int  p = 0;nzones.size(); p++ )//places populations into nzones.
{
nzones[p] = zones[p];

for( int l = 0; zones.size(); l++ )//actual calculation of populations times coefficents
{

nzones[p] = zones[p] + coeff[p][l] * zones[l];

}
}
for ( p = 0;  p < numN; p++ )//places new zones populations into zones.
{
zones[p] = nzones[p];
}
if ( z % print == 0)// prints every so many times.
{
for (int k = 0; k < numN; k++)

cout << nzones[k] << endl;

}
}
return 0;
}```
thanks

2. What was the compiler error? The code compiles fine for me.

Your problem appears to be that it isn't running properly, in which case you should tell us what input you are giving it.

In this case, you are using numN to provide the size of the vectors, but at that point in the program it is 0. You should wait until after you get a valid value for numN and then declare your vectors.

Also, when looping through the two dimensional vector, you should use the size of the inner vector for your inner loop. The inner vector is just coeff[j].

3. the problem was that i had initilzed numN to zero and tried to initialze the vectors as well. so i moved the vectors down under the user input for numN.
now i have a forever loop for the coeffiecent input part. I believe it has something to do with the inner loop, but i do not know why it would continue on forever if the size is i.e 2.
Code:
```#include <iostream>
#include <vector>
using namespace std;

int main()
{
int numN = 0 , years, print;
double population;
cout << "Enter the number of N areas in the area." << endl;//number or areas
cin >> numN;

if (numN <= 0 )//test statement for areas greater than 0.
{
cout << "That is not a valid number." << endl;
return(-1);
}
vector <double> nzones(numN);
vector <vector <double> > coeff (numN, vector<double>(numN));//2d array
vector<double>zones(numN);
for ( int i = 0; i < zones.size(); i++ )//enter populations into a 1-d array.
{
cout << "Enter the populations for the given number of areas." << endl;
cin >>  zones[i];
}

cout << "How many years do you want to calculate?" << endl;//number of years to calculate
cin >> years;

cout << "How often do you want to print the calculated population data?" << endl;//how often to print every so many years
cin >> print;

for ( int j = 0; coeff.size(); j++ )// puts coeffiecients into a 2d array.
{
cout << "dafadf" << coeff.size();
for ( int f = 0; coeff[j].size(); f++ )
cout << "dafadf" << coeff[j].size();
{
cout << "Enter the population coefficients for the given number of areas." << endl;
cin >>  coeff[j][f];
}
}
for ( int z = 0; z <= years; z++ )//loop to calculate area populations for so many years.
{
for ( int  p = 0;nzones.size(); p++ )//places populations into nzones.
{
nzones[p] = zones[p];

for( int l = 0; zones.size(); l++ )//actual calculation of populations times coefficents
{

nzones[p] = zones[p] + coeff[p][l] * zones[l];

}
}
for ( p = 0;  p < numN; p++ )//places new zones populations into zones.
{
zones[p] = nzones[p];
}
if ( z % print == 0)// prints every so many times.
{
for (int k = 0; k < numN; k++)

cout << nzones[k] << endl;

}
}
return 0;
}```

4. Both your for loop controls aren't quite right. You are missing something important.

Also, the second cout line you put in there to test is in the wrong place and will cause the inner for loop to only run that line.

5. What was the compiler error?
I'd settle for some spell checking.

6. Code:
`vector <double> nzones(numN);`
You don't actually need a size at all:
Code:
`vector<double> nzones;`
There is not maximum size with vectors. The size just specifies how big (ie, how much memory should be allocated) initially.

7. >> You don't actually need a size at all

The method that was used is probably the best. Initializing the vector with the appropriate size from the beginning avoids all re-allocations. Moving the declaration to after the correct size is available is preferred over not setting a size initially and resizing later.

Popular pages Recent additions