posted 2 years ago

I am trying to find the median of 3 numbers. I am having trouble figuring out where certain if statements should be. I am only allowed to use if - then and else statements. Also, no || and &&. Here is what I have so far.

if (s < t)

if (f < t)

median = f;

else if (t < f)

median = f;

//(f = first, s = second, t = third)

Could you give me some guidance on how to complete this for s and t? Nested if statements are OK.

Thanks!

if (s < t)

if (f < t)

median = f;

else if (t < f)

median = f;

//(f = first, s = second, t = third)

Could you give me some guidance on how to complete this for s and t? Nested if statements are OK.

Thanks!

posted 2 years ago

It's going to be easier if you indent your code properly. (You may have tried but you didn't UseCodeTags (that's a link)).

Can you figure it out from there?

Can you figure it out from there?

All things are lawful, but not all things are profitable.

mark gile

Greenhorn

Posts: 4

mark gile

Greenhorn

Posts: 4

posted 2 years ago

I see what you mean. I tried it again and it wouldn't work with certain inputs. Could I get anymore help?

Henry Wong wrote:

m gayle wrote:

Look good?

Since there are six permutations of three numbers, I would guess that there are three cases that are not taken care of by this set of comparisons.

Henry

I see what you mean. I tried it again and it wouldn't work with certain inputs. Could I get anymore help?

posted 2 years ago

I am afraid there is a piece of equipment which is hindering you and it needs to be removed from your attention so as not to distract you. The simplest way to do that is to power down your computer.

Get a sheet of paper and a pencil and an eraser (the latter probably the most important) and write down how you would do it without a computer. Once you have that, you will probably find it easy to turn into code.

Get a sheet of paper and a pencil and an eraser (the latter probably the most important) and write down how you would do it without a computer. Once you have that, you will probably find it easy to turn into code.

posted 2 years ago

Highly agree with Campbell.

There are six possible permutations. First, you need to figure out what those six are (listing them). And second, you need to map the path to all six of them. Mapping it out on paper was how I was able to do it.... and quite frankly, I would be very impressed if someone can do it without mapping it out first (and simply coding).

Henry

Highly agree with Campbell.

There are six possible permutations. First, you need to figure out what those six are (listing them). And second, you need to map the path to all six of them. Mapping it out on paper was how I was able to do it.... and quite frankly, I would be very impressed if someone can do it without mapping it out first (and simply coding).

Henry

It is sorta covered in the JavaRanch Style Guide. |