Count the loop iterations?so i dont know what number to divide by
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
The divide by will be different each iteration, which is why you need a simple counter which increases by one each time the user enters a number. The first number entered will just BE the average of course, but the second one will only get a 1/2 share. To add the fifth number into your ongoing average, you might use the following calculation:
mean=mean*(4/5) + number*(1/5)
If a sixth number is added, you'll need to update your average:
mean=mean*(5/6) + number*(1/6)
What you're saying is correct, but the exact implementation isn't -- (1/6) == 0. Since the counter is almost certainly an integer, you'll need a cast to double before doing the division.
But I wouldn't do it that way anyway -- I'd just count the iterations and divide once at the end. All those intermediate divisions and additions would make me nervous about floating point noise.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Oh yeah, duh, you can just divide the total at the end. You will need to go to a float though, unless the user happens to enter numbers that divide evenly, which isn't likely.