//Zachary Abdollahi CS1A Chapter 9, P. 538, #4
//
/*******************************************************************************
*
* SORT STUDENT TEST SCORES
* _____________________________________________________________________________
* This program dynamically allocates parallel arrays to store student
* names and their corresponding integer test scores, sorts both arrays in
* ascending order by score using pointers, and computes the class average.
*
* Computation is based on the formula:
* Average = Sum of All Scores / Number of Students
* _____________________________________________________________________________
* INPUT
* numStudents : Number of students to process
* names : Dynamically allocated array of student names
* scores : Dynamically allocated array of integer test scores
*
* OUTPUT
* names : Student names ordered by ascending test score
* scores : Student test scores sorted in ascending order
* average : Average of all student test scores
*
******************************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Function Prototypes
void sortStudentData(string *, int *, int);
double calcAverage(const int *, int);
int main()
{
int numStudents; //INPUT - Number of students to process
string *names; //INPUT - Pointer to dynamic array of names
int *scores; //INPUT - Pointer to dynamic array of scores
double average; //OUTPUT - Average of all test scores
// Prompt User for Number of Students
cout << "How many students took the test? ";
cin >> numStudents;
// Validate Positive Number of Students
while (numStudents <= 0)
{
cout << "Please enter a number greater than 0: ";
cin >> numStudents;
}
// Dynamically Allocate Parallel Arrays
names = new string[numStudents];
scores = new int[numStudents];
// Read Student Name-Score Pairs Using Pointers
for (int count = 0; count < numStudents; count++)
{
cout << "Enter student #" << (count + 1) << "'s name: ";
cin.ignore();
getline(cin, *(names + count));
cout << "Enter " << *(names + count) << "'s score: ";
cin >> *(scores + count);
// Validate Non-Negative Score
while (*(scores + count) < 0)
{
cout << "Negative scores are not allowed. Enter again: ";
cin >> *(scores + count);
}
}
// Sort Parallel Arrays in Ascending Order by Score
sortStudentData(names, scores, numStudents);
// Compute Class Average
average = calcAverage(scores, numStudents);
// Output Sorted Student Roster and Average
cout << "\nSorted Student Scores:\n";
cout << "-----------------------------\n";
for (int count = 0; count < numStudents; count++)
{
cout << left << setw(20) << *(names + count)
<< right << setw(5) << *(scores + count) << endl;
}
cout << fixed << showpoint << setprecision(2);
cout << "\nClass Average: " << average << endl;
// Free Dynamically Allocated Memory
delete [] names;
delete [] scores;
names = nullptr;
scores = nullptr;
return 0;
}
/*******************************************************************************
* Function: sortStudentData
* Performs an ascending selection sort on the scores array while keeping
* the parallel names array synchronized using pointer notation.
******************************************************************************/
void sortStudentData(string *namesArr, int *scoresArr, int size)
{
int startScan, minIndex, minScore;
string minName;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minScore = *(scoresArr + startScan);
minName = *(namesArr + startScan);
for (int index = startScan + 1; index < size; index++)
{
if (*(scoresArr + index) < minScore)
{
minScore = *(scoresArr + index);
minName = *(namesArr + index);
minIndex = index;
}
}
*(scoresArr + minIndex) = *(scoresArr + startScan);
*(scoresArr + startScan) = minScore;
*(namesArr + minIndex) = *(namesArr + startScan);
*(namesArr + startScan) = minName;
}
}
/*******************************************************************************
* Function: calcAverage
* Computes the floating-point average of an integer array using pointer
* notation.
******************************************************************************/
double calcAverage(const int *arr, int size)
{
int total = 0;
for (int count = 0; count < size; count++)
{
total += *(arr + count);
}
return static_cast<double>(total) / size;
}