Write in C++, source code please! Mean and standard deviation. Write a program t
ID: 673886 • Letter: W
Question
Write in C++, source code please!
Mean and standard deviation. Write a program that reads a set of floating-point data values. Choose an appropriate mechanism for prompting for 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. The average of a data set {x_1,..., xn} is x = Sigma x_i/ n, where Sigma x_i = x_1 + ... + x_n is the sum of the input values. The standard deviation is s = square root summation (x_i - x)^2 / n - 1 However, this formula is not suitable for the task. By the time the program has computed x, the individual x_i are long gone. Until you know how to save these values, use the numerically less stable formula s = square root summation s_i^2 - 1/n (summation x_i)^2 / n - 1 You can compute this quantity by keeping track of the count, the sum, and the sum of squares as you process the input values.Explanation / Answer
#include <iostream>
#include<math.h>
using namespace std;
float standard_deviation(float data[], int n)
{
float mean=0.0, sum_deviation=0.0;
int i;
for(i=0; i<n;++i)
{
mean+=data[i];
}
mean=mean/n;
for(i=0; i<n;++i)
sum_deviation+=(data[i]-mean)*(data[i]-mean);
return sqrt(sum_deviation/n);
}
float mean(float data[],int n)
{
float sum = 0;
for(int i=0;i<n;i++)
sum = sum+data[i];
return sum/n;
}
int main()
{
int n;
cout << "How many float numbers you want to input ? ";
cin >> n;
float numbers[n];
for(int i=0;i<n;i++)
{
cout << "Input number "<<i+1 << " " ;
cin >> numbers[i];
}
cout << "Mean is : " << mean(numbers, n);
cout << "Standard Deviation is : " << standard_deviation(numbers, n);
cout << ' ';
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.