This week's book giveaway is in the Java in General forum.We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Problem at the drawing board - ordering numbers.

ShaunRobert Williams
Greenhorn
Posts: 3
All,

Ive been trying to get into the programming grove with java, but wasnt having much opertunity to practise what Ive learnt, and then I thought I had found the perfect solution - The uni of auckland posts assignments from previous years on its website. I thought if I could do these, ill get plenty of the right kinda practise, i mean, these guys teach people how to code for a living, right?

One small problem, and its not with Java per se.

Im stuck trying to figure the algorythm for the 3rd (yes, the 3rd) excerise.

It sounds simple enough. Given 3 numbers, put them in order, smallest to bigest.
Just one complication: no conditional statements of any kind. Use on Math.max() and Math.min().

Now, im sure its possible, but I cant figure it. Neither can the various members of the family ive posed it as a simple puzzle too. Ive spend a good 2 hours on this away from the computer with 3 cards and a notebook.

I think I got finding the smallest and biggest, like this:

But, whatever i do, it seems without conditional statments (which I take to be if, while, and for)
I can not get the middle number.

Im sure its possible, probley very obvious, but the way my brain is wired, I can't figure.

Any pointers, tips and/or peusdocode would enable me to sleep tonight.
Thanks.

Joe Wolfe
Ranch Hand
Posts: 87
I guess nested function or methods

int maximum = Math.max(n1, Math.max(n2,n3)); or something similar.

There could be some neat OO way to do it too. not sure.

Most college textbooks have problems sets at the end of the chapters along with solutions for the odd or even exercises on the CDs. I suggest these.

This sounds like a trivial exercise for a college course?

Anupam Sinha
Ranch Hand
Posts: 1090
Now that you have the max and the min number. Add all the three orignal numbers together. Then subtract the sum of the maximum and the minimum from the sum of the three numbers.
[ May 08, 2007: Message edited by: Anupam Sinha ]