Write a C++ program that can figure out the average value of a set of numbers en
ID: 3912180 • Letter: W
Question
Write a C++ program that can figure out the average value of a set of numbers entered at the keyboard. Allow your program runs continuously until user wants to end. Requirements: Number: integers and real numbers. Assume, all values entered are numeric. Set: the number of elements could be any (including nothing for empty set) and is unknown at the run time. So, don’t ask for “How many”. Precision: set result with a limit to 2 digits after the dot. Output must be held on screen to view at the end of the run. Your name as programmer who wrote the code must be displayed (ONLY) at the end of the run.
Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
int main(){
float number,count=0,sum=0;
cout<<"enter a character to stop input ";
while(cin>>number){ // cin return false when a character is entered
sum+=number;
count++;
}
cout<<"Average is: ";
cout<<fixed<<setprecision(2);
if(count==0)cout<<"0"<<" ";
else cout<<sum/count<<" ";
cout<<"your name ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.