• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

array algorithm

 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have been given a snipit of code and I am suppose to find a better algorithm for determining the max vale. My problem would be I do not see anything wrong. Any help would be appricated.
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well, if the values are in an array, you could do a sort and the pick off

the first, if you sorted them in descending order, or last if in ascending

order...

but im not sure if that would be better..

Justin
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I am dealing with arrays but the problem is that I really don't understand the code I was given
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Victoria Preston:
I have been given a snipit of code and I am suppose to find a better algorithm for determining the max vale. My problem would be I do not see anything wrong. Any help would be appricated.



It looks fine... You only access the members of the array once and only once. There doesn't seem to be any way to improve it.

The only issue that I can think of is what happens if all the values are negative -- the max will be zero, which is incorrect. Your max value should be initially set to the smallest possible double number.

double max = Double.NEGATIVE_INFINITY;

Henry
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All it tells me is there is a subtle error in it. I think it does have something to do with the first line
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Victoria Preston:
All it tells me is there is a subtle error in it. I think it does have something to do with the first line



Mr. Henry raised a very good point.

Secondly, you are using enhanced for-loop in the code which you provided, a feature of Java 1.5. In enhanced for-loop, element type must be specified. You have not specified the type of x

Naseem
[ September 09, 2006: Message edited by: Naseem Khan ]
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh ok, i thought it was saying that there was a better(efficient) algorithm,
but i was thinking to find the max value, you had to look at each index.

Justin
 
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Victoria:

1. First, the code:


I presume this is pretty self-explanatory (let's assume that the container for "doubles", and the variable "x", are both defined and initialized elsewhere). You're just looking through every element in "values" , and you're trying to set "max" to the largest value you find in that container.

2. The issue, as you noted, is whether or not you should initialize "max" to 0.

This might be correct ...
... but ONLY if you're ABSOLUTELY SURE "0" is the smallest possible value in your set. Otherwise, it's a bug.

3. Another approach, suggested above, was to initialize "max" to the smallest possible value, Double.NEGATIVE_INFINITY.

4. I would argue that a *better* approach ...
... is simply initialize "max" to the first element (e.g. "values[0]", or "values.get(0)").

IMHO ..
[ September 10, 2006: Message edited by: Paul Santa Maria ]
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ola rancheros!

Paul wrote

...let's assume that the container for "doubles", and the variable "x", are both defined and initialized elsewhere...


I think that's the point, because it is wrong.
The iterator must be declared in the for-each loop. So x cannot be declared elsewhere, the following snippet
will not compile due to the for loop.
In an "old" for loop it would work but not in a for-each loop.

So Victoria should put a double identifier into her loop.




Yours,
Bu.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Actually, "Double.MIN_VALUE" is not a better value to use than zero, since it is actually larger than zero!

I think you are thinking about using "-Double.MAX_VALUE" (negative max value) instead.

Henry
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry Wong wrote:

Actually, "Double.MIN_VALUE" is not a better value to use than zero, since it is actually larger than zero!

I think you are thinking about using "-Double.MAX_VALUE" (negative max value) instead.


Mea culpa. Actually I mixed it up with the MIN_VALUE constant of integer types. In contrast to Double and Float, there the min value really is negative.

That I posted nonsense with the Double.MIN_VALUE came to my mind on my sunday's bicycle tour. This shows again the importance of fresh air to the brain...


Yours,
Bu.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all your help. That was very helpful. At least I was on the right track. That makes me feel good. Maybe I am actually learning this stuff.
lol
 
reply
    Bookmark Topic Watch Topic
  • New Topic