3 Replies - 3699 Views - Last Post: 07 January 2010 - 12:08 PM Rate Topic: -----

#1 irv_the_swerve  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 03-November 09

Finding lowest non-zero number in an array

Post icon  Posted 07 January 2010 - 08:53 AM

Hey guys, hope you all had a good Christmas and new year :)

I have a quick (probably easy) question about reading values in an array...I've been working on a program for 6 hours today and my brain has suddenly deserted me :)

I want to basically print/select the lowest number in an array, which is non-zero.

For example if the array was:

X[5]

and the elements were:

[0 0 3 4 5]


I would like it to select the minimum (3 in this case) and enter it into (for example) minX.

Thanks B)

This post has been edited by irv_the_swerve: 07 January 2010 - 08:57 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Finding lowest non-zero number in an array

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2780
  • View blog
  • Posts: 11,767
  • Joined: 20-September 08

Re: Finding lowest non-zero number in an array

Posted 07 January 2010 - 09:07 AM

Sort the array, then scan it for first element > 0
Was This Post Helpful? 0
  • +
  • -

#3 DillonSalsman  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 144
  • Joined: 30-October 07

Re: Finding lowest non-zero number in an array

Posted 07 January 2010 - 11:56 AM

Well.. damn that was easy :)
My brain having not done anything productive this morning started to come up with a horrible solution.
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: Finding lowest non-zero number in an array

Posted 07 January 2010 - 12:08 PM

You don't even have to sort it...

int minX = Integer.MAX_VALUE;

for (int i = 0; i < X.length; i++)
{
    if (X[i] != 0 && X[i] < minX)
        minX = X[i];
}

This post has been edited by Locke: 07 January 2010 - 12:15 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1