How do I calculate the mean for this C++ program?

Define a getMean() function with the specification and prototype shown below:

// Return the sum of all scores divided by the number of scores.
// This know as the "mean" or average of the scores.
// Assume that numScores > 0.
double getMean(double scores[], int numScores);

Here is the driver (main()) used to test my function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include <iostream>
 #include <iomanip>

 using namespace std;
 const int MAX_SCORES = 10; // Maximum number of scores

 double getMean(double scores[], int numScores);

 int main() {
     double scores[MAX_SCORES];
     int scoreCount;
     cin>>scoreCount;
     scoreCount = min(MAX_SCORES, scoreCount);
     for (int i = 0; i < scoreCount; i++)
         cin >> scores[i];
     cout << fixed <<setprecision(2);
     cout <<getMean(scores, scoreCount) << endl;
     return 0;
 }


Here is my solution:

1
2
3
4
5
6
7
8
9
10
double getMean(double scores[], int numScores)
{
	int Sum;
	int Mean;
	for (int i = 0; i < numScores; i++)
 	{
		Sum += scores[i];
 	}
	Mean = Sum / numScores;
}


Here are the expected outputs I want to get:

Input: 1 1
Mean: 1

Input: 87 87 89
Mean: 87.67

Input: 70 70
Mean: 70.00

My program is supposed to calculate the mean of the scores, but it is not giving the correct output. What am I doing wrong? Did I do the calculation incorrectly?
You forgot to initialize sum and you do not return any value from the function. It will look the following way

1
2
3
4
5
6
7
8
9
10
11
double getMean( const double scores[], int numScores )
{
	double Sum = 0;

	for ( int i = 0; i < numScores; i++ )
 	{
		Sum += scores[i];
 	}

	return ( Sum / numScores );
}

Last edited on
I don't understand your correct output results. Maybe lack of neccessary information. Can you explain more, how to become the final results that you must have?
Topic archived. No new replies allowed.