Describe a step by step process for each of the scenarios given below using C++.
ID: 674701 • Letter: D
Question
Describe a step by step process for each of the scenarios given below using C++.
In numerical analysis the average and standard deviation is often calculated. The average or mean is given by the formula
Average = x/n
where n is the number of x values.
The standard deviation is a measure of scatter and is given by the formula
Standard deviation = (((x – xave)2)/(n-1))0.5
where xave is the average x, and n is the number of x values.
Suppose you have an array of x values write the process to calculate the average and standard deviation for these values.
Explanation / Answer
#include<iostream>
#include <cmath>
using namespace std;
int main()
{
int arr[10] = {1,2,3,4,4,4,5,5,5,6};
float mean = 0;
float sum_deviation = 0.0;
for (int i=0;i<10;i++) {
mean = mean+arr[i];
}
cout << "Average is : " << mean/10 << endl;
for(int i=0; i<10;++i)
sum_deviation+=(arr[i]-mean)*(arr[i]-mean);
cout << "Standard deviation is : " << sqrt(sum_deviation/10) << endl;
return 0;
}