In: Computer Science
In C++
2. Test Scores #2
Modify the program #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. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name or *(myArray).name) Make it a class. You will need a destructor because you are using dynamic arrays.
Solution:
Here is the code updated for you:
#include <iostream>
#include <iomanip>
using namespace std;
typedef struct
{
string name;
double testScore;
}Records;
//Function prototypes
void arrSelectSort(Records *, int);
double arrAvgScore(Records *, int);
//Allow the user to enter name-score pairs.
int main()
{
//Define variables
Records *TestScores;
double total = 0.0, average;
int numTest, count;
//Get the number of test scores you wish to average and put in
order
cout << "How many test scores do you wish to enter?";
cin >> numTest;
//Dynamically allocate an array large enough to hold that many
scores
TestScores = new Records[numTest];
//Get the test scores
cout << "Enter the test scores below.\n";
for (count = 0; count < numTest; count++)
{
//Display score
cout << "Name " << (count + 1) << ": ";
cin >> (TestScores + count)->name;
cout << "Test Score " << (count + 1) << ":
";
cin >> TestScores[count].testScore;
// Input validation. Only numbers between 0-100
while ((TestScores + count)->testScore<0 || (TestScores +
count)->testScore>99)
{
cout << "You must enter a scores that non-negative" <<
endl;
cout << "Please enter again: ";
cin >> TestScores[count].testScore;
}
}
//Dsiplay the results
arrSelectSort(TestScores, numTest);
average = arrAvgScore(TestScores, numTest);
cout << fixed << showpoint <<
setprecision(2);
cout << "The test scores, sorted in ascending order, are:
\n";
for (count = 0; count < numTest; count++)
cout << (TestScores + count)->name << " " <<
(TestScores + count)->testScore << endl;
cout << endl;
cout << "The average of all the test score is " <<
average << endl;
//Free dynamically allocated memory
delete [] TestScores;
TestScores = 0; //make TestScores point to null
//Display the Test Scores in ascending order
//system ("pause");
return 0;
}
//Ascending order selection sort
void arrSelectSort(Records *arr, int size)
{
int startScan;
int minIndex;
double minElem;
for(startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = (arr + startScan)->testScore;
for(int index = startScan; index < size; index++)
{
if ((arr + index)->testScore < minElem)
{
minElem = (arr + index)->testScore;
minIndex = index;
}
}
if(minIndex!=startScan)
{
Records temp = arr[minIndex];
arr[minIndex] = arr[startScan];
arr[startScan] = temp;
}
}
}
double arrAvgScore (Records *arr, int size)
{
double total = 0,average;
int numTest;
for (int count = 0; count < size; count++)
{
total += (arr + count)->testScore;
}
average = total / size;
return average;
}
#please consider my effort and give me a like...thank u....