Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
April 15th, 2013, 05:08 PM
 mcydes
Registered User

Join Date: Apr 2013
Posts: 2
Time spent in forums: 1 h 22 m 57 sec
Reputation Power: 0
[HELP] finding largest of 5 integers without if statement

first post here, im a freshman going for a computer science degree and need a little help here to finish this program..

#include <stdio.h>
/* This function returns the larger of a and b. */
int larger(int a, int b)
{
if(a > b)
return a;
else
return b;
}

/* This function returns the largest of x1, x2, x3, x4, and x5. */
int max(int x1, int x2, int x3, int x4, int x5)
{
/* Your code comes here. */
/* Do not use any if statements in this function. */
}

main()
{
int a, b, c, d, e;
int largest;
printf("Enter five integers: ");
scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);
largest = max(a, b, c, d, e);
printf("\nLargest = %d\n", largest);
}

Thank you for the help in advance

#2
April 15th, 2013, 06:22 PM
 Scorpions4ever
Banned ;)

Join Date: Nov 2001
Location: Woodland Hills, Los Angeles County, California, USA
Posts: 9,534
Time spent in forums: 2 Months 3 Days 4 h 40 m 8 sec
Reputation Power: 4106
Piece of cake. HINT: call larger() from max() 4 times. Also pay attention to what larger() returns.
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne

#3
April 15th, 2013, 06:44 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,115
Time spent in forums: 1 Month 3 Weeks 2 Days 3 h 56 sec
Reputation Power: 455
If you're permitted to use a ternary operator
condition ? true_expression : false_expression)
then
the problem is easily solved
else
you could increment each number. The first one to have its sign change....hmm....you'd still need a detection mechanism.

Or you could subtract one from the other....that's how the INTEL cmp opcode works anyway. Save the flags, discard the result.

I keep returning to ?: .

oh, you're permitted to use larger[/edit]
__________________
[code]Code tags[/code] are essential for python code!

Last edited by b49P23TIvg : April 15th, 2013 at 06:46 PM.

#4
April 15th, 2013, 10:41 PM
 miz6565
Registered User

Join Date: Mar 2013
Posts: 96
Time spent in forums: 22 h 2 m 11 sec
Reputation Power: 0
I used the ternary operator.

Code:
```#include<stdio.h>

int data[9] = {5,7,5,7,8,5,10,8};

int max(int nums[])
{
int largest = nums[0];
int i;
for(i = 1;i <= sizeof(data) / 4;i++)
{
largest = nums[i] > largest ? nums[i] : largest;
}
return largest;
}

int main()
{
printf("%d",max(data));
getchar();
return 0;
}```

#5
April 15th, 2013, 11:16 PM
 mcydes
Registered User

Join Date: Apr 2013
Posts: 2
Time spent in forums: 1 h 22 m 57 sec
Reputation Power: 0
sorry for my ignorance, but for the max function is it something like returning larger to max and voiding b multiple times? (dont know if voiding is the right word)

nvm figured it out, heres the solution if for some off chance someone googles to this :P

int temp1, temp2, temp3, temp4;

temp1 = larger(a, b);
temp2 = larger(c, d);
temp3 = larger (e, temp1);
return temp3;

#6
April 16th, 2013, 10:33 AM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,115
Time spent in forums: 1 Month 3 Weeks 2 Days 3 h 56 sec
Reputation Power: 455
So, mcydes, just what would that fine algorithm return if the largest value happened to be in d or in c?

Comprehensive testing, though hard on the index finger, is worthwhile.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > [HELP] finding largest of 5 integers without if statement