Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NOTE: I do not want a whole new code. I just want to know where the code went wr

ID: 3869364 • Letter: N

Question

NOTE: I do not want a whole new code. I just want to know where the code went wrong and why it returns the wrong standard deviation. If found, can you please correct! thank you.

Here is program so far:

#include <string>
#include <cmath>
#include "Person.hpp"

using namespace std;

double stdDev(Person array[], int size)
{

double sum=0.0;
double mean;
double stdDev=0.0;

for (int i=0;i<=size;i++)
{
cout<<array[i].getAge()<<endl;
cout<<array[i].getName()<<endl;
sum+=array[i].getAge();
}

mean=sum/size;

for (int i=0;i<=size;i++)
{
stdDev += pow(array[i].getAge() - mean, 2);
}
return sqrt(stdDev/size);
}


int main()
{
double result;
Person myArray[]={
Person("Joe",25),
Person("Joey",40),
Person("Kyle",20),
Person("Dave",28)
};
result=stdDev(myArray, 3);
cout<<"The standard deviation is: "<<result<<endl;

return 0;
}

for my output i get:

The standard deviation is: 13.8015.

However, this is not the correct standard deviation.

Explanation / Answer

#include <string>
#include <cmath>
#include "Person.hpp"

using namespace std;

double stdDev(Person array[], int size)
{

double sum=0.0;
double mean;
double stdDev=0.0;

for (int i=0;i<=size;i++)
{
cout<<array[i].getAge()<<endl;
cout<<array[i].getName()<<endl;
sum+=array[i].getAge();
}

mean=sum/size;

for (int i=0;i<=size;i++)
{
stdDev += pow(array[i].getAge() - mean, 2);
}
return sqrt(stdDev/size);
}


int main()
{
double result;
Person myArray[]={
Person("Joe",25),
Person("Joey",40),
Person("Kyle",20),
Person("Dave",28)
};
result=stdDev(myArray, 4);
cout<<"The standard deviation is: "<<result<<endl;

return 0;
}

==============================

you have given sie as 3.It should be 4