(HINT: You need to sort the vector or other container) |
The key words in there are Other Container. Meaning you can use a array, list, vector any container to store the results of the user entering the grades.
So you can use a array to hold the grades instead of a vector (In my opinion the professor should give you bonus points for using a vector since its up to date code but =/). You can sort a array with a bubble sort algorithm which is what most beginners start out with (More into below).
So this is what you need to do to the code I posted to have it work for your assignment.
1. Replace the vectors with arrays. This is actually a big problem and why vectors are so much better then array for this. Let me explain, when you make a array you need to define how many elements are in that array. Now you might just make a array with 100 elements in it and call it done. That is not correct because when you try and sort that array you will be sorting undefined elements and you will not get the right result.
So what you need to do is everytime you add a element(grade) to the array you need to create a new array and copy all the elements from the old array into it, then delete the old array. This is a lot of work considering in a vector you can just do
grades.push_back(n)
compared to something like this
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
|
int main()
{
int *Old_array(new int[4]);
for(int I(0); I < 4; ++I)
Old_array[I] = (I + 1);
{
// Resize.
int *New_array(new int[5]);
// Copy over.
for(int I(0); I < 5; ++I)
New_array[I] = ((I == 4) ? 0 : Old_array[I]);
// We no longer need the old array.
delete [ ] Old_array;
Old_array = New_array;
}
for(int I(0); I < 5; ++I)
std::cout << Old_array[I] << " ";
delete [ ] Old_array;
}
// Borrowed from Framework since I didn't want to type out a new one.
|
Or you can keep track of the number of elements that the user entered grades into with a counter and then only sort up to that number of elements, so you are not sorting uninitialized elements.
Either way would work.
So you can see why vectors would be better.
2) Input the users data into the arrays. You can do this by doing what I said above, create a new array -> Copy the old arrays elements into the new array -> delete the old array.
3) Sort the array. You can use a bubblesort for this since that is what most beginners learn first. There are much better sorting options available also.
4) Find the median. Like I showed in my previous example.