Average is incorrect

Hi guys I am a beginner in C++ and just started using Class function. I have come up with a code but my average is incorrect and I am unable to figure out what is the mistake. Pls help mi guys ....
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <string>
using namespace std;
// Class Declaration 
class StallRating
{ 
private:  
	string name;   
	int numRated[5];
	double result;

public:  
	StallRating(); // Constructor  
	StallRating(string Name); // Overloaded Constructor  
	string getName(); // Accessor of name  
	void setName(string newName); // Mutator of name   
	void addRating(int num); // Add rating, 1 - 5 to the stall  
	double getAverage(); // Find the average of the ratings
	void display();  // Display
};

// Implementation
StallRating :: StallRating()
{
	name = "Unknown";
	numRated[0]=0;
	numRated[1]=0;
	numRated[2]=0;
	numRated[3]=0;
	numRated[4]=0;
}

StallRating :: StallRating( string Name)
{
	name = Name;
	numRated[0]=0;
	numRated[1]=0;
	numRated[2]=0;
	numRated[3]=0;
	numRated[4]=0;

}

void StallRating :: setName(string newName)
{ 
	name = newName;
}

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

void StallRating :: addRating(int num)
{
	int i = num;
	if(num>0 && num <=5)
	{
	 numRated[i-1]++;
	}

else 
{
	cout<<"Number out of range."<<endl;
}
	
}
double StallRating :: getAverage()
{
	double num = numRated[0]+numRated[1]+numRated[2]+numRated[3]+numRated[4];
	double total = 5.0;
	double result = num/total;

	return result;
}

void StallRating :: display()
{
	cout<<name<<", Average rating: "<<result<<".";
}


int main()
{
	StallRating Stall1;
	Stall1.setName("Macpherson Market Stall ID 2");
	Stall1.addRating(5);
	Stall1.addRating(5);
	Stall1.addRating(4);
	Stall1.addRating(4);
	Stall1.addRating(5);
	Stall1.getAverage();
	Stall1.display();
	
	StallRating Stall2;
	Stall2.setName("SunTec City Stall ID 3");
	Stall2.addRating(3);
	Stall2.addRating(2);
	Stall2.addRating(2);
	Stall2.addRating(4);
	Stall2.addRating(1);
	Stall2.getAverage();
	Stall2.display();

	StallRating Stall3;
	Stall3.setName("Lau Pa Sat Stall ID 5");
	Stall3.addRating(3);
	Stall3.addRating(2);
	Stall3.addRating(3);
	Stall3.addRating(4);
	Stall3.addRating(1);
	Stall3.getAverage();
	Stall3.display();
}
In your addRating(), you're collecting the count of that point. However in getAverage() you sum the total count and divide by 5. It's not calculating average. Check your algorithm.
Hi liuyang

Don't mind can you tell in detail where should I be making the changes as I still don't get the mistake.

Thank you
In your addRating(), you're counting HOW MANY items match that rating.
For example in your case 1, your numRated would be

0 0 0 2 3

If you simply sum them and divide the result by 5, you'll get 1, which is incorrect. Your equation should be:

(numRated[0] * 1 + numRated[1] * 2 + numRated[2] * 3 + numRated[3] * 4 + numRated[4] * 5) / 5.0


So, TC, what happened to your original post?
Here is the original post. In future avoid deletin your posts. It prevents people who know how to use search from getting help quickly.

dev05 wrote:
Hi guys I am a beginner in C++ and just started using Class function. I have come up with a code but my average is incorrect and I am unable to figure out what is the mistake. Pls help mi guys ....
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <string>
using namespace std;
// Class Declaration 
class StallRating
{ 
private:  
	string name;   
	int numRated[5];
	double result;

public:  
	StallRating(); // Constructor  
	StallRating(string Name); // Overloaded Constructor  
	string getName(); // Accessor of name  
	void setName(string newName); // Mutator of name   
	void addRating(int num); // Add rating, 1 - 5 to the stall  
	double getAverage(); // Find the average of the ratings
	void display();  // Display
};

// Implementation
StallRating :: StallRating()
{
	name = "Unknown";
	numRated[0]=0;
	numRated[1]=0;
	numRated[2]=0;
	numRated[3]=0;
	numRated[4]=0;
}

StallRating :: StallRating( string Name)
{
	name = Name;
	numRated[0]=0;
	numRated[1]=0;
	numRated[2]=0;
	numRated[3]=0;
	numRated[4]=0;

}

void StallRating :: setName(string newName)
{ 
	name = newName;
}

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

void StallRating :: addRating(int num)
{
	int i = num;
	if(num>0 && num <=5)
	{
	 numRated[i-1]++;
	}

else 
{
	cout<<"Number out of range."<<endl;
}
	
}
double StallRating :: getAverage()
{
	double num = numRated[0]+numRated[1]+numRated[2]+numRated[3]+numRated[4];
	double total = 5.0;
	double result = num/total;

	return result;
}

void StallRating :: display()
{
	cout<<name<<", Average rating: "<<result<<".";
}


int main()
{
	StallRating Stall1;
	Stall1.setName("Macpherson Market Stall ID 2");
	Stall1.addRating(5);
	Stall1.addRating(5);
	Stall1.addRating(4);
	Stall1.addRating(4);
	Stall1.addRating(5);
	Stall1.getAverage();
	Stall1.display();
	
	StallRating Stall2;
	Stall2.setName("SunTec City Stall ID 3");
	Stall2.addRating(3);
	Stall2.addRating(2);
	Stall2.addRating(2);
	Stall2.addRating(4);
	Stall2.addRating(1);
	Stall2.getAverage();
	Stall2.display();

	StallRating Stall3;
	Stall3.setName("Lau Pa Sat Stall ID 5");
	Stall3.addRating(3);
	Stall3.addRating(2);
	Stall3.addRating(3);
	Stall3.addRating(4);
	Stall3.addRating(1);
	Stall3.getAverage();
	Stall3.display();
}
EDIT:
YOU HAVE DECLARED a local variable with the same name RESULT...Remove that declaration and your program will work


1
2
3
4
5
6
7
8
9
10
11
12
13
14
double StallRating :: getAverage()
{
	double num = numRated[0]+numRated[1]+numRated[2]+numRated[3]+numRated[4];
	double total = 5.0;
        result = num/total; /// Local variable declaration removed

	return result;
}
//just my 2 cents try to use loops when dealing with arrays its a lot nicer looking
// for example
for(int i = 0; i < NUMBER_OF_GRADES)
{
     sum += grades[i];
}
Last edited on
Please DON'T delete your question after you've received your answer. It's extremely selfish - it deprives future readers of the opportunity to learn from this thread.
Yeah probably embarrassed he declared a local variable of the same name and just realized it :o
Yeah probably embarrassed he declared a local variable of the same name and just realized it :o

Seems unlikely, since that didn't cause any problem. The actual problem was addressed in the second post in the thread.
Interesting...How would that not cause a problem? He is storing all the info in to a variable local to that function and once out of scope that variable will be non existent..then he calls a display function that displays the other variable result
Last edited on
Topic archived. No new replies allowed.