# pick the largest, second and third number from an array

cs thiang

Greenhorn

Posts: 8

posted 11 years ago

think about how you would do it by hand. if i gave you a stack of papers, each witha number written on it, how would you find the largest? the second largest?

there are a couple of ways... you could do it with three passes through the data, each time saving the largest of what's left.

or, it could be done in one pass, if you think about how to store the info...

but write out in english how you would do it, then use that to help figure out the code

there are a couple of ways... you could do it with three passes through the data, each time saving the largest of what's left.

or, it could be done in one pass, if you think about how to store the info...

but write out in english how you would do it, then use that to help figure out the code

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

cs thiang

Greenhorn

Posts: 8

posted 11 years ago

Hmm...the Math.max method can only compare 2 integer...how to have it to compare 8?

Originally posted by fred rosenberger:

think about how you would do it by hand. if i gave you a stack of papers, each witha number written on it, how would you find the largest? the second largest?

there are a couple of ways... you could do it with three passes through the data, each time saving the largest of what's left.

or, it could be done in one pass, if you think about how to store the info...

but write out in english how you would do it, then use that to help figure out the code

Hmm...the Math.max method can only compare 2 integer...how to have it to compare 8?

Mani Ram

Ranch Hand

Posts: 1140

posted 11 years ago

Hmm...what stops you to use Math.max method multiple times - either with in a loop or by nesting it like

And why do you need to use Math.max() method when the "greater than" operator is available?

Originally posted by cs thiang:

Hmm...the Math.max method can only compare 2 integer...how to have it to compare 8?

Hmm...what stops you to use Math.max method multiple times - either with in a loop or by nesting it like

*Math.max(a, Math.max(b, c))*?

And why do you need to use Math.max() method when the "greater than" operator is available?

Mani
*Quaerendo Invenietis*

posted 11 years ago

You're jumping ahead... Don't worry about the tools until you know what you need... Follow Fred's advice.

How would you do it by hand? Once you figure that out, how would you write that in pseudo code?

Henry

Originally posted by cs thiang:

Hmm...the Math.max method can only compare 2 integer...how to have it to compare 8?

You're jumping ahead... Don't worry about the tools until you know what you need... Follow Fred's advice.

How would you do it by hand? Once you figure that out, how would you write that in pseudo code?

Henry

cs thiang

Greenhorn

Posts: 8

posted 11 years ago

By hand:

First, out of the array of 8 integer, i take out the largest one.

then, out of the 7 , i take out the largest again.

lastly, out of the 6, i take out the largest.

But here comes another qn, how do i take away, coz the numbers in the array are random and how do i get the loop not to count the one that i wanted to take out?

[ January 31, 2006: Message edited by: cs thiang ]

First, out of the array of 8 integer, i take out the largest one.

then, out of the 7 , i take out the largest again.

lastly, out of the 6, i take out the largest.

But here comes another qn, how do i take away, coz the numbers in the array are random and how do i get the loop not to count the one that i wanted to take out?

[ January 31, 2006: Message edited by: cs thiang ]

posted 11 years ago
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Your brain can only compare two integers at a time. you may think you can do 3, 4, or 8 at a time, but really what you're doing is comparing two, remembering which of the two is bigger, and comparing THAT one to the next, and so on...

(that was a BIG hint, btw).

(that was a BIG hint, btw).

Layne Lund

Ranch Hand

Posts: 3061

posted 11 years ago

I think this is closer to what Fred was trying to get you to think about. To answer your question, you cannot remove an element from an array since an array has a fixed size. Can you come up with a way to make it LOOK like you remove an element from an array? Let's put this in terms of Java and say you want to write a method with the following signature:

Again, you should step back and describe the steps it would take to do this. I think Fred's illustration is very apt. If we gave you a stack of papers with numbers written on them, how would you find the maximum value in the stack?

I hope this helps get the cogs turning for you. Good luck with this.

Layne

Originally posted by cs thiang:

By hand:

First, out of the array of 8 integer, i take out the largest one.

then, out of the 7 , i take out the largest again.

lastly, out of the 6, i take out the largest.

But here comes another qn, how do i take away, coz the numbers in the array are random and how do i get the loop not to count the one that i wanted to take out?

[ January 31, 2006: Message edited by: cs thiang ]

I think this is closer to what Fred was trying to get you to think about. To answer your question, you cannot remove an element from an array since an array has a fixed size. Can you come up with a way to make it LOOK like you remove an element from an array? Let's put this in terms of Java and say you want to write a method with the following signature:

Again, you should step back and describe the steps it would take to do this. I think Fred's illustration is very apt. If we gave you a stack of papers with numbers written on them, how would you find the maximum value in the stack?

I hope this helps get the cogs turning for you. Good luck with this.

Layne