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