Using a Function to find the lowest score

Any Help would be nice.
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
#include <iostream>
using namespace std;

double getJudgeScore(double judge);
double findLowest(double lowest);


int main()
{
	double judge;
	double lowest = 15;
	
	cout << "Enter score from judge #1: ";
	cin >> judge;
	getJudgeScore(judge);
	findLowest(lowest);

	cout << "Enter score from judge #2: ";
	cin >> judge;
	getJudgeScore(judge);
	findLowest(lowest);

	cout << "Enter score from judge #3: ";
	cin >> judge;
	getJudgeScore(judge);
	findLowest(lowest);

	cout << "Enter score from judge #4: ";
	cin >> judge;
	getJudgeScore(judge);
	findLowest(lowest);

	cout << "Enter score from judge #5: ";
	cin >> judge;
	getJudgeScore(judge);
	findLowest(lowest);

	return 0;
}

double getJudgeScore(double judge)
{
	if(judge >= 0 && judge <= 10)
	{
		return judge;
	}
	else
	{
		cout << "Score must be between 0 and 10. Please re-enter: ";
		cin >> judge;	
	}
}
double findLowest(double lowest)
{
	double judge;
	
	if(judge < lowest)
		lowest = judge;

}

As you can see I'm using a Function to data-validation the, but I cant get the lowest score Function to work. I'm new to Function so it may be some think easy that I over looked. Again any help would be nice and Thank You for your time.
OK, a couple of things:

To begin with, you're passing the variable "lowest" from main () to findLowest() by value. These are two separate variables; that they have the same name is irrelevant.) I recommend you pass it by reference, or use the return argument to get the value derived back to main(). The way you're doing it, the lowest in findLowest gets derived, then disappears when the function returns.

Also, I think you've introduced more complexity than is necessary. You also seem to be confusing the judge with the score the judge has awarded. Also, the same comment about passing the variable "judge" by value applies. Plus, I think the double variable judge should be called something like "score" for clarity, but this won't affect the way the program runs.

Try tackling these changes first, then let's see how it's working.
Last edited on
Thank you for your reply, I have been reading up on reference but I don't fully understand them. All so I have add the return lowest; like you suggested but I still cant get it to work right.
1
2
3
4
5
6
7
8
9
10
11
12
13
double findLowest(double lowest)
{
	double judge;
	lowest = 15;

	if(judge < lowest)
	{
		lowest = judge;
		return lowest;
	}
	else
		return lowest;
}


On a different not I'm I putting the findLowest(lowest); in the right areas in the int main()?

Hm, findLowest(double lowest) doesnt do anything, since you overwrite your lowest variable with 15 in the second line. Also, judge is a random value since you havent initialized it.

Also, in your first code, you never catch the variable returned by getJudgeScore(judge).
You need to use pointers if you want your functions to affect the variables in your main program, since your function gets only a copy of your judge and lowest variables, completely ignoring the original ones.

So, the main problem here is that you dont realize that the lowest and judge variables in your functions are totally different from the ones in your main program, all are local, never affect each other.

Also, you make your function a return which you never assign to anything..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main(){
        double lowest;
        double temp;
	
        for (int i=1; i<=5; i++){
          if (i==1) lowest=getScoreFrom(i);
        
          else if ((temp=getScoreFrom(i))<lowest) lowest=temp;
        }

	return 0;
}

double getScoreFrom(int judgeID){
  double score=-1;
  cout << "Enter score from Judge #" << judgeID << endl;
  do {
    cin >> score;
  } while (score<0 || score >10);
  return score;
}


This is another solution to the same problem, hope it helps. I doubt it would compile bcos i wrote it just here but it has the main point.
Last edited on
Hello, I am working with Moonlit on this project. Your solution is logical and we understand it, however we are not allowed to use for loops on this.

We also cannot declare any variables outside of functions.

It is these restrictions mainly that have us tripped up. Any advice would be greatly appreciated.
Well, you don't need any global variables, and they're usually not a good idea, so I'm not surprised your professor disallowed them.

I still don't see the need for your function, though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	double judge;
	double lowest = 15;
	
	cout << "Enter score from judge #1: ";
	cin >> judge;
        lowest = min(lowest, judge);

	cout << "Enter score from judge #2: ";
	cin >> judge;
        lowest = min(lowest, judge);

	cout << "Enter score from judge #3: ";
	cin >> judge;
        lowest = min(lowest, judge);

	cout << "Enter score from judge #4: ";
	cin >> judge;
        lowest = min(lowest, judge);

	cout << "Enter score from judge #5: ";
	cin >> judge;
        lowest = min(lowest, judge);
Last edited on
Yes your right there is no need for a function but are professor whats us to use functions.
Last edited on
Did your professor say what he wanted you to use the function for? Like creating your own min() function?
We have to prototype the function above main, define them below main. There is to be no variables out side the functions at all.

Here is are latest code xD.
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
#include <iostream>
using namespace std;

double getJudgeScore(double judgeID);
double findLowest(double lowest);
double findHigest(double higest);

int main()
{

	cout << getJudgeScore;
	cout << findLowest;
	cout << findHigest << endl;

	return 0;
}

double getJudgeScore(double judgeID)
{
	double score = -1;

	cout << "Enter score from Judge #" << judgeID << ": ";
	cin >> score;

	do
	{
		cin >> score;
	}
	while(score < 0 || score > 10);
	
	return score;
}

double findLowest(double lowest)
{
	int lowest;
	int lowtemp;
	
	for (int i = 1; i <= 5; i++)
	{
		if ( i == 1) 
			lowest = getJudgeScore(i);
        else if ((lowtemp = getJudgeScore(i)) < lowest) 
			lowest = lowtemp;
	}
	return lowest;
}

double findHigest(double higest)
{
	int higest;
	int higtemp;

	for(int a = 1; a <= 5; a++)
	{
		if(a == 1)
			higest = getJudgeScore(a);
		else if ((higtemp = getJudgeScore(a)) > higest)
			higest = higtemp;
	}
	return higest;
}


The getJudgeScore and findLowest functions work right now, its just the findHigest function now.
Thank You for all your help.
OK, I realize that I'm skipping around here, but...you do realize that your cout calls in main() are missing valid functions, right? Parens, parameters, etc).

Also, if your professor isn't going to allow you to use for loops, is he going to allow do/while loops? That doesn't make much sense.
Topic archived. No new replies allowed.