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:

 #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:

 double getMean(double scores[], int numScores)
 {
      int sum;
      int j;
      for (j = 0; j > scores[numScores]; j++)
      {
          sum = (scores[j+numScores]) / numScores;
      }
      return(scores[sum]);
 }

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?

add all of the scores using a loop with a condition that it must not exceed the number of scores then divide the total value by the number of scores

it should look something similar to this

    for (int i = 0; i < TotalNumberofScores; i++){
            Sum += Scores[i];
    }
    Mean = Sum / TotalNumberofScores;

Edited 4 Years Ago by zeroliken

Okay, I modified my code, but now it is giving a run-time error. What did I do wrong?

Here is my solution:

double getMean(double scores[], int numScores)
{
    int Sum = 0;
    int total;
    int Mean;
    for (int i = 0; i < total; i++)
    {
        Sum += scores[i];
    }
    Mean = Sum / total;
    return Mean;
}

The for loop needs braces too.

 #include <iostream>
 #include <iomanip> 
 using namespace std;
 const int MAX_SCORES = 10; //Maximum number of scores
 double getMean(double score[], 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;
 }
//double getMean( double scores[], int numScores )
double getMean( const double scores[], int numScores ) // **** const
{ 
    /*
    // You may want to ignore this bit for now.
    using limits = std::numeric_limits<double> ;
    if( numScores < 1 )  
        return limits::has_quiet_NaN ? limits::quiet_NaN() : limits::signaling_NaN() ;
    */

    // int Sum = 0; 
    double Sum = 0.0 ; // **** Sum is of type double

    // ...
This article has been dead for over six months. Start a new discussion instead.