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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <iomanip>
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.