// Andres Guzman CSC5 Chapter 9, P. 538, #3
/******************************************************************************
*
* REMOVE LOWEST SCORE
* ____________________________________________________________________________
* Program will take inputed values and dynamically allocate memory and sort
* values in ascending order and determine the average of the values without
* the lowest inputed value.
*
* Computation:
* Uses selection sort to ascend values from lowest to highest and then drops
* the lowest score and uses the formula "total / (b - 1)" to calculate the
* average amongst the remaining inputed values.
* ____________________________________________________________________________
*
* INPUT
*
* SIZE = Inputed Array Size
*
* OUTPUT
*
* test = Sorted Test Scores
* testnumber = Corresponding Test Numbers
* averagescore = Average Excluding Lowest Score
*
******************************************************************************/
#include <iostream>
using namespace std;
//
//Function Prototypes
void ascendingorder(int *a, int *b, int c); //Sorts numbers from low to high
int average(int *a, int b); //Calculates Average
int main()
{
int SIZE; //Array Size
cout << "Enter Amount of Test Scores: ";
cin >> SIZE;
while (SIZE <= 1) //Input Validation
{
cout << "Please Enter a Value Greater Than 1." << endl;
cout << "Enter Amount of Test Scores: ";
cin >> SIZE;
}
int *test = new int[SIZE]; //Dynamically Allocates Memory
int *testnumber = new int[SIZE]; //Dynamically Allocates Memory
//
//Allows input value for each subscript
for(int count = 0; count < SIZE; count++)
{
cout << "\nTest " << (count + 1) << ": ";
cin >> *(test + count);
while(*(test + count) < 0) //Input Validation
{
cout << "Please Enter Positive Value." << endl;
cout << "Test " << (count + 1) << ": ";
cin >> *(test + count);
}
*(testnumber + count) = count + 1; //Allows value to match subscript
}
cout << "\n\n"; //Spacing
//
//Function Call for sorting
ascendingorder(test, testnumber, SIZE);
//
//For loop to step through array subscript and output test scores accordingly
for(int count = 0; count < SIZE; count++)
{
cout << "Test " << *(testnumber + count)
<< ": " << *(test + count) << endl;
}
cout << "\n"; //Spacing
//
//Lowest Score Dropped
cout << "Lowest Score Dropped: " << *test << endl;
//
//Function call for average score calculation
int averagescore = average(test, SIZE);
//
//Output results
cout << "Average Score: " << averagescore << endl;
delete [] test; //Deletes new memory
delete [] testnumber; //Deletes new memory
return 0;
}
/*******************************************************************************
* Adding definition to function. The function will selection sort to sort the
* inputed values inside the subscript to structure them in ascending order.
******************************************************************************/
void ascendingorder(int *a, int *b, int c)
{
int startscan, minIndex; //Used to scan through the array
int minValue; //Holds lowest value found
int temp; //Temporary Position
//
//Initializing For Loop
for (startscan = 0; startscan < (c - 1); startscan++)
{
minIndex = startscan; //Assigns starting position as lowest
minValue = *(a + startscan); //Assigns starting number as lowest
//
//Searches for lowest value
for (int index = startscan + 1; index < c; index++)
{
if(*(a + index) < minValue)
{
minValue = *(a + index); //Updates lowest number value
minIndex = index; //stores position of lowest value
}
}
//
//Swaps Values
*(a + minIndex) = *(a + startscan);
*(a + startscan) = minValue;
temp = *(b + minIndex);
*(b + minIndex) = *(b + startscan);
*(b + startscan) = temp;
}
}
/*******************************************************************************
* Adding definition to function. The function will accumulate the total scores
* and calculate the average of the input values and return the value while
* excluding the lowest score.
******************************************************************************/
int average(int *a, int b)
{
int total = 0; //Accumulator
int average; //Holder for formula
//
//For loop to step through subscript
for(int count = 1; count < b; count++)
{
total += *(a + count); //Adds Value to accumulator
}
average = total / (b - 1); //Calculates Average
return average; //Returns Value
}