PROBLEM: Thestandarddeviationofalistofnumbersisameasureofhowmuchthenumbers devia
ID: 3539112 • Letter: P
Question
PROBLEM:
Thestandarddeviationofalistofnumbersisameasureofhowmuchthenumbers
deviatefromtheaverage.Ifthestandarddeviationissmall,thenumbersare
clusteredcloseto theaverage.Ifthestandarddeviationislarge,thenumbersare
scatteredfarfromtheaverage.Thestandarddeviation,S,ofalistofNnumbersxi is
definedasfollows,
S =
where x istheaverageoftheNnumbersx1,x2,....Defineafunctionthattakesan
arrayofnumbersasitsargumentandreturnsthestandarddeviationofthe
numbersinthearray.Yourfunctionshouldhave twoparameters:anarray
parameterandaformalparameteroftypeint thatgives thesizeofthearray.The
numbersinthearraywillbeoftypedouble,butwillnotbenegative. Tostart,use
aconstintsetto5todefinethesizeofthearrayandallowtheusertoinputthat
manynumberstofillit.
Yourmainprogramshouldaccepttheinputtofillthearray,makeacalltothe
functiontocalculatethestandarddeviation,andthenoutputtheresult.
Forextracredit,allowtheusertoinputasmanyvaluesastheywouldlike (upto
20). Thencallthefunctionwiththepartiallyfilledarray.
hint:
1). Rememberthefunctionsqrt()
2). Don'tbeintimidatedbytheequationabove.Breakitdown:first,calculate
theaverage;thenloopthroughthearray performingasubtractionand
accumulatingthesum ofthesedifferences;thendividethatsumbyNand
takethesquareroot.
3). Toallowtheusertoenteravariableamountofnumbers,youcanfirstaskfor
thenumberofinputsandacceptthatmanyoryoucanprompttheuserto
enter-1toflagtheendof their list(i.e.loopwhileinputnumber !=-1).
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[30];
int i=0,j;
double average,sd,sum=0;
do
{cout<<"Enter a number (-9999 when done) ";
cin>>x[i];
i++;
}while(i<30&&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.