I have created a program that allows for a user-defined number of test scores as input. After the user enters these scores, the program calculates the average test score. Here is my code:
#include <iostream>
#include <iomanip>
usingnamespace std;
// Function prototypes
double getAverage(double*, int);
int main()
{
// Variable declarations
double *testScores; // Dynamically allocate an array of test scores
double average; // Hold the average test score
int numTests; // Number of test scores
int count; // Counter variable
// Get the number of tests
cout << "How many tests do you wish to process? ";
cin >> numTests;
// Verify input is not a negative number
while (numTests <= 0)
{
// Get input again.
cout << "Please enter a positive number: ";
cin >> numTests;
}
// Dynamically allocate an array large enough to hold the test scores
testScores = newdouble[numTests];
// Get the specified number of test scores
cout << "Enter the test scores below.\n";
for (count = 0; count < numTests; count++)
{
cout << "Test " << (count + 1) << ": ";
cin >> *(testScores + count);
// Verify input is not a negative number
while (*(testScores + count) < 0)
{
// Get input again.
cout << "Please enter a valid test score.\n";
cin >> *(testScores + count);
}
}
// Calculate the average test score
average = getAverage(testScores, numTests);
// Display the results.
cout << fixed << showpoint << setprecision(2);
cout << endl;
cout << "The average of those scores is: " << average << endl;
// Free dynamically allocated memory
delete [] testScores;
testScores = 0; // Make testScores point to null
return 0;
}
//function getAverage - calculates the average of the test scores
double getAverage(double* scores, int num)
{
double avg;
double total = 0.0;
for (int count = 0; count < num; count++)
{
total += scores[count];
}
avg = total / num;
return avg;
}
I am having trouble with the final part of my program. How do I pass the array of test scores to a function that calculates how many people got an A (90+) on the test? The final output should look like this:
The average of those scores is:
The number of A grades is: