Answer to Question #172118 in C++ for sara

Question #172118

. 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


1
Expert's answer
2021-03-16T08:02:54-0400

//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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS