I'm lost at sea and am hoping someone could help steer me in the right direction. I am supposed to write a modular program that reads a student's name and score and then calculate the student scores'standard deviation based on an array of pointers to the students. I think that my student files are correct but my standard deviation function needs help...
1. Remove the double parameter for Student::getScore(). You don't need it.
2. Same for the string parameter for Student::getName().
3. On lines 12 and 17, you're not defining the member functions of Student, you're defining two global functions that happen to have the same name. For example:
4. With the previous modifications, line 34 will now simply change to something like total += studentNew->getScore();, but keep in mind that if a function takes a double, you can't just write the word 'double' at the call site. You have to actually pass a double. For example:
I've copied the formula from other sites detailing how to calculate standard deviation but it still isn't working. Can someone clarify? I'm also supposed to calculate the student scores' standard deviation based on an array of pointers to the students. I'm not clear exactly what an array of objects would look like so I'm using arrays as I understand them (which is clearly limited)...
student.hpp
Line 28: This is wrong: Student *pointerToStudent = studentArray[]; Just remove that line. You won't need it.
Line 35: See the following example for how to use an array:
1 2 3 4
int array[] = { 1, 2, 3, 4 };
int sum = 0;
for (int i = 0; i < 4; i++)
sum += array[i];
Line 42: 'Student' is a type, not an array. See previous point.
Thank you both for the quick replies. I have fixed my constructor and the other corrections you pointed out, but my compiler is still giving me errors with getScore.
double Student::stdDev(double studentArray[], int sizeOfArray)
{
int x;
double standardDev = 0;
double total = 0;
for (x = 0; x < sizeOfArray; x++)
{
total += studentArray[x].getScore();
}
double mean = total / sizeOfArray;
for (int x = 0; x < sizeOfArray; x++)
{
standardDev += pow(studentArray[x] - mean, 2);
}
return sqrt(standardDev / sizeOfArray);
}
When I try and compile I get the following:
1 2
request for member ‘getScore’ in ‘*(studentArray + ((sizetype)(((longunsignedint)x) * 8ul)))’, which is of non-class type ‘double’
total += studentArray[x].getScore();
Thinking that I may be causing issues by passing an int (x) into a function that is supposed to return a double, I switched "int x" to "double x" and got this error:
1 2 3
error: invalid types ‘double*[double]’ for array subscript
total += studentArray[x].getScore();
^
Too many versions of your code floating around. Note the multiple amendments to your code below. If you have made the stated changes to your other files then this should work.
Think VERY carefully about the types of all variables and functions. They may be an intrinsic type like int or double, or a derived type like Student. An array index (here, x) is always an integer, never a double - it is not a function parameter.
#include <iostream>
#include "Student.hpp"
#include <string>
#include <cmath>
using std::string;
using std::cout;
using std::cin;
using std::endl;
double stdDev( Student studentArray[], int sizeOfArray); // <===== Note the type of studentArray[] is Student
int main()
{
Student studentRob("Rob", 95);
Student studentBob("Bob", 89);
Student studentGob("Gob", 99);
double standardDev;
Student studentArray[3] = {studentRob, studentBob, studentGob};
standardDev = stdDev( studentArray, 3 ); // <==== need to actually work it out
cout << "Standard deviation is: " << standardDev << endl;
return 0;
}
double stdDev( Student studentArray[], int sizeOfArray) // <===== Note the type of studentArray[] is Student
{ // and that of the function is just double
int x;
double standardDev = 0;
double total = 0;
for (x = 0; x < sizeOfArray; x++)
{
total += studentArray[x].getScore();
}
double mean = total / sizeOfArray;
for (int x = 0; x < sizeOfArray; x++)
{
standardDev += pow(studentArray[x].getScore() - mean, 2); // <===== Need getScore()
}
return sqrt(standardDev / sizeOfArray);
}
Other things to think about.
- There are mathematically better and more efficient ways of finding standard deviation, especially if you aren't actually returning the mean as well.
- You are tied to the "magic number" of 3 students. (Where I work we wouldn't be allowed to have classes that small.)