Question: Please please please, this is the third time I am submitting this and getting wrong answers. Pease correct my code. There an exception right at
Please please please, this is the third time I am submitting this and getting wrong answers. Pease correct my code. There an exception right at ' free memory section'.
/* 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 headings. Use pointer notation rather than array notation whenever possible. Input Validation: Do not accept negative numbers for test scores.
*/
#include
// function prototype double getAverage(double, int); void selectionSort(double[], int);
int main() { int numOFTests; // number of test scores double total = 0.0; // Accumilator initialized with 0 double average; double *Test_Scores;
cout << "Enter the number of test scores you will enter: "; cin >> numOFTests;
//Allocates an array large enough to hold a user defined number of test scores Test_Scores = new double[numOFTests];
for (int count = 1; count <= numOFTests; count++) { cout << " Enter Score " << count << endl; cin >> *(Test_Scores+count); total += *(Test_Scores+count); while (*(Test_Scores+count) < 0) { cout << "The value you have entered is invalid."; cout << "please re-enter the test score."; cin >> *(Test_Scores + count); } }
// Display the result cout << fixed << showpoint << setprecision(2); cout << "The table of Scores " << endl; cout << "---------------------" << endl;
selectionSort(Test_Scores, numOFTests); average = getAverage(total, numOFTests); cout << "The average test scores is " << average << endl; cout << "The total test scores is " << total << endl; // Free the memory delete[] Test_Scores; Test_Scores = nullptr; return 0; }
void selectionSort(double array[], int size) { int minIndex; double minValue;
for (int start = 0; start < (size - 1); start++) { minIndex = start; minValue = array[start]; for (int index = start + 1; index < size; index++) { if (array[index] < minValue) { minIndex = index; } } minValue = array[start]; array[start] = array[minIndex]; array[minIndex] = minValue; } cout << "Sorted Array:"; for (int count = 0; count < size; count++) { cout << array[count] << " "; cout << endl; }
} double getAverage(double total, int numOFTests) { return total / numOFTests; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
