Thread: trouble with assignment, vectors..

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    10

    Unhappy 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. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    10
    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. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    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. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    What was the compiler error?
    I'd settle for some spell checking.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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 subscribe to a feed

Similar Threads

  1. Trouble with assignment in C
    By mohanlon in forum C Programming
    Replies: 17
    Last Post: 06-23-2009, 10:44 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Trouble with vectors
    By jaw24 in forum C++ Programming
    Replies: 11
    Last Post: 04-07-2007, 06:05 PM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. How does this work?...assignment using vectors...
    By Gamma in forum C++ Programming
    Replies: 1
    Last Post: 04-22-2002, 03:58 PM