I am writing the following program that calculates the average of a group of test scores where the lowest score in the group is dropped. It has the following functions:

  1. void getScore()-should ask the user for a test score, store it in a reference parameter variable, and validate it. This function should be called by main once for each of the five scores to be entered.
  2. void calcAverage() should calculate and display the average of the four highest scores. This function should be called just once by main and should be passed the five scores.
  3. int findLowest(): should find and return the lowest of the five scores passed to it. It should be called by the calcAverage, who uses the function to determine which of the five scores to drop.

So far, here is my version of the program:

#include <iostream>
#include <conio>
using namespace std;
//Function prototype
void getscore(int, int, int, int, int);
 
int main()
{
        int score1, score2, score3, score4, score5;
        cout << "Enter your five test scores: ";
        cin  >> score1  >> score2 >> score3  >> score4  >> score5;
        getscore (score1, score2, score3, score4, score5) <<endl;
        getch();
        return 0;
}
void getscore(int score1, int score2, int score3, int score4, int score5)
{
        cout << "Your five test scores are:\n";
        cout << score1 <<endl;
        cout << score2 <<endl;
        cout << score3 <<endl;
        cout << score4 <<endl;
        cout << score5 <<endl;
}

But, as far as the void calcAverage() and the int findLowest() functions, I am totally confused. I have read my textbook. My instructor tells me that I can use an if-else statement for this program to find the lowest score; she doesn't want us using MAX or MIN or anything like that; we haven't gotten that far.
I need help!!!:|

  1. Your implementation of getScore doesn't appear to be asking the user for anything....
  2. A no-brainer.
  3. Hint: stop using score1, score2, scorex and start using arrays. It will not only simplify the rest of your program, but it'll make searching for the smallest score as simple as creating a loop.

BTW, this is wrong:

getscore (score1, score2, score3, score4, score5) <<endl;

Can't add endl to something that's not a stream.

MIN and MAX won't really be of much value in your program anyway.

First use pencil & paper to find the minimum value of the 5 scores. That will give you a clue how to write the program. You would set the initial minimum value to be the value of the first number. Then look at the second number -- if it is less then the current minimum change to current minimum to be the second number. Do the same with each of the other 3 values. In your program you will have a series of 4 if statements, one if statement for each of the numbers 2 through 5.

Now do the same thing to find the maximum value.

[edit] I agree with Joe about using arrays, but only if you have learned them yet. If you can use arrays it will make the coding a lot easier[/edit]

There are a few problems with the existing code itself:
1. Requirement says "void getScore()-should ask the user for a test score, store it in a reference parameter variable,"
=> This is not done. So teh values updated within the function won't be reflected in main.
2. Usually it's a better idea to use an array of 5 elements instead of 5 int variables. So if you have already learned arrays you should use that.


Coming to the min/max part. Well you need to figure out which variable holds the min value. Try and write down a small algo in english to find this out.
Anyway before you come to this part I would say you tell us if you can use an array or not, things will depend on it.

Actually, we haven't even got to arrays in our textbook yet. So, I really don't know how to do them.

>Actually, we haven't even got to arrays in our textbook yet. So, I really don't know how to do them.
Sorry about that. I agree with AD and thekashyap, write out the algorithm with pencil and paper, and you'll see it much clearer.

Ok, I'll try that and come back.

Thanks for all of your help.

:-)I'll make sure to give you guys credit when I complete this program!!!

actually, what AD says bout the max/min value is absolutely the best way to do this. You just have to assign the value in the first variable to another one,so you can compare with if´s, so if it is lower than the temporary variable, you assign the value you just compared to this varuable and continue comparing.

As for the getScore function, you should initiate the 5-scores variables outside of the main, so they are general to the whole program, so you ask the user for them inside the getScore() function.

Now, the calcAverage() function should not be a problem.

Well, I changed the getscore() function, and that is working fine.

However, I am confused on the findLowest function. This is what I came up with:

void findLowest(int LO, int score1, int score2, int score3, int score4, int score5)
{
        (LO = 1000);
        if (score1 < LO)
        {
            LO = score1;
            cout << "The lowest score is" <<LO <<endl;
        }
        else if (score2 < LO)
        {
            LO = score2;
            cout << "The lowest score is" <<LO <<endl;
        }
        else if (score3 < LO)
        {
            LO = score3;
            cout << "The lowest score is" <<LO <<endl;
        }
        else if (score4 < LO)
        {
            LO = score4;
            cout << "The lowest score is" <<LO <<endl;
        }
        else if (score5 < LO)
        {
            LO = score5;
            cout << "The lowest score is" <<LO <<endl;
        }
}

But, when I run it, it declares all of the scores to be the lowest. How do I find out (using if statements of if/else if statements) which one is the lowest?

1. remove the else in that function. Each score must be tested and the else is preventing that from happening.

2. At the top of the function set LO to be initially the value of score1.

3. Why is LO a parameter to the function? Make it a local variable and return it at the end of the function.

int findLowest(int score1, int score2, int score3, int score4, int score5)
{
    int lo = score1;
    // other code here

   return lo;
}

Well, I revised my code, and this is what I came up with.

#include <iostream>
#include <conio>
using namespace std;
//Function prototype
void getscore(int&, int&, int&, int&, int&);
int findLowest (int, int, int, int, int);
void calcAverage (int, int, int, int, int, int, int);
int main()
{
        int average=0, score1=0, score2=0, score3=0, score4=0, score5=0, small=0;
        getscore (score1, score2, score3, score4, score5);
        calcAverage (average, score1, score2, score3, score4, score5, small);
        getch();
        return 0;
}
 
void getscore (int &score1, int &score2, int &score3, int &score4, int &score5)
{
        cout << "Enter first score: ";
        cin  >> score1;
        cout << "Enter second score: ";
        cin  >> score2;
        cout << "Enter third score: ";
        cin  >> score3;
        cout << "Enter fourth score: ";
        cin  >> score4;
        cout << "Enter fifth score: ";
        cin  >> score5;
}
int findLowest(int score1, int score2, int score3, int score4, int score5)
{
        int small =score1;
        if(small>score2)
        {
        small=score2;
        }
        if(small>score3)
        {
        small=score3;
        }
        if(small>score4)
        {
        small=score4;
        }
        if(small>score5)
        {
        small=score5;
        }
        return small; 
}
void calcAverage (int average, int score1, int score2, int score3, int score4, int score5, int small)
{
         
        average = ((score1 + score2 + score3 + score4 + score5)-small)/4;
        cout << average;
}

As soon as I tried to find the average of 100 + 100 + 100 + 100, my average came out to be 125!:@ When you run my program, you will see.

this is because you have a small mistake right in the end...

erm... you aren't using four scores... you are actually using five scores...

oooohh... i know where your problem is... when you call

calcAverage

you are using your variable "small" instead of the function you used to find the smallest... your code should go this way in line 12:

calcAverage(score1,score2,score3,score4,score5,
findLowest(score1,score2,score3,score4,score5));

or, the other way around, you could do this before calling your calcAverage function:

small=findLowest(score1,score2,score3,score4,score5);

which will work exactly the same...

got it?

The problem is that you are not getting the value of small as evaluated from the function findLowest().

So, better try this thing...

void calcAverage (int average, int score1, int score2, int score3, int score4, int score5)
{
         int small=findLowest(score1,score2,score3,score4,score5);
        average = ((score1 + score2 + score3 + score4 + score5)-small)/4;
        cout << average;
}

As suggested by Nichito.

Another point:
Why are you declaring average in main() and then passing as an argument? Just declare it in calcAverage() only.

Edited 3 Years Ago by happygeek: fixed formatting

Another point:
Why are you declaring average in main() and then passing as an argument? Just declare it in calcAverage() only.

i agree with anupam_smart, but you can save even more memory by not declaring average at all... just do

cout<<(((score1+score2+score3+score4+score5)-small)/4);

Here's my code again with the corrections that you suggested:

#include <iostream>
#include <conio>
using namespace std;
//Function prototype
void getscore(int&, int&, int&, int&, int&);
int findLowest (int, int, int, int, int);
void calcAverage (int, int, int, int, int,);
int main()
{
        int average, score1, score2, score3, score4, score5, small;
        getscore (score1, score2, score3, score4, score5);
        calcAverage (score1, score2, score3, score4, score5);
        getch();
        return 0;
}
 
void getscore (int &score1, int &score2, int &score3, int &score4, int &score5)
{
        cout << "Enter first score: ";
        cin  >> score1;
        cout << "Enter second score: ";
        cin  >> score2;
        cout << "Enter third score: ";
        cin  >> score3;
        cout << "Enter fourth score: ";
        cin  >> score4;
        cout << "Enter fifth score: ";
        cin  >> score5;
}
void calcAverage (int average, int score1, int score2, int score3, int score4, int score5)
{
         
        average = ((score1 + score2 + score3 + score4 + score5)-small)/4;
        cout << average;
}
int findLowest(int score1, int score2, int score3, int score4, int score5)
{
        int small =score1;
        if(small>score2)
        {
        small=score2;
        }
        if(small>score3)
        {
        small=score3;
        }
        if(small>score4)
        {
        small=score4;
        }
        if(small>score5)
        {
        small=score5;
        }
        return small; 
}

But, here are the error messages that i got:

  • Undefined symbol "small"

Small is representing the smallest number from the five numbers that I key in.

void calcAverage (int average, int score1, int score2, int score3, int score4, int score5)
{
         
        average = ((score1 + score2 + score3 + score4 + score5)-small)/4;

You never had small as a function parameter. And more to the point, you never calculated small in your program (even though you coded the function).

as suggested before, you shoul (in the main() function) assign your int small the value of the function findLowest, so, later, you can send it as a parameter to the function calcAverage... that should do it...

Okay, this is what I did:

#include <iostream>
#include <conio>
using namespace std;
//Function prototype
void getscore(int&, int&, int&, int&, int&);
int findLowest (int, int, int, int, int);
void calcAverage ();
int main()
{
        int average, score1, score2, score3, score4, score5;
        getscore (score1, score2, score3, score4, score5);
        calcAverage ();
        getch();
        return 0;
}
 
void getscore (int &score1, int &score2, int &score3, int &score4, int &score5)
{
        cout << "Enter first score: ";
        cin  >> score1;
        cout << "Enter second score: ";
        cin  >> score2;
        cout << "Enter third score: ";
        cin  >> score3;
        cout << "Enter fourth score: ";
        cin  >> score4;
        cout << "Enter fifth score: ";
        cin  >> score5;
}
void calcAverage (int average, int score1, int score2, int score3, int score4, int score5)
{
        int small=findLowest(score1,score2,score3,score4,score5);
        average = ((score1 + score2 + score3 + score4 + score5)-small)/4;
        cout << average;
}
int findLowest(int score1, int score2, int score3, int score4, int score5)
{
        int small =score1;
        if(small>score2)
        {
        small=score2;
        }
        if(small>score3)
        {
        small=score3;
        }
        if(small>score4)
        {
        small=score4;
        }
        if(small>score5)
        {
        small=score5;
        }
        return small; 
}

I made the necessary corrections (I think), but then when I ran it, it said that I has an unresolved external calcAverage() reference. Why is that???

Ok, this is what I did:

calcAverage (average, score1, score2, score3, score4, score5);

But now, my error says this:
Extra parameters in call to calcAverage

Did you remember to change this line as well?

int main()
{
        int average, score1, score2, score3, score4, score5;
        getscore (score1, score2, score3, score4, score5);
        calcAverage (); // ya have to pass it 'average' as well as scores

[edit]

Oh, now I see what the error means. You changed that line, but you didn't change your prototype:

void getscore(int&, int&, int&, int&, int&);
int findLowest (int, int, int, int, int);
void calcAverage (); // need something here!

when we recommended you to change the functions we told you, we meant you to change JUST THE LINES WE WERE TALKING ABOUT... instead, you changed the call 4 the function and the function prototype... which had nothing to do with what we recommended...;)

OHMIGOODNESS! It worked! It actually worked! Thanks you guys! You all are genuises

hi, why I can't run that program in my computer, because my teacher want DOUBLE calcAverage not void,
anyon can help me thanks and No less than - and higher than 100 for score

#include <iostream>
#include <conio>
using namespace std;
//Function prototype
void getscore(int, int, int, int, int);
 
int main()
{
        int score1, score2, score3, score4, score5;
        //===== 
        for (unsigned int i=0;i<5;i++)
        {
        cout << "Enter your five test scores: ";     
        if (i==0)
             cin  >> score1; // cin >> score[i];
        else if (i==1)
             cin >> score2 ;
        else if (i==2)
             cin >> score3 ;
        else if (i==3)
             cin >> score4;
        else if (i==4)
             cin  >> score5;
        }
        //============
        // or use array of int
        // int score[5];
        // ...
        // cin >> score[i];

        getscore (score1, score2, score3, score4, score5) <<endl;
        getch();
        return 0;
}
void getscore(int score1, int score2, int score3, int score4, int score5)
{
        cout << "Your five test scores are:\n";
        cout << score1 <<endl;
        cout << score2 <<endl;
        cout << score3 <<endl;
        cout << score4 <<endl;
        cout << score5 <<endl;
}

just wanted to say (even though this is closed)... make the int small global (declare it just before int main().
Globalizing variables ROX!!!

>make the int small global
Yea, that's bad advice.

>Globalizing variables ROX!!!
You're just messing with us, aren't you?

This question has already been answered. Start a new discussion instead.