// HW 5.6 Write a program that uses a for statement to calculate the average of several integers.
// Assume the last value read is the sentinel 9999.
#include <iostream>
using namespace std;
int main()
{
float average;
int sum=0;
int input;
for ( int i = 0; i = 9999; ++i ){
cout << endl << "Please enter a number(9999 to exit).";
cin >> input;
if ( input == 9999){
cout << endl << "The average is" << average;
break;
}
sum += input;
average = (sum / i);
}// i for statement
}// main
The program is giving me an average of 0? I'm sure it's simple but, I'm missing something.
// HW 5.6 Write a program that uses a for statement to calculate the average of several integers.
// Assume the last value read is the sentinel 9999.
#include <iostream>
using namespace std;
int main()
{
float average;
int sum = 0;
int input;
for ( int i = 0; input = 9999; ++i ){
cout << endl << "Please enter a number(9999 to exit).";
cin >> input;
if ( input == 9999){
cout << endl << "The average of the numbers entered is:" << (average/i) << "\n";
break;
}
sum += input;
average = sum;
I'm pretty sure it is not working they way it is supposed to. When you do average / i what value does average hold, it's never initialized and it never is never assigned inside the for loop.
You need to move your summation up into the loop. The average is calculated by taking the sum and dividing by the number of integers in that sum. Meaning average = sum / count[/count] instead of [count]average / count
I ran several tests of the second code and it worked fine. It is assigned as average = sum in the for loop, I think... I'm really new to this, but the curly brace beneath it is labeled with a comment that says it goes to the for loop and the other curly braces go with the if statement, which I think I could do away with after thinking about the problem a bit more.
If I moved the calculations above the if statement I think it would incorrectly add 9999 to sum and my answer would be wrong.
Ok the formatting through me off, I see what you are saying.
The reason it is working is because in your first implementation you were setting i = 9999 and then calculating your average with average = sum / i which was actually average = sum / 9999.
Since both sum and i are integers you are doing integer division and you don't have the precision of a float, so even though you would expect a very small number it evaluates to zero. If you were to cast it to a float you would not have a zero but it would be a very small number (and still not correct)
Actually the summation IS in the loop but it's hard to tell without proper indentation. BUT, there are still major problems:
Why bother with average at all??? You declare it as a float but then assign it to an integer (sum) every iteration. Just use sum.
Since you didn't use floats your results will always be rounded since you press the loop variable (i) into service as the denominator of the average calculation.
BTW, you can leave out the input = 9999. Leaving it blank means no end condition. Init it to 0 when you declare it.
Play around with this a bit. Indent your code and simplify. Maybe think about a while-do loop instead of the for loop?