//Ryan Shahriyarpour   CS1A  Chapter 9   Question 2
/*****************************************************************************
 *
 * Sort and Average Test Scores
 *
 *********************
 *
 * This program lets the user enter a number of test scores, rejecting any
 * negative scores. It then displays the scores sorted from lowest to
 * highest, along with the average score.
 *
 * INPUT
 *  numScores  - how many scores will be entered
 *  scores     - each test score (must not be negative)
 *
 * OUTPUT
 *  The sorted list of scores and the average score
 *
 ****************************************************************************/

#include <iostream>
#include <iomanip>
using namespace std;

// Function prototypes
void sortScores(double *array, int size);
double calcAverage(const double *array, int size);

int main()
{
    // DATA DICTIONARY
    // numScores  - number of test scores the user wants to enter
    // scores     - pointer to the dynamically allocated array of scores
    // average    - the average of all the scores
    // count      - loop counter
    // startScan  - position being filled on each pass of the sort
    // index      - loop counter used to scan for the smallest value
    // minIndex   - index of the smallest value found
    // minValue   - the smallest value found
    // total      - running total of the scores
    int numScores;
    double *scores;
    double average;
    int count;

    // Get the number of scores
    cout << "How many test scores do you want to enter? ";
    cin >> numScores;
    while (numScores < 1)
    {
        cout << "Please enter at least 1: ";
        cin >> numScores;
    }

    // Allocate the array
    scores = new double[numScores];

    // Get each score and reject negatives
    for (count = 0; count < numScores; count++)
    {
        cout << "Enter test score " << (count + 1) << ": ";
        cin >> *(scores + count);
        while (*(scores + count) < 0)
        {
            cout << "Scores cannot be negative. Try again: ";
            cin >> *(scores + count);
        }
    }

    // Sort the scores and calculate the average
    sortScores(scores, numScores);
    average = calcAverage(scores, numScores);

    // Display the results
    cout << fixed << setprecision(2);
    cout << "\nSorted Scores (low to high)" << endl;
    cout << "---------------------------" << endl;
    for (count = 0; count < numScores; count++)
        cout << *(scores + count) << endl;
    cout << "\nAverage Score: " << average << endl;

    // Free the memory
    delete [] scores;
    scores = nullptr;

    return 0;
}

// Sorts the array into ascending order using a selection sort
void sortScores(double *array, int size)
{
    int startScan, index, minIndex;
    double minValue;

    for (startScan = 0; startScan < size - 1; startScan++)
    {
        minIndex = startScan;
        minValue = *(array + startScan);
        for (index = startScan + 1; index < size; index++)
        {
            if (*(array + index) < minValue)
            {
                minValue = *(array + index);
                minIndex = index;
            }
        }
        *(array + minIndex) = *(array + startScan);
        *(array + startScan) = minValue;
    }
}

// Returns the average of the values in the array
double calcAverage(const double *array, int size)
{
    int count;
    double total = 0.0;

    for (count = 0; count < size; count++)
        total += *(array + count);

    return total / size;
}