Test results program

closed account (3UMLy60M)
Hi all,

In a program that allows a user to enter a specified number of test scores, how would I create a function that calculates how many of the scores were above 90?

This is my code so far:

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
#include <iostream>
#include <iomanip>
using namespace 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 = new double[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;
}
Last edited on
The same way as you created function getAverage.
closed account (3UMLy60M)
I'm so confused. I don't know how to create the function.
@codenewb54
I'm so confused. I don't know how to create the function.


This means only that your statement that "This is my code so far" is a lie.
Last edited on
Topic archived. No new replies allowed.