Help with dynamic arrays and pointers!

Hi i am writing a class that dynamically allocates an array that holds a user-defined number of test scores (test scores go from 0 to 10 both included).
Once all the test scores are entered and validated (values only between 0 and 10, both included), the array should be passed to a function that sorts them in ascending order.
Another function should be called that calculates theaverage of all the scores.The main program should display the sorted list of scoresand the average of the scores with appropriate headings.

I am pretty familiar with what needs to be done, i am just having a bit of trouble writing it as a class. Thanks for any help or suggestions!! Ps my goal is to learn and understand these :)
, i am just having a bit of trouble writing it as a class

If you could post the code you have so far and explain specifically what part you are stuck on. Classes are a big subject so it's hard to tell exactly what part you find confusing.
Write a class with the pointer and an integer representing the amount of scores entered as your private attributes and the functions as your member functions.

Make a function that takes in a test score to store it in pointer array.

Make a function that finds the average and return the average.

You could have a function that returns a string representation of all the scores, using a stringstream and returning it in string format. There are a bunch of other ways to do this as well, but I suggest this cause I love the simplcity of stringstream.

DO NOT forget the deconstrutor. You need to deallocate all that memory at the end. Nobody likes memory leaks.

Good luck!
Thanks so much PrivatRyan! this is what i have so far:

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
#include <iostream>
using namespace std;

class TestScores
{
private:
	int *score;
	double average;
	double total;
	int size;
	int numScores;
public:
	TestScores();
	void getScore();
	void setTotal();
	void getAverage(int*, int size);
};
void TestScores::getScore()
{
	score= new int [numScores];
	for(int i=0;i<numScores;i++)
	{
		cout<<"Enter the score for test#"<<i+1;
		cin>>score[i];
}
void TestScores::setTotal()
{total=0.0;}
void TestScores::getAverage()
{
	double sum=0.0,avg;
	for(int i=0;i<size;i++)
	{
		sum+=score[i];
	}
	return avg=sum/size;
}


I just need to know if im on the right path
Remember that member functions of an object have access to their own class private attributes, so the getAverage() function does not need anything to be passed in.

The int size private attribute that records the number of integers in the int array int* score is going to be the same as int numScores, so you don't need int numScores as well.

You do not need need to store double average or double total as private attributes. They are variables only found inside you functions. The only time you would want to store a value like that is if you had to get to it a lot and the process to find it was a massive calculation.

In short, you really only need int *score and int size.

You getScore() confuses me. It needs to be able to be called multiple times by the user, so write it like a stack. It is bad practice to have cin and cout inside of a function. The user input needs to happen in the main.

Do not forget you destructor. In this program is very simple, just a single line to delete (though some would argue that you also set your score to NULL in case it isn't done automatically)
Last edited on
ok this is what i have worked on
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
#include <iostream>
using namespace std;

class TestScores
{
private:
	double *score;
	int numScores;
public:
	void getScore();
	void sortScore(double score[], int numScores);
	void showScore(const double score[], int numScores);
	double getAverage(double*, int);
	
};

void TestScores::getScore()
{
	score= new double [numScores];
	for(int i=0;i<numScores;i++)
	{
		cout<<"Enter the score for test#"<<i+1;
		cin>>score[i];
		while(numScores<0||numScores>10)
		{
			cout<<"Error! Invalid score. Please try again."<<endl;
			cin>>score[i];
		}
	}
}

double TestScores::getAverage(double *score,int numScores)
{
	double total=0.0,avg;
	for(int i=0;i<numScores;i++)
	{
		total+=score[i];
	}
	avg=total/numScores;
	cout<<"The average is"<<avg;
	return avg;
}

void TestScores::sortScore(double score[], int numScores)
{
	int temp;
	bool swap;
	do
	{
		swap=false;
		for(int count=0;count<(numScores-1);count++)
		{
			if(score[count]>score[count+1])
			{
				temp=score[count];
				score[count]=score[count+1];
				score[count+1]=temp;
				swap=true;
			}
		}
	}while(swap);
}
void TestScores::showScore(const double score[], int numScores)
{
	for(int count=0;count<numScores;count++)
		cout<<score[count]<<" ";
	cout<<endl;
}

int main()
{
	int size;
	double *grades;
	TestScores student;
	student.getScore();
	student.sortScore( grades,size);
	student.getAverage(grades,size);
	student.showScore(grades,size);
	delete grades;
	system("pause");

}
Topic archived. No new replies allowed.