Write a program to calculate the average and the standard deviation of a list of
ID: 3667383 • Letter: W
Question
Write a program to calculate the average and the standard deviation of a list of numbers. You program should...
Prompt for the number of values the user wishes to enter
Prompt the user to enter the values one at a time Calculate and display the average and the standard deviation
The flow of your code should be similar to:
int main()
{
/** Prompt the user and get the number of values to enter **/
/** Get the values from the user **/
/** Calculate and display the average **/
/** Calculate the standard deviation **/
return 0;
}
In c++ using a vector to contain the values entered by the user
Explanation / Answer
#include <iostream>
#include <cmath>
using namespace std;
float standard_deviation(float data[], int n);
int main()
{
int n, i;
float data[100],sum=0.0, average;
cout << "Enter number of data: ";
cin >> n;
while (n>100 || n<=0)
{
cout << "Error! number should in range of (1 to 100)." << endl;
cout << "Enter the number of data again: ";
cin >> n;
}
cout << "Enter elements: " << endl;
for(i=0; i<n; ++i)
{
cin >> data[i];
sum+=data[i];
}
average=sum/n;
cout << endl;
cout << "Average = " << average << " ";
cout << "Standard Deviation = " << standard_deviation(data,n);
return 0;
}
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);
}
OUT PUT:
Enter number of data: 4
Enter elements:
1
2
3
5
Average = 2.75
Standard Deviation = 1.47902
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.