//Cecilia Rangel CSC5 Chapter 9, P.537, #2
//
/**************************************************************
*
* COMPUTE TEST SCORES
* ____________________________________________________________
* This program dynammically allocates an array large enough
* to hold a user defined number of test scores. Once all the
* scores are entered, the array should be passed to a function
* that sorts them in ascending order. The program should
* display the sorted list of scores and averages with
* appropriate headings.
*_____________________________________________________________
* INPUT
* SIZE : INPUT the ammount of numbers to allocate
* SCORES : INPUT the test scores to be sorted
*
* OUTPUT
* SORTED : OUTPUT the scores sorted from
* heighest to lowest
* AVERAGE : OUTPUT the average of all the scores
* ***************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void sortElements(int, int*);
int calcAvg(int, int*);
int main()
{
int SIZE; //Size of memory allocated
int* ptr; //pointer for memory allocated
ptr = new int[SIZE]; //allocate enough memory for SIZE
int avg; //OUTPUT - average of test scores
//
//User defines size of memory allocated
cout << "Enter the number of test scores to enter.\n";
cin >> SIZE;
//
//acquire test scores
for (int count = 0; count < SIZE; count ++)
{
cout << "Enter test score #" << count + 1 << endl;
cin >> *(ptr + count);
while (*(ptr + count) < 0)
{
cout << "Enter a valid test score.\n";
cin >> *(ptr + count);
}
}
//
//call to functions
avg = calcAvg(SIZE, ptr);
sortElements(SIZE, ptr);
//
//output test scores and average
cout << "Test Scores from lowest grade to highest grade.\n";
for (int count = 0; count < SIZE; count ++)
{
cout << *(ptr + count) << endl;
}
cout << "Test Score average: ";
cout << avg << endl;
return 0;
}
void sortElements(int SIZE, int* ptr)
{
int startSearch;
int lowScore;
int lowElement;
for (startSearch = 0; startSearch < SIZE; startSearch++)
{
lowScore = *(ptr + startSearch);
lowElement = startSearch;
for (int count = startSearch + 1; count < SIZE; count++)
{
if (lowScore > *(ptr + count))
{
lowScore = *(ptr + count);
lowElement = count;
}
}
*(ptr + lowElement) = *(ptr + startSearch);
*(ptr + startSearch) = lowScore;
}
}
int calcAvg(int SIZE, int* ptr)
{
int total = 0;
int avg = 0;
for (int count = 0; count < SIZE; count++)
{
total += *(ptr + count);
}
avg = total / SIZE;
return avg;
}