// Chapter 9, Programming Challenge 2: Test Scores #1
//Gerardo Martinez
//Computer Science
//11/25/14
//***************************************************************
#include <iostream>
#include <iomanip>
usingnamespace std;
// Function prototypes
void sort(double*, int);
double average(double*, int);
int main()
{
int numTestScores; // The number of test scores
double *testScorePtr; // To point to an array
double testAverage; // The test score average
// Get the number of test scores.
cout << "\nHow many test scores will you enter? ";
cin >> numTestScores;
// Validate the input.
while (numTestScores < 0)
{
cout << "The umber cannot be negative.\n";
cout << "Enter another number: ";
cin >> numTestScores;
}
// Allocate an array to hold the test scores.
testScorePtr = newdouble[numTestScores];
// ******
// ADD CODE HERE TO FILL IN THE ARRAY WITH TEST SCORES
// DO NOT ALLOW SCORES LESS THAN 0
// *******
for (int count = 0; count < numTestScores; count++)
{
cout << "\nEnter The test score for test # " << (count + 1) << ": ";
cin >> testScorePtr[numTestScores];
while (testScorePtr[numTestScores] < 0)
{
cout << "The umber cannot be negative.\n";
cout << "Enter another number: ";
cin >> numTestScores;
}
}
// Sort the test scores.
sort(testScorePtr, numTestScores);
// Get the average of the test scores.
testAverage = average(testScorePtr, numTestScores );
//*** ADD CODE HERE TO OUTPUT THE SORTED SCORES
// AND THE AVERAGE
// ***
cout << "The average of all tests without the lowest test drop: " << testAverage ;
// Free the dynamically-allocated memory.
delete [] testScorePtr;
testScorePtr = 0;
return 0;
}
// ********************************************************
// The sort function performs a selection sort on the *
// array pointed to by the score parameter into ascending *
// order. The size parameter holds the number of elements.*
// Selection sort is described in Chapter 8 *
// ********************************************************
void sort(double* score, int size)
{
int startScan, minIndex;
double minValue;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minValue = *(score+startScan);
for(int index = startScan + 1; index < size; index++)
{
if (*(score+index) < minValue)
{
minValue = *(score+index);
minIndex = index;
}
}
*(score+minIndex) = *(score+startScan);
*(score+startScan) = minValue;
}
}
// ********************************************************
// Write the average function to calculate and return the *
// average of the values stored in the array passed in as *
// a parameter. The number of elements in the array is *
// the second parameter *
// ********************************************************
double average(double *score, int size)
{
double total = 0.00;
int totalAverage;
cout << fixed << showpoint << setprecision(2);
for (int count = 0; count < size; count++)
{
total += *score;
score++;
totalAverage = total / size;
}
return totalAverage;
}