This is my full program as it is now...the last function is to remove a student's lowest testscore and then calculate an average, I am thinking that I should compare the testscores to find the lowest but hasn't the selection sort function done that? I even tried returning the "lowest" variable in order to remove it but it didn't work then either...
Code:
/***This program dynamically allocates an array large enough to
hold a user-defined number of test scores***/
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
int getnumtestscores();
void gettestscores(int[], int);
void showArray(int [], int);
void sort(int [], int);
double average(int[], int);
int main()
{
int numts = getnumtestscores();
int *testscores = new int[numts]; //To dynamically allocate an array
gettestscores(testscores, numts);
cout << "The testscores are: \n"; showArray (testscores, numts);
cout << "The testscores sorted are: \n"; sort (testscores, numts);
showArray (testscores, numts);
cout << "The average of all " << numts << " test scores is: "
<< fixed << showpoint << setprecision(2)
<< average(testscores, numts);
cout << endl;
// Free dynamically allocated array in memory
delete [] testscores;
// Make scores point to null
testscores = 0;
return 0;
}
int getnumtestscores()
{
int num;
cout << "\n How many test scores do you want to enter : ";
for(;;) // loop forever ... until return
{
cin >> num;
return num;
}
}
void gettestscores(int testscores[], int numts)
{
cout << "What are the testscores for the students?\n";
for (int count = 0; count < numts; count++)
{
cout << "Testscore " << setw(2) << (count+1) << ":";
if (testscores < 0) //Input validation
{
cout << "An invalid score was entered, please enter a score more than 0\n";
}
cin >> testscores[count];
}
}
void sort(int array[], int numts)
{
int lowIndex, lowest;
for (int count = 0; count < (numts-1); count++)
{
lowIndex = count;
lowest = array[count];
for (int index = count + 1; index < numts; index++)
{
if (array[index] < lowest)
{
lowest = array[index];
lowIndex = index;
}
}
array[lowIndex] = array[count];
array[count] = lowest;
}
return;
}
void showArray(int array[], int numts)
{
for (int count = 0; count < numts; count++)
cout << array[count] <<" \n";
cout << endl;
}
double average(int array[], int numts)
{
int total = 0;
int lowest = 0;
double avg;
for (int count = 0; count < numts; count++)
{
total +=array[count];
}
avg = (total - lowest) / (numts-1);
return avg;
}