### Dynamically allocated arrays

closed account (3UMLy60M)
I have a program that allows the user to enter a specific number of test scores. Based on these test scores, the program should display the average score and calculate how many of the scores were above 90. I have most of it done but I'm having trouble figuring out how to calculate the number of scores that were above 90. What am I doing wrong?

This is my code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100`` ``````#include #include using namespace std; // Function prototypes double average(double*, int); int howManyA(double*, int); int main() { // Variable declarations double *score; // Dynamically allocate an array of test scores double scoreAverage = 0; // Hold the average test score int numOfA; int numScores; // Number of test scores int count; // Counter variable // Get the number of tests cout << "How many tests do you wish to process? "; cin >> numScores; // Verify input is not a negative number while (numScores <= 0) { // Get input again. cout << "Please enter a positive number: "; cin >> numScores; } // Dynamically allocate an array large enough to hold the test scores score = new double[numScores]; // Get the specified number of test scores cout << "Enter the test scores below.\n"; for (count = 0; count < numScores; count++) { cout << "Test " << (count + 1) << ": "; cin >> *(score + count); // Verify input is not a negative number while (*(score + count) < 0) { // Get input again. cout << "Please enter a valid test score.\n"; cin >> *(score + count); } } // Calculate the average test score scoreAverage = average(score, numScores); // Calculate the number of A grades numOfA = howManyA(score, numScores); // Display the results. cout << fixed << showpoint << setprecision(2); cout << endl; cout << "The average of those scores is: " << scoreAverage << endl; cout << "The number of A grades is: " << numOfA << endl; // Free dynamically allocated memory delete [] score; score = 0; // Make testScores point to null return 0; } //************************************************** // Definition of function average. * // This calculates the average of the test * // scores that were inputted by the user. * //************************************************** double average(double* score, int numScores) { double averageOfScores; double total = 0.0; for (int count = 0; count < numScores; count++) { total += score[count]; } averageOfScores = total / numScores; return averageOfScores; } //************************************************** // Definition of function howManyA. * // This calculates the number of scores that * // were above 90. * //************************************************** int howManyA(double* score, int numScores) { int A = 0; for (int i = 0; i >= numScores; i++) { if (score[i] >= 90) { A++; } } return A; }``````
`for (int i = 0; i >= numScores; i++)`

vs

`for (int i = 0; i < numScores; i++)`
Topic archived. No new replies allowed.