I have the whole code written for what I want to do, but the only problem is, on my Daily_Average function, I want to print the average of the scores occurring Day 3, but I'm getting average of ALL scores together as one. Here is an example of the scores etc.

..... Day 1   Day 2    Day 3
Jimmy   70       68          71
Jason    72       75          68
Roy       68       69          67
Griff      65       64          59
John      71       81          74

I want to get the average of the scores under the Day 3 column, but how do I do it? I tried everything. I'm getting the sum of all the numbers, but unfortunately, that is the only way I know how to get the sum. How do I get sum for the scores in Day 3 and not the whole thing? This is for "Daily_Average" function. Also, I need to return that average. Thanks!

#include <iostream>
#include <iomanip>

using namespace std;

void Prt_Gradebook (string names[], int score[][3]);
double Daily_Average (int score[][3], int day);
int Lowest_score (string names[], int score[][3]);

int main ()
{

    string names[5] = {"Jimmy","Jason","Roy","Griff","John"};
    int score[5][3] = {{70,68,71},{72,75,68},{68,69,67},{65,64,59},{71,81,74}};
    int day;

    Prt_Gradebook(names,score);
    cout<<"The Average for Day 3 is "<<Daily_Average(score,day)<<endl;
    Lowest_score(names,score);

    cout<<endl;


    system ("PAUSE");
    return 0;
}

    void Prt_Gradebook (string names[], int score[][3])
{
     int sum = 0;

     for (int i = 0; i < 5; i++)
     {
         cout<<names[i]<<" ";

         for (int j = 0; j < 3; j++)
         {
         cout<<score[i][j]<<" ";
         sum+=score[i][j];
         }
         cout<<sum;
         cout<<endl;

     }


}


  double Daily_Average (int score[][3],int day)
{

  double average;
  int sum = 0;
  int j = day-1;

  for (int i = 0; i < 5; i++)
      {
           for (j = 0; j < 3; j++)
           {
           sum+=score[i][j];

           average = (sum / 5);
           }
      }   

          return average;


}



  int Lowest_score (string names[], int score[][3])
{  
   int lowest;
   int day;
   int j = day-1;

   for (int i = 0; i < 5; i++)
       {
            for (j = 0; j < 3; j++)

            if (score[i][j] < lowest)

            lowest = score[i][j];
       }

       cout<<"Langon's lowest score of "<<lowest<<" was shot on day "<<j<<endl;


} 
void Prt_Gradebook (string names[], int score[][3])
{

     
     for (int i = 0; i < 5; i++)
     {
         cout<<names[i]<<" ";
         int sum = 0;//Changed It to Here. 
         for (int j = 0; j < 3; j++)
         {
         cout<<score[i][j]<<" ";
         sum+=score[i][j];
         }
         cout<<sum/3;
         cout<<endl;

     }
}

How about this ?

I changed the place for sum into the for loop.

And divide the total by 3 in order to get the average which will be printed there.

In the "Daily_Average" function, you set the variable "j", like this:

int j = day-1;

but then in the loop, you do this:

for (j = 0; j < 3; j++)

thereby resetting the value of "j" back to zero.

Initialize a new variable at the top of the function, like this:

int k = day-1;

Then set your loop, like this:

for (j=k; j<3; j++)

And you should be all set.

Thanks for both of your replys.


I declared a new variable "k" and then setting k = day-1.

Now for the Daily_Average function, I returned the average, and wrote a cout statement inside of main for it to print to the screen, but I'm getting nasty numbers for the output.

The example code is suppose to be "cout<<Daily_Average(Arrayforthescores,3)<<endl;

For example:

I'm getting:

"The Average for Day 3 is 2.21112e-311"

It should actually be

"The Average for Day 3 is 67.8"

Did I return the average correctly? I'm trying to get the average for all the numbers that occur under the Day 3 column.

Reminder: The stats of the scores etc. is in my first post.

#include <iostream>
#include <iomanip>

using namespace std;

void Prt_Gradebook (string names[], int score[][3]);
double Daily_Average (int score[][3], int day);
int Lowest_score (string names[], int score[][3]);

int main ()
{
    
    string names[5] = {"Jimmy","Jason","Roy","Griff","Crimson"};
    int score[5][3] = {{70,68,71},{72,75,68},{68,69,67},{65,64,59},{71,81,74}};
    int day;
    double average;
    
    Prt_Gradebook(names,score);
    cout<<"The Average for Day 3 is "<<Daily_Average(score,day)<<endl;
    Lowest_score(names,score);
    

    cout<<endl;

    
    system ("PAUSE");
    return 0;
}

    void Prt_Gradebook (string names[], int score[][3])
{
     int sum = 0;
     
     for (int i = 0; i < 5; i++)
     {
         cout<<names[i]<<" ";
         
         for (int j = 0; j < 3; j++)
         {
         cout<<score[i][j]<<" ";
         sum+=score[i][j];
         }
         cout<<sum;
         cout<<endl;

     }
  

}


  double Daily_Average (int score[][3],int day)
{

  double average;
  int sum = 0;
  int k = day-1;
  
  for (int i = 0; i < 5; i++)
      {
           for (int j = k; j < 3; j++)
           {
           sum+=score[i][j];
           
           average = (sum / 5);
           }
      }   
           
          return average;
          
          
}



  int Lowest_score (string names[], int score[][3])
{  
   int lowest;
   int day;
   int j = day-1;
   
   for (int i = 0; i < 5; i++)
       {
            for (j = 0; j < 3; j++)
            
            if (score[i][j] < lowest)
            
            lowest = score[i][j];
       }
       
       cout<<"Crimson's lowest score of "<<lowest<<" was shot on day "<<j<<endl;
 
 
}

I dont understand Why You need the second loop in the average function.

double Daily_Average (int score[][3],int day)
{

  double average;
  int sum = 0;
  int k = day-1;
  
  for (int i = 0; i < 5; i++)
      {
    sum+=score[i][k];
     }      
           average = (sum / 5);
           
       
           
          return average;
          
          
}

And Secondly.
Another Mistake is in main ();

int day is not initialised to any number.

int day=3;

And now the code will work.

That's the fateful (and typical) mistake in the average value calculations:

double average;
  int sum = 0;
  ...
  average = (sum / 5); // *** integer division!

You try to assign the result of integer division to double value. You want to get exact average value. But 7/5 == 1, 9/5 == 1, 12/5 == 2 and so on. You lost a fractional part of calculated value. It does not matter what's a type of a target.

If you want to get an exact (more precisely, double type) result, make float division, for example:

average = sum / 5.0; // Type of 5.0 constant is double
commented: LEARNED something New ;) Thx +1

Thank you guys, it worked. The problem is now solved. =)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.