//Cameron Pham      		    CS1A                       Chapter 9, p. 537, #2
//
/******************************************************************************
* SORT AND ANALYZE TEST SCORES
* ______________________________________________________________________________
* This program dynamically allocates an array to store a user-defined number of 
* test scores. It accepts an integer value for every test score. The program 
* then sorts the scores, calculates their average, and displays the sorted 
* scores along with the average. 
* 
* INPUT
* Number of scores			// Total number of test scores
* Scores					// Every individual test score
* 
* OUTPUT
* Average					// Average of test scores
*******************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;

// Function prototypes
void sortScores(int* scores, int size);
double calculateAverage(int* scores, int size);

int main()
{
    // Initialize variables
    int numScores;       // Number of test scores
    int* scores;         // Pointer to dynamically allocated array
    double average;      // Average test score
    // Ask for the number of test scores
    cout << "Enter the number of test scores: ";
    cin >> numScores;
    // Validate the number of scores
    while (numScores < 1)
    {
        cout << "Please enter at least 1 test score: ";
        cin >> numScores;
    }
    // Dynamically allocate the array
    scores = new int[numScores];
    // Get each test score
    for (int count = 0; count < numScores; count++)
    {
        cout << "Enter test score " << count + 1 << ": ";
        cin >> *(scores + count);
        // Validate the test score
        while (*(scores + count) < 0)
        {
            cout << "Test scores cannot be negative. Enter again: ";
            cin >> *(scores + count);
        }
    }
    // Sort the test scores
    sortScores(scores, numScores);
    // Calculate the average test score
    average = calculateAverage(scores, numScores);
    // Display the sorted test scores
    cout << endl << "Sorted test scores:" << endl;
    for (int count = 0; count < numScores; count++)
    {
        cout << *(scores + count) << endl;
    }
    // Display the average
    cout << fixed << setprecision(2);
    cout << "Average test score: " << average << endl;
    // Release dynamically allocated memory
    delete[] scores;
    scores = nullptr;
    return 0;
}
// Sort test scores in ascending order using bubble sort
void sortScores(int* scores, int size)
{
    int temp;
    for (int pass = 0; pass < size - 1; pass++)
    {
        for (int count = 0; count < size - 1 - pass; count++)
        {
            if (*(scores + count) > *(scores + count + 1))
            {
                temp = *(scores + count);
                *(scores + count) = *(scores + count + 1);
                *(scores + count + 1) = temp;
            }
        }
    }
}
// Calculate and return the average test score
double calculateAverage(int* scores, int size)
{
    int total = 0;
    for (int count = 0; count < size; count++)
    {
        total += *(scores + count);
    }
    return static_cast<double>(total) / size;
}