Status
Not open for further replies.

Col. Forbin

Ars Scholae Palatinae
883
Subscriptor
I'm having a problem with a program for my C++ class (which is going way too slowly for my liking). The program gets 5 test scores, finds the lowest one and "drops" it. Then averages the remaining 4 and outputs the average to the screen. I got it working except for one case, when the lowest score is equal to another score (e.g. 93 91 89 85 85).

First, what would be a better way to go about this? And second, what can do to fix the same score problem? Code for the problem function is below.
code:<HR>
Code:
int findLowest(int score1, int score2, int score3, int score4, int score5)
{
	int sum;
	if (score1 < score2)
	{
		if (score1 < score3)
		{
			if (score1 < score4)
			{
				if (score1 < score5)
					sum = score2 + score3 + score4 + score5;
			}
		}
	}
	else if (score2 < score1)
	{
		if (score2 < score3)
		{
			if (score2 < score4)
			{
				if (score2 < score5)
					sum = score1 + score3 + score4 + score5;
			}
		}
	}
	else if (score3 < score1)
	{
		if (score3 < score2)
		{
			if (score3 < score4)
			{
				if (score3 < score5)
					sum = score1 + score2 + score4 + score5;
			}
		}
	}
	else if (score4 < score1)
	{
		if (score4 < score2)
		{
			if (score4 < score3)
			{
				if (score4 < score5)
					sum = score1 + score2 + score3 + score5;
			}
		}
	}
	else if (score5 < score1)
	{
		if (score5 < score2)
		{
			if (score5 < score3)
			{
				if (score5 < score4)
					sum = score1 + score2 + score3 + score4;
			}
		}
	}
	return sum;
}

Any help would be greately appreciated. Thanks.

-Forbin
 

shabadu

Ars Tribunus Militum
1,851
yipes! I'd just sort the scores and remove the lowest from the sorted array! You may not be allowed to do that if the scores are in a special order, but you could pass by value the array to a function that sorts it and returns the lowest score, then search the original for that score and remove it. Anything's better than that huge if else block. That's scary!

edit: this would fix both your problems

edit again: out of curiosity, where are you in chicago? I'm a student at IIT
 

Col. Forbin

Ars Scholae Palatinae
883
Subscriptor
The thing is, we haven't gotten to arrays yet in the class material. I could read ahead, but I have a feeling the teacher (from the way he acts in class) would not like me "showing off" by doing something he hasn't "taught" us. ;)

-Forbin

edit: missed your second edit. I'm about 8 miles south of IIT campus, and about 2 or 3 miles west, just off the Ryan Expy.

[This message was edited by Col. Forbin on November 27, 2001 at 23:44.]
 

yoggo

Smack-Fu Master, in training
81
what about adding all the scores together, then going through and finding the lowest, and deleting it from the total?

code:<HR>
Code:
FindAverageScore( i1, i2, i3, i4, i5 )
{
  sum = i1 + i2 + i3 + i4 + i5;
  lowest = 0;
  if ( i1 < lowest )
    lowest = i1;
  if (i2 < lowest )
    lowest = i2;
  .. etc ..
  sum = sum - lowest;
  average = sum / 4.0;
  return average;
}<P>
 

moses_the_red

Wise, Aged Ars Veteran
137
Try using some "ands" in your conditional statements. For instance that entire first piece of code
[if (score1 < score2) {
if (score1 < score3)
{
if (score1 < score4) {
if (score1 < score5) sum = score2 + score3 + score4 + score5; } }]

Could be written
[
if( (score1<score2) && (score1<score3) && (score1<score4) && (score1<score5))
{
sum = score2+score3+score4+score5;
}
]
You should also take shabadu's advice and learn about arrays. It will seriously shorten your code.
 
add up all four. Create 4 variables, subtract each of the original 4 from the sum, and average, one for each variable. Nested if's to find the largest.


s1 = v1 + v2 + v3 + v4

a1 = (s1 - v1) / 3
a2 = (s1 - v2) / 3
.....

at which point i'm back where you are. Damn. For some reason, i thought there was a trick if you start this way. I'm going to post this anyway incase someone catches the trick at the end.


edit :---------

Yoggo beat me. That's what i was thinking of.
 

shabadu

Ars Tribunus Militum
1,851
If it doesn't have to be done in one function call, you could have a function like this:
code:<HR>
Code:
int findLowest(int score1, int score2)
{
  if (score1 < score2)
    return score1;
  else
    return score2;
}
and then the main would be like this
code:<HR>
Code:
int lowest=0, ave=0;
lowest = findLowest(score5, findLowest(score4, findLowest(score3, findLowest(score2, score1))));
ave=score1+score2+score3+score4+score5-lowest;
It's probably possible with recursion, but I can't think of how.
edit: I didn't realize you had to average the others too. I just put that in.

I think I win the shortest code prize so far. :p
 

hanser

Ars Legatus Legionis
43,847
Subscriptor++
The easiest way, is, of course, using arrays. To show how clean it could be:

Code:
<P>
int findInfo()
{
	int scores[] = {50,100,0,0,1};
	int smallest = 65000, total = 0;
	float average;<P>	for (int i = 0; i <= 4; i++)
	{
		if (scores[i] < smallest)
		{
			smallest =  scores[i];
		}
		total += scores[i];
	}
	average = (total - smallest) / 4;
	cout << smallest << endl;
	cout << average << endl;
	return 0;
}
 

Col. Forbin

Ars Scholae Palatinae
883
Subscriptor
Guess I forgot to say this before, but since this program is to help me "learn" how to use functions, we have to use 3 functions. One to cin the values, another to find the lowest value, and a third to average them. :( Otherwise yoggo would win for the best suggestion. But I will also use the &&'s instead of the nested if's.

-Forbin
 
Originally posted by shabadu:
What's the goal of this program? Is it just supposed to teach the use of conditions? If so, then you probably have the solution he's looking for.

If it were me, I'd use a pointer just to piss him off. :D

All programs for class should use unions, duff's device, and goto's whereever possible :D. I was so happy when my os class let me use goto's. Had a big if statement read
code:<HR>
Code:
if (stuff) {
    do_stuff();
    if (condition) {
        goto more_stuff;
    }
} else {
more_stuff:
    do_stuff2();
};

I found programming fairly simple when I was doing it, so I tried hard to be evil when i did do it. :D

So can anyone think of other ways to solve this without using arrays? It now amuses me.
 
Originally posted by Col. Forbin:
Guess I forgot to say this before, but since this program is to help me "learn" how to use functions, we have to use 3 functions. One to cin the values, another to find the lowest value, and a third to average them. :( Otherwise yoggo would win for th ebest suggestion. But I will also use the &&'s instead of the nested if's.

-Forbin
\\\

Yoggo still has a vaild solution then, just rearrange it. (look at mine some)

read_in ();
find_lowest(0-average1(), 0-average2()......)


follow? You use 3 functions, One to read in, One to average all 5, one to find the lowest (since lowest, 0-average would make the highest the lowest). See?


edit:----------
errr.... average 4 of the 5. hmm, I guess it isn't much like Yoggo's. My bad.
 

Kozm0

Ars Scholae Palatinae
819
I got it working except for one case, when the lowest score is equal to another score (e.g. 93 91 89 85 85).

Well you've already got plenty of good alternatives and since I'm really not in the mood to debug your code at 1 AM.
I'll just point out that in none of your if statements are you using a "__ or equal" statement such as <= or >=, and that _MIGHT_ be (not is, i haven't looked over the whole thing) the cause of the buggy beheavior when 2 values are equalities.
 

jaem

Ars Scholae Palatinae
1,023
Originally posted by Col. Forbin:
I'm having a problem with a program for my C++ class (which is going way too slowly for my liking).
...

-Forbin

First thing is that you seek a lowest number in a list, which might not be unique. Hence you should not use strict inequalities (< ) but "large" ones (<=) (as pointed by Kozm0). Then the function should work, but it's badly written: It's of the form:
if (...)
{} /* executed if (...) true */
else
{} /* executed if (...) false */
else
{} /* Now what? we did treat the two possible cases */

My C compiler accept this syntax. It would have been easier if you had kept track of what assertions were true inside blocks:

code:<HR>
Code:
{
    if (l1<=l2)
    {   /* l1<=l2 */
	if (l1<=l3)
	{   /* l1<=l2,l3 */
	    if (l1<=l4)
	    {   /* l1<=l2,l3,l4 */
		if (l1<=l5) then sum=l2+l3+l4+l5      
				else sum=l1+l2+l3+l4; 
	    }
	    else                                      
	    {   /* l4<l1, l1<=l2,l3 */
		if (l4<=l5) then sum=l1+l2+l3+l5
				else sum=l1+l2+l3+l4;
	    }
	}
	else  
	{   /* l3<l1, l1<=l2 */
	    if (l3<=l4) then 
		{   /* l3<=l4, l3<l1, l1<=l2 */
		    if (l3<=l5) then sum=l1+l2+l4+l5
				    else sum=l1+l2+l3+l4;
		}
etc...
 
Status
Not open for further replies.