//Andrew Alspaugh CS1A Chapter 9. P. 537. #2
/****************************************************************************
Process User Input Scores
____________________________________________________________________________
This program lets the user input any amount of scores; while the scores are
being input, the program creates a new array everytime to ensure the size
of the array is exactly right for the amount of inputs. Once the user inputs
the test scores they use the sentinal value -1 to end putting in inputs.
After the test scores are input, the program then uses a function SortScores
to sort the scores in ascending order. Then the program calls GetAverage function
to calculate the average of all of the scores
The program dislplays the total number of scores, the scores in ascending order,
and the average of all scores.
_____________________________________________________________________________
//input
int score = 0; //input score
int sizeAccumulator = 1; //size of array
int count = 0;
//output
int *testScores; //pointer to beginning of testScores Array
testScores = new int[sizeAccumulator]; //Pointer Definition
//SORTSCORES FUNCTION
bool swap;
int temp;
//GETAVERAGE FUNCTION
int total = 0;
float Average; //Average
*****************************************************************************/
#include <iostream>
using namespace std;
void SortScores(int *array, int size);
float GetAverage(int *array, int size);
int main()
{
//Data Dictionary
//input
int score = 0; //input score
int sizeAccumulator = 1; //size of array
int count = 0;
//output
int *testScores; //pointer to beginning of testScores Array
testScores = new int[sizeAccumulator]; //Pointer Definition
//SORTSCORES FUNCTION
bool swap;
int temp;
//GETAVERAGE FUNCTION
int total = 0;
float Average; //Average
//INPUT
cout << "Enter Test Scores: Enter -1 to quit: " << endl;
while(score != -1)
{
cin >> score;
if (score != -1)
{
if (count == sizeAccumulator)
{
sizeAccumulator += 1;
int* newTestScores = new int [sizeAccumulator];
for(int i = 0; i < count; i++)
{
newTestScores[i] = testScores[i];
}
delete[] testScores;
testScores = newTestScores;
}
testScores[count] = score;
count++;
}
}
//Process
SortScores(testScores, count);
Average = GetAverage(testScores, count);
//Output
cout << "There are " << sizeAccumulator << " scores" << endl;
for(int i = 0; i < sizeAccumulator; i++ )
{
cout << "Score " << (i+1) << " is: " << *(testScores + i) << endl;
}
cout << "Average is: " << Average << endl;
delete [] testScores;
return 0;
}
//Sort the scores using a bubble sort
void SortScores (int *array, int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array [count + 1];
array [count + 1] = temp;
swap = true;
}
}
}
while (swap);
}
// Find the Average function
float GetAverage(int *array, int size)
{
int total = 0;
for(int i = 0; i < size; i++)
total += array[i];
return static_cast<double>(total)/size;
}