for( k = 0; k < n; k++ )
{
total += total + temps[k];
}
avgTemp = total / n;
temps is my array that contains n elements. avgTemp stores the average of all the values in temps. k is just some integer to make my loop work. k, n, and total are already declared appropriately somewhere above. total keeps track of the total of the elements in the array.
My exercise thing is telling me this is wrong. What am I doing wrong?
This
should be
or
Using the iterative summation would be even better. It allows to avoid the round-off errors.
bames53 also gives a nice STL-based code in the comment.