Make a number equal to the bigger number


C

chriswo2k

hello

whats the best way to make a number equal to a bigger number?

so say we have:

int one = 1;
int two = 2;
int three = 3;
int biggest;

is there a way to set biggest to whatever number (one, two, three) is biggest?

all I can come up with is a loooong if-else sequence. gotta be a way to do this in a line or two.

ChrisW
 
Ad

Advertisements

O

ozbear

hello

whats the best way to make a number equal to a bigger number?

so say we have:

int one = 1;
int two = 2;
int three = 3;
int biggest;

is there a way to set biggest to whatever number (one, two, three) is biggest?

all I can come up with is a loooong if-else sequence. gotta be a way to do this in a line or two.

ChrisW
biggest = one > two && one > three ? one : two > three ? two : three;

In other words, if the first conditional clause is true, then "one"
must be the biggest. Otherwise either either "two" or "three" or both
are bigger than "one" so the problem reduces to finding the larger of
"two" and "three" which the second conditional determines.

Oz
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top