• Post Reply Bookmark Topic Watch Topic
  • New Topic

array algorithm  RSS feed

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • 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.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Paul Santa Maria
Ranch Hand
Posts: 236
  • Mark post as helpful
  • send pies
  • 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 ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!