//Devin Scheu CS1A Chapter 9, P. 538, #4
//
/**************************************************************
*
* PROCESS AND ANALYZE TEST SCORES WITH NAMES
* ____________________________________________________________
* This program determines the sorted list of test scores with
* corresponding student names and their average, excluding the
* lowest score.
* ____________________________________________________________
* INPUT
* numScores : The number of test scores to process
* studentNames : The names of the students
* testScores : The test scores entered by the user
*
* OUTPUT
* sortedNames : The sorted list of student names
* sortedScores : The sorted list of test scores
* averageScore : The average of the test scores excluding the lowest
*
**************************************************************/
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Function to swap two integers using pointers
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
// Function to swap two strings using pointers
void swap(string* a, string* b) {
string temp = *a;
*a = *b;
*b = temp;
}
// Function to perform bubble sort on scores and names using pointers
void bubbleSort(int* scores, string* names, int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (*(scores + j) > *(scores + j + 1)) {
swap((scores + j), (scores + j + 1));
swap((names + j), (names + j + 1));
}
}
}
}
// Function to calculate average excluding lowest score using pointers
double calculateAverageExcludingLowest(int* arr, int size) {
if (size <= 0) return 0.0;
double sum = 0;
int* minPtr = arr;
for (int i = 0; i < size; i++) {
if (*(arr + i) < *minPtr) {
minPtr = arr + i;
}
sum += *(arr + i);
}
return (sum - *minPtr) / (size - 1);
}
int main () {
//Variable Declarations
int numScores; //INPUT - The number of test scores to process
string* studentNames; //INPUT - The names of the students
int* testScores; //INPUT - The test scores entered by the user
double averageScore; //OUTPUT - The average of the test scores excluding the lowest
string* sortedNames; //OUTPUT - The sorted list of student names
int* sortedScores; //OUTPUT - The sorted list of test scores
//Prompt for Input
cout << "Enter the number of test scores: ";
cin >> numScores;
cout << numScores << endl;
//Allocate dynamic arrays
studentNames = new string[numScores];
testScores = new int[numScores];
//Input Test Scores and Names with EOF check
int validScores = 0;
for (int i = 0; i < numScores && cin >> *(studentNames + i) >> *(testScores + i); i++) {
while (*(testScores + i) < 0) {
cout << "\nError: Please enter a non-negative score for " << *(studentNames + i) << ": ";
cin >> *(testScores + i);
}
cout << *(studentNames + i) << " " << *(testScores + i) << endl;
validScores++;
}
//Adjust numScores to actual valid inputs if less than expected
if (validScores < numScores) {
numScores = validScores;
}
//Create copies for sorting
sortedNames = new string[numScores];
sortedScores = new int[numScores];
for (int i = 0; i < numScores; i++) {
*(sortedNames + i) = *(studentNames + i);
*(sortedScores + i) = *(testScores + i);
}
//Sort Scores and Names
bubbleSort(sortedScores, sortedNames, numScores);
//Calculate Average Excluding Lowest
averageScore = calculateAverageExcludingLowest(testScores, numScores);
//Separator and Output Section
cout << "-------------------------------------------------------" << endl;
cout << "OUTPUT:" << endl;
//Output Result
cout << fixed << setprecision(2);
cout << left << setw(25) << "Sorted Scores with Names:" << right;
for (int i = 0; i < numScores; i++) {
cout << *(sortedNames + i) << " (" << *(sortedScores + i) << ")";
if (i < numScores - 1) cout << ", ";
}
cout << endl;
cout << left << setw(25) << "Average Score (Lowest Dropped):" << right << setw(15) << averageScore << endl;
//Clean up memory
delete[] studentNames;
delete[] testScores;
delete[] sortedNames;
delete[] sortedScores;
} //end of main()