Modify the program of Programming Challenge 1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. Modify both the sorting
and average-calculating functions so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers rather than array indices.
I have a the first part written. Struggled with the second part and just gave up.
//Test Scores #1
#include
#include
using namespace std;
//Prototypes
void arrSelectSort(float *, int);
void showArrPtr(float *, int);
void showAverage(float, int);
int main()
{
float *scores,
total=0.0,
average;
int numScores;
//Number of test scores.
cout << "How many test scores would you like to enter?";
cin >> numScores;
while (numScores <= 0)
{
cout << "Zero or negative numbers not accepted.n";
cin >> numScores;
}
scores = new float[numScores];
//Gather test scores
cout << "Enter the test scores below.n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << ( count + 1 ) << ": ";
cin >> scores[count];
while (scores[count] <= 0)
{
cout << "Zero or negative numbers not accepted.n";
cout << "Test Score #" << (count + 1) << ": ";
cin >>scores[count];
}
}
//Calculate the total
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}
//sort & display elements of the array pointers
arrSelectSort ( scores, numScores );
cout << "The test scores in ascending order are: n";
showArrPtr ( scores, numScores );
showAverage( total, numScores );
delete [] scores;
return 0;
}
//Functions
void arrSelectSort(float *array, int size)
{
int scan, minIndex;
float minElem;
for (scan = 0; scan < ( size - 1 ); scan++)
{
minIndex = scan;
minElem = array[scan];
for (int index = scan + 1; index < size; index++)
{
if ( array[index] < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[scan];
array[scan] = minElem;
}
}
void showArrPtr(float *array, int size)
{
for (int count=0; count< size; count++)
cout << array[count] << " ";
cout << endl;
}
void showAverage(float total, int numScores)
{
float average;
average = total / numScores;
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
}