Finding the Average from an array

closed account (jEb91hU5)
I'm having a small problem finding the average from an array that has been read from a text file. The text file has 20 names (first and last) along with a number attached to each name. These numbers have been inputted into a struct the same way the names have--record[i].firstName and record[i].lastName and record[i].testScore. I need to average the test scores but the code isn't doing what I want no matter what I try to compute. This is what I have at the moment, but I've tried many other things. I feel like I'm missing something very simple.

1
2
3
4
5
6
7
8
case 5: // Print the average of all test scores
			int ave;
			for (int i = 0; i < SIZE; i++)
			{
				ave = record[i].testScore / SIZE;
			}
			cout << "The average of all test scores is " << ave << endl;
			break;
To find an average ... ADD THEM UP and, once you have FINISHED adding them up, then divide by the number of them.

Your code doesn't add anything up.

Your code doesn't wait until it has finished going through the scores before dividing the (as-yet-not-found) sum by the number of them.
closed account (jEb91hU5)
This is what I tried using before and I'm getting too large of a number. Am I adding wrong?

1
2
3
4
5
6
7
8
case 5: // Print the average of all test scores
			int ave;
			for (int i = 0; i < SIZE; i++)
			{
				ave = (record[i].testScore + record[i + 1].testScore) / SIZE;
			}
			cout << "The average of all test scores is " << ave << endl;
			break;
JLaw21 wrote:
Am I adding wrong?


!*%?*% ... yes!


- You haven't initialised any variable that might reasonably construed as a sum to 0.

- All you are doing within the for loop is adding two consecutive array elements together (and going beyond the end of an array - but that's the least of your problems).

- You are still trying to divide by the number of items every time you (incorrectly) add one; WAIT 'til the for loop is finished!
Lets say that you have {2,5,3,8}. Your loop will do 4 iterations:

i=0: ave=(2+5)/4
i=1: ave=(5+3)/4
i=2: ave=(3+8)/4
i=3: ave=(8+X)/4

Every iteration overwrites the ave. Therefore, after the loop:
ave == (8+X)/4

What is X? Nobody knows. The array does not have 5th element.

You want to achieve:
1
2
sum = 2+5+3+8
ave = sum/4


What one iteration of loop should do? Add current element's value to the sum. Only that.

What is the value of sum before first iteration?
A simple change of the line 5 will resolve this issue; the line to become:

ave += record[i].testScore / SIZE;

Of course, the observation of lastchance is the best one.
Just remember to initialise ave (or whatever variable you are actually using to hold a sum) to zero.

And watch out for integer division.
@condor: Mathematically 2/4 + 5/4 + 3/4 + 8/4 == (2+5+3+8)/4

However, computers do not behave exactly like math. Calculations involving floating point numbers are not trivially intuitive. In integer division the left side evolves to 0+1+0+2, i.e. to 3 and the right side to 4, while math says 4½.

Furthermore, the left side has 3 additions and 4 divisions, but the right side has 3 additions and only 1 division. Even if you would get same result, one approach would require more computations than the other. (Insignificant difference in a toy program, but not something one should learn to ignore.)
closed account (jEb91hU5)
Thank you all for your help. I was initializing it but it was still giving me an error. I finally was able to fix it by putting {} around the 'case.'
Topic archived. No new replies allowed.