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.

#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

Recommended Answers

All 2 Replies

You should be using push_back or push_front when inserting into a vector.

If I remember right, the vector class is very slow when inserting elements to the front, or any place except the back. So, if there is a method push_front() then you probably shouldn't use it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.