. Suppose you want to read some positive integers and average them, but you do not have a preset number of data items in mind. Suppose the number -999 marks the end of the data. Find the sum and the average of these numbers
//Program reads some positive integers and average them
// Find the sum and the average of these numbers
// the preprocessing directives
#include <iostream>
using namespace std;
int main() {
// declare and initialize variables
int i=0, count=0, sum=0;
double avg;
// loop while !=-999
while(1){
cout << "i = ";
cin >> i;
if(i!=-999){
sum = sum+i;
count++;
}
else
break;
}
avg = double(sum)/double(count);
cout<<"\nSUM = "<<sum;
cout<<"\nAVG = "<<avg;
cout<<endl;
return 0;
}
Comments