Results 1 to 9 of 9
Thread: Help with Array problem!
 06102011, 04:19 AM #1Member
 Join Date
 Jun 2011
 Posts
 6
 Rep Power
 0
Help with Array problem!
I need some guidance again. I am given this problem to solve. Can someone please help me soon!?!
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.
public static void minGap(int[] array) {
for (int i=0; i < (array.length); i++)
 06102011, 04:20 AM #2
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.

And also consider replying to the nice folks who tried to help you out in your previous thread: Help me please!. At least give thanks for their giving time to consider your problem. Thanks in advance.
 06102011, 04:28 AM #4Member
 Join Date
 May 2011
 Location
 Maryland
 Posts
 38
 Rep Power
 0
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
 06102011, 04:30 AM #5
"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.
 06102011, 06:43 AM #6Member
 Join Date
 Jun 2011
 Posts
 6
 Rep Power
 0
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.
 06102011, 06:51 AM #7
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.

 06172011, 03:49 AM #9Member
 Join Date
 Jun 2011
 Posts
 6
 Rep Power
 0
Similar Threads

Display Array on another class after extracting from txt file and into array problem
By jonathan920 in forum NetBeansReplies: 0Last Post: 05122011, 07:04 PM 
Array Problem?
By noobgrammer in forum New To JavaReplies: 4Last Post: 06192010, 11:25 PM 
Array problem
By binarzt in forum New To JavaReplies: 5Last Post: 02142010, 10:01 AM 
array problem
By wats in forum New To JavaReplies: 1Last Post: 12122007, 08:08 AM 
array problem
By Albert in forum Advanced JavaReplies: 2Last Post: 07012007, 01:13 AM
Bookmarks