//Devin Scheu CS1A Chapter 9, P. 538, #3
//
/**************************************************************
*
* PROCESS AND ANALYZE TEST SCORES WITH LOWEST DROPPED
* ____________________________________________________________
* This program determines the sorted list of test scores and
* their average, excluding the lowest score.
* ____________________________________________________________
* INPUT
* numScores : The number of test scores to process
* testScores : The test scores entered by the user
*
* OUTPUT
* sortedScores : The sorted list of test scores
* averageScore : The average of the test scores excluding the lowest
*
**************************************************************/
#include <iostream>
#include <iomanip>
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 perform bubble sort using pointers
void bubbleSort(int* arr, int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (*(arr + j) > *(arr + j + 1)) {
swap((arr + j), (arr + 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
int* testScores; //INPUT - The test scores entered by the user
double averageScore; //OUTPUT - The average of the test scores excluding the lowest
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 array
testScores = new int[numScores];
//Input Test Scores with EOF check
int validScores = 0;
for (int i = 0; i < numScores && cin >> *(testScores + i); i++) {
while (*(testScores + i) < 0) {
cout << "\nError: Please enter a non-negative score: ";
cin >> *(testScores + i);
}
cout << *(testScores + i) << endl;
validScores++;
}
//Adjust numScores to actual valid inputs if less than expected
if (validScores < numScores) {
numScores = validScores;
}
//Create copy for sorting
sortedScores = new int[numScores];
for (int i = 0; i < numScores; i++) {
*(sortedScores + i) = *(testScores + i);
}
//Sort Scores
bubbleSort(sortedScores, 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:" << right;
for (int i = 0; i < numScores; i++) {
cout << *(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[] testScores;
delete[] sortedScores;
} //end of main()