(Averaging Integers) Write a program that uses a for statement to calculate the average of several integers. Assume the last value read is the sentinel 9999.
For example, the sequence 10 8 11 7 9 9999 indicates that the program should calculate the average of all the values preceding 9999.
For example, the sequence 10 8 11 7 9 9999 indicates that the program should calculate the average of all the values preceding 9999.
Solution:
#include < iostream> using namespace std; int main() { int sum; int counter = 0; cout << "Enter integers for average calculation." << "Enter 9999 to end." << endl; cin >> sum; for( int x = 0; x != 9999; counter++){ sum += x; cin >> x; } cout << "Average is " << sum/counter << endl; //for pause system("PAUSE"); return 0; }
Post A Comment:
0 comments: