//Written by Mark Lair

//October 29, 2007

//Chapter 9

//Assignment 1,2,3  Average Test scores

// this program will calculate and display
// the average of an array of test scores
// with the lowest one dropped.

#include <iostream>
#include <iomanip>
using namespace std;


// Function Prototypes
int *allocateArray(int);
void selectionSort(int [], int);
void avgScores(int [], int);

int main()
{



//	double	average;	// To hold average of scores

	int		*scores,	// points to scores
			numTests,	// to hold the number of test scores
			total = 0,  // accumulator
			count;		// counter variable



	// Get number of test scores
	cout << "Enter the number of tests that you have taken. ";
	cin >> numTests;

	// get the array with enough memory to hold test scores
	scores = allocateArray(numTests);




	// Get test scores
	cout << "Enter your test scores below. ";
	
	for (count = 0; count < numTests; count++)
	{
		cout << " \n";
		cout << "Test# " << (count + 1) << ": ";
		cin >> scores[count];
		while (scores[count] < 0)
		{
			cout << "Error - no such thing as a negative test score \n";
			cout << "... please enter a positive number! \n";
			cout << "Test# " << (count + 1) << ": ";
			cin >> scores[count];
		}
	}


	

	//sort scores
	selectionSort(scores, numTests);

	//display test scores
	cout << "your scores from lowest to highest are :" << endl;
	for (count = 0; count < numTests; count++)
		cout << scores[count] << endl;
	
	//display highest test scores
	cout << "your scores without the lowest score are :" << endl;
	for (count = 1; count < numTests; count++)
		cout << scores[count] << endl;


	// display average of the highest scores
	
	avgScores(scores, numTests);
	



	system("pause");
	return 0;
}





    // allocate array function	
	int *allocateArray(int num)
	{
		int *array;    // to hold dynamic array of scores


		//Return null if num is zero or negative
		if (num <= 0)
			return NULL;

		// allocate array
		array = new int[num];


		return array;
	}





	//sort the array
	void selectionSort(int array[], int num)
	{
		int startScan, minIndex, minElem;


		for (startScan = 0; startScan < (num -1); startScan++)
		{
			minIndex = startScan;
			minElem = array[startScan];
			for (int index = startScan + 1; index < num; index++)
			{
				if (array[index] < minElem)
				{
					minElem = array[index];
					minIndex = index;
				}
			}
			array[minIndex] = array[startScan];
			array[startScan] = minElem;
		}
	}




	//calculate and display average of the scores
	void avgScores(int array[], int num)
	{
		int total = 0;
		double avg;

		for (int count = 1; count < num; count++)
			{
				total += array[count];
			}
		
		avg = total / (num - 1);

		cout << "Your average score is: " << avg << "!" << endl;

	}
















