help with average and classes

closed account (jihpDjzh)
User enters # of exams, and score for each.
I then get the average, but it outputs the #'s the user has entered.

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
#include <iomanip>
#include <iostream>
#include <string>

using namespace std;

class TestScores
{
	int scores;

public:
	TestScores::TestScores()
	{
		scores = 0;
	}

	void TestScores::setScores(int s)
	{
		scores = s;
	}
	void TestScores::getScores()
	{

		cout << scores << ", ";
	}
	void Average(int num)
	{
		int sum;
		sum = 0;
		for (int i= 0; i < num; i++)
		{
			sum = sum + scores;
		}
		
		int mysum;
		mysum = sum / num;
		cout << mysum << endl;
	}

	};



int main()
{
	int Num_Scores;
	Num_Scores = 0;
	cout << "How many Test Scores? : "; cin >> Num_Scores;

	TestScores* myscores;
	myscores = new TestScores[Num_Scores];
	cout << "Enter " << Num_Scores << " Test Scores." << endl;

	for (int i = 0; i < Num_Scores; i++)
	{
		int x;
		cout << "Test " << i + 1 << ": "; cin >> x;
		myscores[i].setScores(x);
	}
	cout << "All Scores: ";
	for (int g = 0; g < Num_Scores; g++)
	{
		myscores[g].getScores();
	}
	cout << endl;

	for (int i = 0; i < Num_Scores; i++)
	{
	 myscores[i].Average(Num_Scores);
	}


	return 0;
}
How about this way...

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
#include <iostream>

using namespace std;

class TestScores
{
	double scores[50]; // 50 or any number you think is MAX. I, myself, am
                           // trying to figure out how to enter a variable  
public:                    // literal as a constant array size.
   
	void setScores(double s[], int arraySize)
	{
	    for (int i= 0; i < arraySize; i++)
		    scores[i] = s[i];
	}

	void getScores(int arraySize)
	{
           for (int i= 0; i < arraySize; i++)
               cout << scores[i] << ", ";
	}
	
	double Average(int num)
	{
		double sum = 0;
		for (int i= 0; i < num; i++)
			sum += scores[i];

		return sum / num;
	}
};

int main()
{
	int Num_Scores = 0;
	cout << "How many Test Scores? : "; 
	cin >> Num_Scores;

	TestScores myscores;
	cout << "Enter " << Num_Scores << " Test Scores." << endl;

	for (int i = 0; i < Num_Scores; i++)
	{
		double x[Num_Scores];
		cout << "Test " << i + 1 << ": "; 
		cin >> x[i];
		myscores.setScores(x,Num_Scores);
	}
	cout << "All Scores: ";
	myscores.getScores(Num_Scores);

	cout << endl << "Average: " << myscores.Average(Num_Scores);

	return 0;
}


Happy coding...
Last edited on
Topic archived. No new replies allowed.