Write a class called Student that has two data members - a string variable calle
ID: 3869278 • Letter: W
Question
Write a class called Student that has two data members - a string variable called name and a double variable called score. It should have a constructor that takes two values and uses them to initialize the data members. It should have get methods for both data members (getName and getAge), but doesn't need any set methods.
Write a separate function (not part of the Person class) called stdDev that takes two parameters - an array of Student and the size of the array - and returns the standard deviation of all the scores(the population standard deviation that uses a denominator of N, not the sample standard deviation, which uses a different denominator). Please do not use pointers or global variabes.
Explanation / Answer
The following Student class contains data members- name of type string and score of double type.
The Student constructor takes in a string and double variable to initialize name and score for the student.
The getName() member function lets user input the student name. And getScore() member function lets the user enter the score.
Finally, the stdDev() function takes two parameters - studentScore[] array of type double and n the array size.
class Student{
private:
string name;
double score;
public:
Student(string sname, double sscore)
{
name = sname;
score = sscore;
}
void getName()
{
cout<<"Enter the student name: ";
cin>>name;
}
void getScore()
{
cout<<"Enter the student score: ";
cin>>score;
}
};
float stdDev(double studentScores[], int n)
{
double sum = 0, mean, sd = 0.0;
int i;
for(i=0; i<n; ++i)
{
sum += studentScores[i]; //sum of all scores
}
mean = sum/n;
for(i=0; i<n; ++i)
sd += pow(studentScores[i] - mean, 2); //to calculate mean deviation
return sqrt(sd/n); //returning standard deviation
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.