Define a function that takes an array of numbers as its argument and returns the
ID: 3539119 • Letter: D
Question
Define a function that takes an array of numbers as its argument and returns the standard deviation of the numbers in the array. Your function should have two parameters: an array parameter and a formal parameter of type int that gives the size of the array. The numbers in the array will be of type double, but will not be negative. To start, use a const int set to 5 to define the size of the array and allow the user to input that many numbers to fill it. Your main program should accept the input to fill the array, make a call to the function to calculate the standard deviation, and then output the result. the program should also allow the user to input as many values as they would like (up to 20). Then call the function with the partially filled array.Explanation / Answer
please rate - thanks
any changes needed, let me know
#include <iostream>
#include <math.h>
#include <fstream>
using namespace std;
double mean(double [],int,double&);
double standardd(double[],int, double);
double calc(int, double);
char letter(double);
int main()
{double x[20];
int i=0,j;
double average,sd,sum=0;
do
{cout<<"Enter a number (-9999 when done) ";
cin>>x[i];
i++;
}while(i<20&&x[i-1]!=-9999);
if(x[i-1]==-9999)
i--;
average=mean(x,i,sum);
sd=standardd(x,i,average);
cout<<" The average is "<<average<<" ";
cout<<"The standard deviation is: "<<sd<<" ";
system("pause");
return 0;
}
double mean(double x[],int n,double& sum)
{int i;
for(i=0;i<n;i++)
sum+=x[i];
return (double)sum/n;
}
double calc(double x,double avg)
{double temp;
temp=x-avg;
return temp*temp;
}
double standardd(double x[],int n, double average)
{double mean,sum=0;
int i;
for(i=0;i<n;i++)
sum+=calc(x[i],average);
mean=sum/n;
return sqrt(mean);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.