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.
May 6, 2011 at 5:00 pm »
I was actually a bit confused by all this, but thanks for taking the time to explain anyway. It was quite well written
May 8, 2011 at 12:34 am »
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
May 8, 2011 at 7:42 pm »
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.
May 22, 2011 at 9:12 am »
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!
May 24, 2011 at 1:19 am »
That’s the whole point of the RSS feed – you don’t get everything unless you visit the origin of the feed.
May 23, 2011 at 2:20 pm »
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
May 24, 2011 at 1:21 am »
RSS feed? What RSS feed are you talking about? There’s none of that. I’m using Ridonculous Spamming Software to “broadcast” the posts.