Write a program that reads a set of floating-point data values that uses an appr
ID: 3556292 • Letter: W
Question
Write a program that reads a set of floating-point data values that uses an appropriate sentinel for indicating the
end of the data set. When all values have been read, print out the count of the values, the average, and the
standard deviation. Notes: The average of a data set { x1, . . ., xn } is:
where n is the number of input values. The standard deviation is:
You can compute this quantity by keeping track of the count, the sum, and the sum of squares as you process
the input values.
Enter numbers - Q to quit: 1 2 3 4 5 6 q
n = 6, average = 3.5, standard deviation = 1.87083
Enter numbers - Q to quit: 1.1 3.3 4.4 6.6 9.9 q
Explanation / Answer
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
float sum=0.0,sum_sq=0.0,x;
int n=0;
cout<<" Enter the numbers. Enter '-0' to quit ";
while(1)
{
cin>>x;
if(x==-0)
break;
n++;
sum+=x;
sum_sq+=x*x;
}
cout<<" n = "<<n<<endl;
cout<<" Mean = "<<(sum*1.0/n)<<endl;
cout<<" Standard deviation = "<<pow(((sum_sq-(sum*sum*1.0)/n)*1.0/(n-1)),0.5)<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.