-
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.
-
I think you want to reset the frequency array after each survey result. Otherwise the results will accumulate with the previous survey