# Help with Array problem!

• 06-10-2011, 04:19 AM
mr2insane
Help with Array problem!
I need some guidance again. I am given this problem to solve. Can someone please help me soon!?!

Quote:

Write a method named minGap that accepts an integer array as a parameter and returns the minimum 'gap' between adjacent values in the array. The gap between two adjacent values in a array is defined as the second value minus the first value. For example, suppose a variable called array is an array of integers that stores the following sequence of values.

int[] array = {1, 3, 6, 7, 12};
The first gap is 2 (3 - 1), the second gap is 3 (6 - 3), the third gap is 1 (7 - 6) and the fourth gap is 5 (12 - 7). Thus, the call of minGap(array) should return 1 because that is the smallest gap in the array. Notice that the minimum gap could be a negative number. For example, if array stores the following sequence of values:

{3, 5, 11, 4, 8}
The gaps would be computed as 2 (5 - 3), 6 (11 - 5), -7 (4 - 11), and 4 (8 - 4). Of these values, -7 is the smallest, so it would be returned.

This gap information can be helpful for determining other properties of the array. For example, if the minimum gap is greater than or equal to 0, then you know the array is in sorted (nondecreasing) order. If the gap is greater than 0, then you know the array is both sorted and unique (strictly increasing).

If you are passed an array with fewer than 2 elements, you should return 0.

This is what i got below.

Quote:

public static void minGap(int[] array) {

for (int i=0; i < (array.length); i++)
• 06-10-2011, 04:20 AM
Junky
and you want us to do the rest?

Before writing any code, grab a piece of paper and a pencil and work out how you would do it by hand.
• 06-10-2011, 04:24 AM
Fubarable
And also consider replying to the nice folks who tried to help you out in your previous thread: http://www.java-forums.org/new-java/...me-please.html. At least give thanks for their giving time to consider your problem. Thanks in advance.
• 06-10-2011, 04:28 AM
littledrummerboy
if you read the example it basically tells you exactly how to do it
all you need to do is subtract each value from the one before it and store it as a variable
then if the for loop ever reaches another value thats less than that one then it should change it to that value
• 06-10-2011, 04:30 AM
Junky
"If you are passed an array with fewer than 2 elements, you should return 0."

You can start with this requirement as it is very simple.
• 06-10-2011, 06:43 AM
mr2insane
Junky: I'm not asking for you to do this for me, I'm asking on some advice on how to get it solved as I did with the previous time I asked about binary numbers. I'm having difficulties on forming a code to compute the gaps. I know I have to add indexes 0 &1 , 1 & 2, 2 & 3, ect...., but am I supposed to create another array to assign the computed gaps into it?

Fubarable: I guess I edited the wrong thread... Need some help!!! I apologize if you feel unthankful for helping me out in my last thread. I really do appreciate all for the help.
• 06-10-2011, 06:51 AM
Junky
You only need 2 variables: the smallest gap so far and the current gap. Each time you calculate a new current gap compare it to the smallest so far to see if it is the new smallest.
• 06-10-2011, 12:57 PM
Fubarable
Quote:

Originally Posted by mr2insane
Fubarable: I guess I edited the wrong thread... Need some help!!! I apologize if you feel unthankful for helping me out in my last thread. I really do appreciate all for the help.

I'm not talking about my reply which was nothing but a little house-keeping, but rather Norm and pbrockway's replies. Again we/they would appreciate it if you reply in that thread with a thanks for their help, that's all. That's how folks are "paid" in here.
• 06-17-2011, 03:49 AM
mr2insane
Thanks again guys... this helped me out alot! :(y):