954,535 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Finding the lowest of five test scores

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: 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.
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.
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!!!:|

naya22
Light Poster
48 posts since Apr 2007
Reputation Points: 24
Solved Threads: 0
 
  • Your implementation of getScore doesn't appear to be asking the user for anything....
  • A no-brainer.
  • 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.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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]

Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
 

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

naya22
Light Poster
48 posts since Apr 2007
Reputation Points: 24
Solved Threads: 0
 

>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.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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!!!

naya22
Light Poster
48 posts since Apr 2007
Reputation Points: 24
Solved Threads: 0
 

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.

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

Got it! Thanks

naya22
Light Poster
48 posts since Apr 2007
Reputation Points: 24
Solved Threads: 0
 

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?

naya22
Light Poster
48 posts since Apr 2007
Reputation Points: 24
Solved Threads: 0
 

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;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,050 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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.

naya22
Light Poster
48 posts since Apr 2007
Reputation Points: 24
Solved Threads: 0
 

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...

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

another thing... why are you using

int small


as a parameter in calcAverage?

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

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?

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

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.

anupam_smart
Posting Pro
599 posts since Feb 2006
Reputation Points: 15
Solved Threads: 4
 
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);
Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

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.

naya22
Light Poster
48 posts since Apr 2007
Reputation Points: 24
Solved Threads: 0
 
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).

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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...

Nichito
Posting Virtuoso
1,602 posts since Mar 2007
Reputation Points: 424
Solved Threads: 57
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You