### Getting large numbers as output

I'm trying to finish up my program, but I'm getting very large numbers as the output for the averages after running the program. This program takes data from the user and stores it in a 3x3 array and then does an average on the rows, columns and entire array. The program is big and ugly and probably could be wrote better but its the best I could do:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104`` ``````#include #include using namespace std; const int ROWS = 3, COLUMNS = 3; // Function: dataManipulation // Purpose: To get data from user and // get the average and maximum // Variables: A lot int dataManipulation() { int sum = 0, row_sum = 0, column_sum = 0, row_average, column_average, average, average_firstrow, average_secondrow, average_thirdrow, average_firstcolumn, average_secondcolumn, average_thirdcolumn, highest_row = 0, highest_column = 0, highest_total = 0, max_firstrow, max_secondrow, max_thirdrow, data[ROWS][COLUMNS]; for (int x = 0; x < ROWS; x++) { for (int y = 0; y < COLUMNS; y++) { if (y == 0) // Reset the variables if we are at the start of the loop and display set { row_sum = 0; column_sum = 0; row_average = 0; column_average = 0; cout << "SENSOR SET " << (x + 1) << endl; cout << "------------\n"; } cout << "Sensor " << (y + 1) << ": "; cin >> data[x][y]; cin.ignore(); sum += data[x][y]; // Running total that does not reset row_sum += data[x][y]; // Running total of the rows column_sum += data[y][x]; // Running total of the columns if (y == 2) // Only do this the last time the loop goes through { row_average = row_sum / COLUMNS; column_average = column_sum / COLUMNS; } if (data[x][y] > highest_row) // Find the highest number in the row { highest_row = data[x][y]; } } if (x == 0) { average_firstrow = row_average; average_firstcolumn = column_average; max_firstrow = highest_row; } else if (x == 1) { average_secondrow = row_average; average_secondcolumn = column_average; max_secondrow = highest_row; } else if (x == 2) { average = sum / pow(COLUMNS, 2); average_thirdrow = row_average; average_thirdcolumn = column_average; max_thirdrow = highest_row; } cout << endl; } cout << "----------------------------------------------\n\n"; cout << "Average for sensor set 1: " << average_firstrow << endl; cout << "Average for sensor set 2: " << average_secondrow << endl; cout << "Average for sensor set 3: " << average_thirdrow << endl << endl; cout << "Average for first sensor in every set: " << average_firstcolumn << endl; cout << "Average for second sensor in every set: " << average_secondcolumn << endl; cout << "Average for third sensor in every set: " << average_thirdcolumn << endl << endl; cout << "The maximum number for the first set of sensors is " << max_firstrow << endl; cout << "The maximum number for the second set of sensors is " << max_secondrow << endl; cout << "The maximum number for the third set of sensors is " << max_thirdrow << endl << endl; cout << "The average sensor reading is " << average << endl; cout << "The maximum sensor reading is " << highest_row << endl; } int main() { cout << "Geological sensor data\n"; cout << "----------------------\n\n"; cout << "Enter the following data\n\n"; dataManipulation(); cin.get(); return 0; }``````

If the program is ran, it gives large numbers for the average for the columns 1 and 2 and the correct answer for column 3 for some reason. I think variables are not being initialized somewhere, but I don't know where. Any help would be appreciated.
1. Get and store input.
2. Manipulate data.

Not:
1. Get 1 input.
2. Attempt to manipulate data not yet input.
3. repeat.

 ``123`` `````` sum += data[x][y]; // Running total that does not reset row_sum += data[x][y]; // Running total of the rows column_sum += data[y][x]; // Running total of t ``````

column_sum... when do you think the row index is also a valid column index?
Are you saying that I should move all of the "if" statements out of the loops and it will work?

What I'm thinking is since I need to get the average of the columns, the column_sum would do that because it would look like this:

1st time through: column_sum [0][0]
2nd time through: column_sum [1][0]
3rd time through: column_sum [2][0]

See, that is what I need it to add. After adding all those up I will divide by the total.

I haven't worked with arrays in C++ very much so I apologize if I can't yet understand what you're talking about.

EDIT: I don't see why the average for row_sum is working while the column_sum is not... If none of them worked, it might be a little easier to see.
Last edited on
Column_sum doesn't work, because you're getting input by row. So when you get the sum of the first column, after you've gotten input for the first row, only one of the 3 values is valid.

When you get the sum of the first column after you've gotten input for the 2nd row, only two of those three values are valid.

When you get the sum of the first column after you've gotten input for the 3rd row, all three values are valid.
I tried to put the column_sum outside of the first loop like this:

 ``123456789101112131415161718192021222324`` `````` cout << "Sensor " << (y + 1) << ": "; cin >> data[x][y]; cin.ignore(); sum += data[x][y]; // Running total that does not reset row_sum += data[x][y]; // Running total of the rows if (y == 2) // Only do this the last time the loop goes through { row_average = row_sum / COLUMNS; } if (data[x][y] > highest_row) // Find the highest number in the row { highest_row = data[x][y]; } } column_sum += data[y][x]; // Running total of the columns column_average = column_sum / COLUMNS; if (x == 0) { average_firstrow = row_average;``````

But it won't compile. What does the error "name lookup of 'y' changed for ISO 'for' scoping"?
I ended up adding another loop to the program:

 ``123456789101112131415161718192021222324`` `````` for (y = 0; y < COLUMNS; y++) { column_sum = 0; for (x = 0; x < ROWS; x++) { column_sum += data[x][y]; } if (y == 0) { average_firstcolumn = column_sum / ROWS; } if (y == 1) { average_secondcolumn = column_sum / ROWS; } if (y == 2) { average_thirdcolumn = column_sum / ROWS; } }``````

Now the columns average properly.

But my question is.. is there anyway to do this average in the other loop? I'm trying to do this program with only one loop (nested loops don't count as additional loops).
I re-wrote the program to make it a little bit smaller:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778`` ``````#include #include using namespace std; const int ROWS = 3, COLUMNS = 3; // Function: dataManipulation // Purpose: To get data from user and // get the average and maximum // Inputs: Integers to hold various data int dataManipulation() { int i, x, y, sum = 0, row_sum = 0, column_sum = 0, highest = 0, average, row_average, column_average, data[ROWS][COLUMNS], row_array[ROWS], column_array[ROWS], maximum[ROWS]; for (x = 0; x < ROWS; x++) { row_sum = 0; cout << "SENSOR SET " << (x + 1) << endl; cout << "------------\n"; for (y = 0; y < COLUMNS; y++) { cout << "Sensor " << (y + 1) << ": "; cin >> data[x][y]; cin.ignore(); sum += data[x][y]; // Running total that does not reset row_sum += data[x][y]; // Running total of the rows if (y == 2) // Only do this the last time the loop goes through { row_average = row_sum / COLUMNS; } if (data[x][y] > highest) // Find the highest number in the row { highest = data[x][y]; } } average = sum / pow(ROWS, 2); row_array[x] = row_average; maximum[x] = highest; cout << endl; } for (i = 0; i < ROWS; i++) { cout << "The average for SENSOR SET " << (i + 1) << " is " << row_array[i] << endl; } cout << endl; for (i = 0; i < ROWS; i++) { cout << "The maximum number in SENSOR SET " << (i + 1) << " is " << maximum[i] << endl; } cout << "\nThe overall average is " << average << endl; cout << "The maximum overall number is " << highest << endl; } int main() { cout << "Geological sensor data\n"; cout << "----------------------\n\n"; cout << "Enter the following data\n\n"; dataManipulation(); cin.get(); return 0; }``````

I'm still not sure where to compute the column_sum and column_average values. Is it possible to average the columns in the same loop as the rows or should I just go with a separate loop?
I wouldn't average the rows in the loop you're averaging the rows.

Consider the following:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172`` ``````void getInput( int data[][COLUMNS], unsigned rows ) { for (unsigned row=0; row < rows ; ++row) { std::cout << "Sensor Set " << row+1 << "\n------------\n" ; for ( unsigned col=0; col < COLUMNS; ++col ) { std::cout << "Sensor " << col+1 << ':' ; std::cin >> data[row][col] ; } } } void output( char const* label, int const * data, unsigned size ) { for ( unsigned i=0; i max ) max = reading ; if ( reading > row_max[row] ) row_max[row] = reading ; if ( reading > column_max[col] ) column_max[col] = reading ; column_average[col] += reading ; row_average[row] += reading; } row_average[row] /= COLUMNS ; } for ( unsigned col=0; col < COLUMNS; ++col ) column_average[col] /= ROWS ; std::cout << "Average for\n" ; output( "Sensor Set", row_average, ROWS ) ; output( "Column", column_average, COLUMNS) ; std::cout << "Highest reading for\n" ; output( "Sensor Set", row_max, ROWS) ; output( "Column", column_max, COLUMNS) ; std::cout << "\nAverage reading: " << sum / (ROWS*COLUMNS) << "\nHighest reading: " << max << '\n' ; }``````
Topic archived. No new replies allowed.