Solving arrays – problem #1: Closest to zero

I have been reviewing arrays for an exam. What better way than to solve array exercises.  This is one of the many that I’ve finished. Elementary, you might think, but I’ve been out of this kind of programming for a while.

For this exercise, it’s about looking for the number closest to zero. Since the original problem (the first 3 lines commented) doesn’t state any other conditions like, “in case there is more than one number closest to zero, return the first element found that is closest” or “check an element in the array with a value of zero then stop”, let’s leave that off for another solution. I won’t be using Math functions in Java too since that would defeat the purpose of the exercise.

Feel free to comment for a simpler answer or if there are mistakes in the solution.

/*
Complete the following program so that it computes and writes out the element in the array that is closest to zero.
*/

class ClosestToZero
{

public static void main ( String[] args )  {

// test data

int[] data = {3, 5, -2, 7, 4, 12, -3, 2, 8, -5};

// declare and initialize variables

int curr = 0; // Just a variable to hold the square of the current index in the loop
int near = data[0]; // Current closest to zero. Assume it’s first element of the array in the beginning.

// find the element nearest to zero

for ( int index=0; index < data.length; index++ ) {

curr = data[index] * data[index]; // We square the values to eliminate negative numbers

System.out.print( curr  + ” or “ + near + “? “ );

if ( curr <= (near * near) )  { // What happens if we just use ‘<’ instead of ‘<=’ ?

near = data[index];

} System.out.println( near );

}

// write out the element closest to zero
System.out.println( near );

}

}

Output of the program:

3 or 3? 3
5 or 3? 3
-2 or 3? -2
7 or -2? -2
4 or -2? -2
12 or -2? -2
-3 or -2? -2
2 or -2? 2
8 or 2? 2
-5 or 2? 2
Closest to zero: 2

The number closest to zero (in terms of distance) is always the number with the least value. True? YES and NO.

NO because, -2 is less than 2. However, both are of the same distance to zero.

Since this is about distance not direction, let’s get rid of negative values.  Square the value to get a positive number in case there are negative numbers in the array. Absolute would have been the choice but no Math functions.

So YES, if we take the absolute value of each number, the number closest to zero is the one with the least value in the array.

Related Posts:

7 Responses to “Solving arrays – problem #1: Closest to zero”

  1. I was actually a bit confused by all this, but thanks for taking the time to explain anyway. It was quite well written :)

  2. Slightly off topic, but i am looking for a few webmasters, programmers etc to help test a free webmaster tool we built, figured i might find a few commenting in here. Hope admins dont mind. Thx Garfield

  3. Hey there, You have done a great job. I will definitely digg it and personally suggest to my friends. I am sure they’ll be benefited from this web site.

  4. Ok I am using a RSS feed thingy on windows 7 but how can I get your feed? When I click on the RSS icon thing I get only like halfe articles, I can’t read that on my desktop. I would have to go to your site every single time!

    • That’s the whole point of the RSS feed – you don’t get everything unless you visit the origin of the feed.

  5. clipless pedals
    May 23, 2011 at 2:20 pm » Reply

    What service do you use for RSS feed? Is it feedburner? I ask because I cant figure out how to install a RSS service on my Blogger blog. I wouldn’t get any of that techy stuff if my life would depent on it :s So if you use a RSS feed service please let me know. Thanks! Jen

    • RSS feed? What RSS feed are you talking about? There’s none of that. I’m using Ridonculous Spamming Software to “broadcast” the posts. ;)

You Comment, I Follow

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>