Another Set of Eyes on my Errors?

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...

student.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef STUDENT_HPP
#define STUDENT_HPP
#include <string>
using std::string;

class Student
{
	private:
		string name;
		double score;
	public:
		Student();
		Student(string, double);
		string getName(string);
		double getScore(double);
		double stdDev(int studentArray[], int sizeOfArray);
};
#endif 


student.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "Student.hpp"
#include <string>
using std::string;

Student::Student(string newName, double newScore)
{
	newName = name;
	newScore = score;
}
	
string getName(string name)
{
	return name;
}	
		
double getScore(double score)
{
	return score;
}


stdDev.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include "Student.hpp"
#include <string>
#include <cmath>
using std::string;
using std::cout;
using std::cin;
using std::endl;

double Student::stdDev(int studentArray[], int sizeOfArray);

int main()
{
	Student studentRob("Rob", 95);
	Student studentBob("Bob", 89);
	Student studentGob("Gob", 99);

	Student studentArray[3] = {studentRob, studentBob, studentGob};

 	cout << "Standard deviation is: " << stdDev << endl;
	
	return 0;
}

double Student::stdDev(int studentArray[], int sizeOfArray)
{
	Student *studentNew;
	int x;
	double standardDev;
	double total = 0;

	for (x = 0; x , sizeOfArray; x++)
	{
		total += studentNew->getScore(double);
	}

	double mean = total / sizeOfArray;

	for (int x = 0; x < sizeOfArray; x++)
	{
		standardDev += pow(Student[studentNew] - mean, 2);
	}

	return sqrt(standardDev / sizeOfArray);
}
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:
1
2
3
4
string Student::getName(string name)
{
	return name;
}
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:
1
2
3
4
5
6
7
8
9
10
11
12
//declaration:
void f(double);

//calls:
f(3.141592); //OK
f(0); //OK.
double x = 2.718281;
f(x); //OK.
double *y = &x;
f(*y); //OK
f(double); //error
f(y); //error 
Thanks for taking the time to help me out, I'm still stuck on an issue though. When trying to calculate standard deviation, I'm getting the error
1
2
error: expected primary-expression before ‘[’ token
   standardDev += pow(Student[x] - mean, 2);

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef STUDENT_HPP
#define STUDENT_HPP
#include <string>
using std::string;

class Student
{
	private:
		string name;
		double score;
	public:
		Student();
		Student(string, double);
		string getName();
		double getScore();
		double stdDev(int studentArray[], int sizeOfArray);
};
#endif 

student.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include "Student.hpp"
#include <string>
using std::string;

Student::Student(string newName, double newScore)
{
	newName = name;
	newScore = score;
}
	
string Student::getName()
{
	return name;
}	
		
double Student::getScore()
{
	return score;
}

stdDev.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include "Student.hpp"
#include <string>
#include <cmath>
using std::string;
using std::cout;
using std::cin;
using std::endl;

double stdDev(int studentArray[], int sizeOfArray);

int main()
{
	Student studentRob("Rob", 95);
	Student studentBob("Bob", 89);
	Student studentGob("Gob", 99);
	double standardDev;

	Student studentArray[3] = {studentRob, studentBob, studentGob};

 	cout << "Standard deviation is: " << standardDev << endl;
	
	return 0;
}

double Student::stdDev(int studentArray[], int sizeOfArray)
{
	Student *pointerToStudent = studentArray[];
	int x;
	double standardDev;
	double total = 0;

	for (x = 0; x < sizeOfArray; x++)
	{
		total += pointerToStudent->getScore();
	}

	double mean = total / sizeOfArray; 
	
	for (int x = 0; x < sizeOfArray; x++)
	{
		standardDev += pow(Student[x] - mean, 2);
	}

	return sqrt(standardDev / sizeOfArray);
}

Thank you to anyone willing to take the time to show/explain/guide me!
Last edited on
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.
Maybe your compiler will let you get away without, but standardDev ought to be initialised to 0 (probably on line 30) before you start adding to it.

In summing for total and standardDev just use studentArray[x].getScore(), not an unnecessary pointer or type.

In your constructor for Student you should be setting
name = newName;
and not the other way round.Similarly
score = newScore;
Last edited on
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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)(((long unsigned int)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();
                          ^

Can someone clarify? Thank you again.
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#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.)
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <cmath>

using std::string;

class Student
{
private:
    string name;
    double score;
public:
    Student(string = "??", double = 0);
    string getName();
    double getScore();
    double stdDev(Student studentArray[], size_t sizeOfArray);
};

Student::Student(string newName, double newScore)
{
    name = newName;
    score = newScore;
}

string Student::getName()
{
    return name;
}

double Student::getScore()
{
    return score;
}

double Student::stdDev(Student studentArray[], size_t sizeOfArray)
{
    double mean = 0;
    double deviation = 0;
    double variance_squared = 0;
    double total = 0;
    
    for (size_t x = 0; x < sizeOfArray; x++)
    {
        total += studentArray[x].getScore();
    }
    
    mean = total / sizeOfArray;
    
    for (size_t x = 0; x < sizeOfArray; x++)
    {
        deviation = studentArray[x].getScore() - mean;
        variance_squared += deviation * deviation;
    }
    
    return sqrt(variance_squared / sizeOfArray);
}

using std::string;
using std::cout;
using std::cin;
using std::endl;

int main()
{
    Student studentRob("Rob", 95);
    Student studentBob("Bob", 89);
    Student studentGob("Gob", 99);
    
    Student studentArray[] = {studentRob, studentBob, studentGob};
    
    Student whole_class;
    
    cout << "Standard deviation is: " << whole_class.stdDev(studentArray,
 sizeof(studentArray)/ sizeof(Student)) << endl;
    
    return 0;
}


Standard deviation is: 4.10961
Program ended with exit code: 0
Last edited on
Topic archived. No new replies allowed.