Thread: Array output strange

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Array output strange

    Hi, I have written a program to display the output of three student suiveys using
    one dimensional arrays. The first array output is correct, but the second and third keep giving me random numbers that do not match the responseSize's for B and C. Here is the code:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    using std::cin;
    
    #include <iomanip>
    
    using std::setw;
    
    // main function - driver
    int main ( void )
    {
     	// declare reponse sizes for each list 
       const int responseSizeA = 20;
       const int responseSizeB = 20;
       const int responseSizeC = 25;
       
       // no more than a 10 list answer
       const int frequencySize = 11;
       
       // results of cantine survey
       int cantine[responseSizeA ] = { 2, 4, 6, 1, 5, 3, 9, 10, 8, 7, 10, 1, 1, 2, 3,
       1, 2, 3, 1, 9 };
       
       // results of liking college survey
       int likecollege[responseSizeB ] = { 8, 2, 1, 3, 5, 1, 10, 7, 9, 8, 4, 6, 7, 7, 10, 2,
       5, 2, 3, 1 };
       
       // resulrs of liking college money amount survey
       int funding[responseSizeC ] = { 10, 10, 10, 1, 3, 2, 5, 7, 8, 9, 7, 6, 3, 4, 1,7,
       5, 1, 2, 9, 8, 7, 1, 6, 5 };
       
       // ignoring element zero
       int frequency[frequencySize] = { 0 };
       
       /*************************************************************************
                    SURVEY 1 - CANTINE FOOD SURVEY                       
       **************************************************************************/
       
       cout << "SURVEY 1: 'LIKING FOOD IN COLLEGE CANTINE'\n\n";
       
       for ( int answer = 0; answer < responseSizeA; answer++ )
          frequency[cantine[answer]]++;
       
    	// column headings   
       cout << "RATING" << setw( 17 ) << "FREQUENCY" << endl;
       
       // output results of survey 1
       for ( int rating = 1; rating < frequencySize; rating++ )
          cout << setw( 6 ) << rating << setw( 11 ) << frequency[rating] << endl;
          
       /*************************************************************************
                    SURVEY 2 - LIKING COLLEGE SURVEY                       
       **************************************************************************/
          
       cout << "\nSURVEY 2: 'FINDING COLLEGE INTERESTING'\n\n";
       
       for ( int answer = 0; answer < responseSizeB; answer++ )
          frequency[likecollege[answer]]++;
          
       // column headings
       cout << "RATING" << setw( 17 ) << "FREQUENCY" << endl;
       
       // output results of survey 2
       for ( int rating = 1; rating < frequencySize; rating++ )
          cout << setw( 6 ) << rating << setw( 11 ) << frequency[rating] << endl;
          
       /*************************************************************************
                    SURVEY 3 - COLLEGE FUND SURVEY                       
       **************************************************************************/
          
       cout << "\nSURVEY 3: 'COLLEGE STUDENT FUNDS IS RIGHT AMOUNT'\n\n";
       
       for ( int answer = 0; answer < responseSizeC; answer++ )
          frequency[funding[answer]]++;
          
       // column headings
       cout << "RATING" << setw( 17 ) << "FREQUENCY" << endl;
       
       // output results of survey 3
       for ( int rating = 1; rating < frequencySize; rating++ )
          cout << setw( 6 ) << rating << setw( 11 ) << frequency[rating] << endl;
       
       cin.get(); // freeze console output window
       
       return 0; // indicate program ended sucsesfuly
    }
    I have double checked I have not overstepped the array bounds, so that is not the
    problem. I think I am mis-calculating my for loops in survey two and three. It is bizzare as
    the output for the first survey is exactly right.

    Any help greatly appreciated.
    Double Helix STL

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I think you want to reset the frequency array after each survey result. Otherwise the results will accumulate with the previous survey
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. displaying a array to output
    By gkoenig in forum C Programming
    Replies: 19
    Last Post: 02-11-2008, 04:18 AM
  2. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  3. problems reading data into an array and printing output
    By serino78 in forum C Programming
    Replies: 4
    Last Post: 04-28-2003, 08:39 AM
  4. Creating 2D arrays on heap
    By sundeeptuteja in forum C++ Programming
    Replies: 6
    Last Post: 08-16-2002, 11:44 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM