Find the lowest number

This is a simple program I am building to find the average of 5 grades where the lowest grade is dropped. I created a function findLowest() which accepts the 5 grades as arguments, finds the lowest grade and returns it as "lowest". I have tried many different way to find the lowest number. Right now, I am working with if...then statements. I am wondering if there is a better way to find the lowest number or if anyone has any ideas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
using namespace std;

void printMessage();                           //Prototype for printMessage
void getScore(int&);                           //Prototype for getScore with reference variable
int  findLowest(int,int,int,int,int,int &);    //Prototype for findLowest with reference variable

int main()
{	
 printMessage();      //Calls printMessage	
	
 //Variables for test scores
 int score1, score2, score3, score4, score5;
 int low;	
 
 //Calls the getScore method for each 	
 getScore(score1);
 getScore(score2);
 getScore(score3);
 getScore(score4);
 getScore(score5);
 
 findLowest(score1,score2,score3,score4,score5,low);
  	
}

//*******************Functions***********************************

void getScore(int &testScore)
{   
	cout << "Enter the test score: ";
	cin  >> testScore;
	
	   while(testScore < 0 || testScore > 100)	
	      {
	       printMessage();
	       cout << "The score must be in the range of 0-100.\n\n";
	       cout << "Enter the test score: ";
	       cin  >> testScore;
	       cout <<endl;
	      }
}

int findLowest(int score1, int score2, int score3, int score4, int score5, int &lowest)
{ 
if(score1 < score2)

      lowest = score1;

else if(score2 < score1)

      lowest = score2;

if(score3 < lowest)

      lowest = score3;

if(score4 < lowest)

      lowest = score4;

if(score5 < lowest)

      lowest = score5;

 cout << "The lowest test score is " << lowest << endl;
}

void printMessage()
{   cout <<endl;
    cout <<"This program finds the average of a group of 5 test scores.\nThe lowest score in the group is dropped.\n"<<endl;
}
You can initialize lowest to the first score, then compare lowest to each other score, updating lowest if the next score is lower than what's currently in lowest.

You could also put the numbers into an array and sort the array. I would tend to use an array instead of having separate score1, score2, score3 etc. variables. Then you could use a for loop to get all the input, updating the lowest score so far as it loops around.
Maybe you can use loop here instead of series of if-else's. :)
Topic archived. No new replies allowed.