| cspctec (17) | |||
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:
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. | |||
|
|
|||
| cire (2362) | |||
|
1. Get and store input. 2. Manipulate data. Not: 1. Get 1 input. 2. Attempt to manipulate data not yet input. 3. repeat.
column_sum... when do you think the row index is also a valid column index? | |||
|
|
|||
| cspctec (17) | |
|
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
|
|
| cire (2362) | |
|
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. | |
|
|
|
| cspctec (17) | |||
I tried to put the column_sum outside of the first loop like this:
But it won't compile. What does the error "name lookup of 'y' changed for ISO 'for' scoping"? | |||
|
|
|||
| cspctec (17) | |||
I ended up adding another loop to the program:
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). | |||
|
|
|||
| cspctec (17) | |||
I re-wrote the program to make it a little bit smaller:
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? | |||
|
|
|||
| cire (2362) | |||
|
I wouldn't average the rows in the loop you're averaging the rows. Consider the following:
| |||
|
|
|||