I need help with my output. I have pretty much everything done. I'm just getting an output like:

"The class average is 5.8%"
"The class average is 12.4%"
"The class average is 13.6%"
"The class average is 18.4%"
etc.

"Highest grade is 58%"
"Highest grade is 66%"
"Highest grade is 85%"
"Highest grade is 96%"
etc.

"0 out of 10 passed the test"
"2 out of 10 passed the test"
"4 out of 10 passed the test"
etc.

I want to end with the last class average (ex: 18.4%), the last highest grade (ex: 96%) and the greatest number of students who passed the text (ex: 4 out of 10 passed the test).

One other thing I need help with....I believe I did everything right, but I need "getMax" and "countPassing" functions to have "value returning"....I don't know how to do "value returning", so I need help with that.

Here is my code:

#include <iostream>
#include <iomanip>

using namespace std;

const int numofgrades = 10;

void getGrades(double grades[]);
double computeAverage(double grades[]);
double getMax(double grades[]);
void countPassing(double grades[]);

int main ()

{
    double grades[numofgrades];
    double avg;
    double largest;

    getGrades(grades);
    computeAverage(grades);
    getMax(grades);
    countPassing(grades);


    system ("PAUSE");
    return 0;
}


    void getGrades(double grades[])
{
    cout<<"Please enter 10 grades"<<endl;
    cout<<""<<endl;

    for (int i=0; i<numofgrades; i++)
    {
     cout<<"Grade #"<<(i+1)<<endl;
     cin>>grades[i];
    }

}

    double computeAverage(double grades[])
{
    double avg;
    double sum = 0;

    for (int i=0; i<10; i++)
    {
    sum+=grades[i];
    avg = (sum/10);

    cout<<"The class average is "<<avg<<"%"<<endl;

    }
}

    double getMax(double grades[])
{

    double largest = grades[0];

    for (int i=0; i<numofgrades; i++)
    {
    if (grades[i] > largest)

    largest = grades[i];

    cout<<"Highest grade is "<<largest<<"%"<<endl;
    }

}

    void countPassing(double grades[])
{
   double sum = 0; 

   for (int i=0; i<numofgrades; i++)
   {

       sum+=grades[i];

       if (grades[i] > 59)
       i++;

       cout<<(i++)<<"out of "<<numofgrades<<" passed the test"<<endl;
   }


}

Recommended Answers

All 8 Replies

#include <iostream>
#include <iomanip>

using namespace std;

const int numofgrades = 10;

void getGrades(double grades[]);
double computeAverage(double grades[]);
double getMax(double grades[]);
void countPassing(double grades[]);

int main ()

{
    double grades[numofgrades];
    double avg;
    double largest;
    
    getGrades(grades);
    computeAverage(grades);
    getMax(grades);
    countPassing(grades);
    

    system ("PAUSE");
    return 0;
}


    void getGrades(double grades[])
{
    cout<<"Please enter 10 grades"<<endl;
    cout<<""<<endl;
    
    for (int i=0; i<numofgrades; i++)
    {
     cout<<"Grade #"<<(i+1)<<endl;
     cin>>grades[i];
    }
    
}

    double computeAverage(double grades[])
{
    double avg;
    double sum = 0;
    
    for (int i=0; i<10; i++)
    {
    sum+=grades[i];
    avg = (sum/10);
    
    cout<<"The class average is "<<avg<<"%"<<endl;
    
    }
}

    double getMax(double grades[])
{
    
    double largest = grades[0];
    
    for (int i=0; i<numofgrades; i++)
    {
    if (grades[i] > largest)
    
    largest = grades[i];

    cout<<"Highest grade is "<<largest<<"%"<<endl;
    }

}

    void countPassing(double grades[])
{
   double sum = 0; 
   
   for (int i=0; i<numofgrades; i++)
   {
       
       sum+=grades[i];
       
       if (grades[i] > 59)
       i++;
       
       cout<<(i++)<<"out of "<<numofgrades<<" passed the test"<<endl;
   }
   
    
}

Don't have your cout statements inside of your loops if you only want them to display once. Calculate a value inside of the loop, then display it once the loop is over.

Lines 77, 82 - Get rid of the sum variable. The function has nothing to do with sums, plus you don't ever do anything with the variable once it has a value.

Lines 85, 87 - You need to use a different variable for the number of passing scores. i is already used as your loop counter.

Thanks for your replying.

I corrected and fixed the cout part, but I'm having trouble with what you meant on lines 85 and 87 (changing the variable). Can you clarify it more for me. I didn't quite understand....Also, is "countPassing" and "getMax" value returning? If not, how do I go about doing that.

Here is my updated code:

#include <iostream>
#include <iomanip>

using namespace std;

const int numofgrades = 10;

void getGrades(double grades[]);
double computeAverage(double grades[]);
double getMax(double grades[]);
void countPassing(double grades[]);

int main ()

{
    double grades[numofgrades];
    double avg;
    double largest;

    getGrades(grades);
    computeAverage(grades);
    getMax(grades);
    countPassing(grades);


    system ("PAUSE");
    return 0;
}


    void getGrades(double grades[])
{
    cout<<"Please enter 10 grades"<<endl;
    cout<<""<<endl;

    for (int i=0; i<numofgrades; i++)
    {
     cout<<"Grade #"<<(i+1)<<endl;
     cin>>grades[i];
    }

}

    double computeAverage(double grades[])
{
    double avg;
    double sum = 0;

    for (int i=0; i<numofgrades; i++)
    {
    sum+=grades[i];
    avg = (sum/numofgrades);  
    }

    cout<<"The class average is "<<avg<<"%"<<endl;

}

    double getMax(double grades[])
{

    double largest = grades[0];

    for (int i=0; i<numofgrades; i++)
    {
    if (grades[i] > largest)

    largest = grades[i];
    }

    cout<<"Highest grade is "<<largest<<"%"<<endl;

}

    void countPassing(double grades[])
{

   for (int i=0; i<numofgrades; i++)
   {

       if (grades[numofgrades] > 59)
       i++;

       cout<<(i++)<<"out of "<<numofgrades<<" passed the test"<<endl;


   }


}

What VernonDozier meant was to remove variable "i" from your code and introduce another variable

void countPassing(double grades[])
{
int a; 
a=0;  
   for (int i=0; i<numofgrades; i++)
   {

               
       if (grades[numofgrades] > 59)
       a++;
       
       cout<<a<<"out of "<<numofgrades<<" passed the test"<<endl;
       
       
   }

And secondly ,remove all the "cout" statements out of the for loops, Because it would be printed on every run of the loop.You can keep the cout statement out of the for loop, so after completing the function the total will only be printed once.

Also, is "countPassing" and "getMax" value returning? If not, how do I go about doing that.

double getMax(double grades[])
{    
    double largest = grades[0];
    
    for (int i=0; i<numofgrades; i++)
    {
        if (grades[i] > largest)  
            largest = grades[i];
    }

    cout<<"Highest grade is "<<largest<<"%"<<endl;
}

No, those functions are not returning any values, though they are supposed to. It's pretty easy to tell because there is no key word return in the function above. In line 1, you declare that you are returning a double, but you never do. This is an error, though your compiler might let you get away with it. To make it return a value, simply type this at the very end of the function:

return largest;

You can harness that value at the function call in the main function:

double highestGrade = getMax(grades);
cout << "The highest grade is " << highestGrade;

Thus if you like, you can remove the cout statement from the function altogether and have it display the results from the main function instead.

Thanks for the reply.

For example, in my output, I will put:

Enter grade:

54
85
63
21
58
69
99
52
65
21

etc.

Class average: 58.7% (correct - non value returning)
Enter Highest grade: 12% (which should be 99% in this case)
0 out of 10 passed the test (it should be 5 out of 10 passed the test)

I don't know where I mistake the mistake it. I put the stuff in main. I tried to switch things around, but I get an ugly result when I do.

Here is my updated code:

#include <iostream>
#include <iomanip>

using namespace std;

const int numofgrades = 10;

void getGrades(double grades[]);
double computeAverage(double grades[]);
double getMax(double grades[]);
int countPassing(double grades[]);

int main ()

{
    double grades[numofgrades];
    double avg;
    double largest;
    int pass;

    getGrades(grades);
    computeAverage(grades);
    largest = getMax(grades);
    pass = countPassing(grades);

    cout<<"Highest grade is "<<largest<<"%"<<endl;
    cout<<pass<<"out of "<<numofgrades<<" passed the test"<<endl;




    system ("PAUSE");
    return 0;
}


    void getGrades(double grades[])
{
    cout<<"Please enter 10 grades"<<endl;
    cout<<""<<endl;

    for (int i=0; i<numofgrades; i++)
    {
     cout<<"Grade #"<<(i+1)<<endl;
     cin>>grades[i];
    }

}

    double computeAverage(double grades[])
{
    double avg;
    double sum = 0;

    for (int i=0; i<numofgrades; i++)
    {
    sum+=grades[i];
    avg = (sum/numofgrades);  
    }

    cout<<"The class average is "<<avg<<"%"<<endl;

}

    double getMax(double grades[])
{

    double largest = grades[0];
    double sum = 0;

    for (int i=0; i<numofgrades; i++)
    {
    if (grades[i] > largest)
    largest = grades[i];

    return largest;

    }

}

   int countPassing(double grades[])
{

   int pass = 0;

   for (int i=0; i<numofgrades; i++)
   {

   if (grades[numofgrades] > 59)
   pass++;

   return pass;

   }

}

Look carefully at the red below and see if that is what you want:

int countPassing(double grades[])
{
   
   int pass = 0;
   
   for (int i=0; i<numofgrades; i++)
   {
               
   if (grades[numofgrades] > 59)
   pass++;
   
   return pass;
   
   }
   
}

Look at the red highlighted code below and think about whether it is in the right place. How many times are you going through the loop?

double getMax(double grades[])
{
    
    double largest = grades[0];
    double sum = 0;
    
    for (int i=0; i<numofgrades; i++)
    {
    if (grades[i] > largest)
    largest = grades[i];
    
    return largest;
    
    }

}

Thanks for the reply.

The thing that I was wrong on was that I didn't put the return 'pass' and return 'largest' out of the for-loop. I also corrected what you told me above. I appreciate it. It all works now. Thanks for the help!

One DOubt,

In the for loop.

Wouldnt the function just return after one pass student.

I think the return should be after the for loop.

I think it should be after the for loop.

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.