The question is to write a program that dynamically 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. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate heading. Use pointer notation rather than array notation whenever possible.
input validation do not accept negative test scores.
This is what i have written.
#include <iostream>
#include <iomanip>
using namespace std;
void arrSelectSort(float *[], int);
void showArrPtr(float *[], int);
void showAverage(float , int);
int main()
{
float *scores, //To dynamically allocate an array
total=0.0, //Accumulator
average; //To hold the averge scores
int numScores; //To hold the number of test scores
//Get the number of test scores.
cout << "How many test scores would you like to process?";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many
//test scores
scores = new float[numScores];
//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << ( count + 1 ) << ": ";
cin >> scores[count];
while (scores <=0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >>scores[count];
}
}
//Calculate the total scores
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}
//sort the elements of the array pointers
arrSelectSort ( scores, numScores );
//Will display them in sorted order.
cout << "The test scores in ascending order are: \n";
showArrPtr ( scores, numScores );
showAverage( total, numScores );
//Free memory.
delete [] scores;
scores = 0;
return 0;
}
void arrSelectSort(float *array[], int size)
{
int startScan, minIndex;
int *minElem;
for (startScan = 0; startScan < ( size - 1 ); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if ( *(array[index] ) < *minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
void showArrPtr(float *array, int size)
{
for (float count=0; count< size; count++)
cout << *(array[count]) << " ";
cout << endl;
}
void showAverage(float total, int numScores)
{
float average;
//Calculate the average
average = total / numScores;
//Display the results.
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
}
I have fixed most of the errors and i believe almost everything is correct. but there are a couple of errors that tell me that cannot convert parameter 1 from float to *float[] for arrselectsort and showarrptr and more errors in other areas for the similar things like float to int. I'm sure that you can figure out that im not very good at writing code, and this chapter of pointers for some reason its confusing the hell out of me. So any pointers as to how to fix my program is greatly appreciated.