Results 1 to 9 of 9
  1. #1
    mr2insane is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default 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++)

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  4. #4
    Join Date
    May 2011
    Location
    Maryland
    Posts
    38
    Rep Power
    0

    Default

    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

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    "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.

  6. #6
    mr2insane is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    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.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by mr2insane View Post
    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.

  9. #9
    mr2insane is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 05-12-2011, 07:04 PM
  2. Array Problem?
    By noobgrammer in forum New To Java
    Replies: 4
    Last Post: 06-19-2010, 11:25 PM
  3. Array problem
    By binarzt in forum New To Java
    Replies: 5
    Last Post: 02-14-2010, 09:01 AM
  4. array problem
    By wats in forum New To Java
    Replies: 1
    Last Post: 12-12-2007, 07:08 AM
  5. array problem
    By Albert in forum Advanced Java
    Replies: 2
    Last Post: 07-01-2007, 01:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •