Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Another Array Problem

  1. #1
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Another Array Problem

    Assignment:
    Given an array of scores sorted in increasing order, return true if the array contains 3 adjacent scores that differ from each other by at most 2, such as with {3, 4, 5} or {3, 5, 5}.

    scoresClump({3, 4, 5}) → true
    scoresClump({3, 4, 6}) → false
    scoresClump({1, 3, 5, 5}) → true

    Codingbat AP-1 3rd question

    public boolean scoresClump(int[] scores) {
     
     
       for (int i = 0; i < scores.length; i++) {
                int a = scores[i];
                int b = scores[i + 1];
                int c = scores[i + 2];
       if (Math.abs(b-a) + Math.abs (c-b) <=2)
                    return true;
     
      }
      return false;
    }

    I got it right for some of the input, but not one of them. I tried to use the for loop and if statement on a specific input that I got wrong:

    {4, 5, 8}
    scores[1] - scores[0] = 1
    scores[2] - scores[1] = 3

    I suspect it has something to do with the for loop, but I don't see the problem with it. It should work, shouldn't it?
    But anyway, here is the error for {4,5,8} :

    Exception:java.lang.ArrayIndexOutOfBoundsException : 3 (line number:7)


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Another Array Problem

    how come that scoresClump({3, 4, 6}) is false? it must true right? because 4 is differ from 6 by 2 just like in scoresClump({1, 3, 5, 5}) → true because 3 is differ from 5 by 2?

    in your statement:
                int a = scores[i];
                int b = scores[i + 1];
                int c = scores[i + 2];
    how can it handle array with length greater than 3 or less than 3?
    you might end up with ArrayIndexOutOfBoundsException

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,039
    My Mood
    Mellow
    Thanks
    184
    Thanked 647 Times in 634 Posts

    Default Re: Another Array Problem

    Imagine you have a moving 'window' that considers 3 elements of the array at a time. The top or right side of the window can never be greater than the last element of the array, and the bottom or left side of the window includes the two elements to the left of the last element in the window but can never be less than the first element of the array. Set up your for loop to control the window within these boundaries.

    Varying the loop control variable (in this case 'i') inside the loop body is always risky and must be done with great care to ensure that it stays within the array boundaries.

  4. #4
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Another Array Problem

    Quote Originally Posted by dicdic View Post
    how come that scoresClump({3, 4, 6}) is false? it must true right? because 4 is differ from 6 by 2 just like in scoresClump({1, 3, 5, 5}) → true because 3 is differ from 5 by 2?

    in your statement:
                int a = scores[i];
                int b = scores[i + 1];
                int c = scores[i + 2];
    how can it handle array with length greater than 3 or less than 3?
    you might end up with ArrayIndexOutOfBoundsException
    No, scoresClumb{3,4, 6} is false. 6-4=2 and 4-3=1. 2+1=3. 3>2, so it has to be false.

    Imagine you have a moving 'window' that considers 3 elements of the array at a time. The top or right side of the window can never be greater than the last element of the array, and the bottom or left side of the window includes the two elements to the left of the last element in the window but can never be less than the first element of the array. Set up your for loop to control the window within these boundaries.

    Varying the loop control variable (in this case 'i') inside the loop body is always risky and must be done with great care to ensure that it stays within the array boundaries.
    Mhm... After looking at the loop over and over again, I still see nothing wrong with the loop. Shouldn't the loop stop once it hits the scores.length? The code only seems to get an error when they output should be false...I do not understand. It seems the loop just won't stop after it does not meet the if command?

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Another Array Problem

    No, scoresClumb{3,4, 6} is false. 6-4=2 and 4-3=1. 2+1=3. 3>2, so it has to be false.
    are you sure?

    so how come that scoresClump({1, 3, 5, 5}) → true ?
    because 5-3 =2, 3-1 = 2, and 2+2 = 4 which is greater than 2?
    so how come that scoresClump({1, 3, 5, 5}) is true?
    your requirement is confusing. are you sure you get the right requirement for that activity?

  6. #6
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Another Array Problem

    Quote Originally Posted by dicdic View Post
    are you sure?

    so how come that scoresClump({1, 3, 5, 5}) → true ?
    because 5-3 =2, 3-1 = 2, and 2+2 = 4 which is greater than 2?
    so how come that scoresClump({1, 3, 5, 5}) is true?
    your requirement is confusing. are you sure you get the right requirement for that activity?
    The assignment says: "return true if the array contains 3 adjacent scores that differ from each other by at most 2"

    5-5=0, 5-3=2 2<3 so it's true.

  7. #7
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 63 Times in 61 Posts

    Default Re: Another Array Problem

    okay, i get it now. sorry for confusion.
    did you figured it out?
    you are going to need a loop with that problem.
    an outer loop and inner loop (nested loop).
    and I guess outer loop must loop (aray.length - 2) times.
    and inner loop must loop only 3 times.

  8. #8
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Another Array Problem

    Well, I just guessed and inputted random for parameters and managed to get the answer correct with:

    for (int i = 0; i <= scores.length-3; i++)

    Not sure why though.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,039
    My Mood
    Mellow
    Thanks
    184
    Thanked 647 Times in 634 Posts

    Default Re: Another Array Problem

    After looking at the loop over and over again, I still see nothing wrong with the loop.
    Then I think you have a misunderstanding about how loops work. For example, will the following cause an "array index out of bounds" error?
    String[] myArray = { "GregBrannon" };
    for ( int i = 0 ; i < myArray.length ; i++ )
    {
        i = 10;
     
        System.out.println( myArray[i] );
    }
    The answer is yes! If the loop control variable is modified outside the loop condition, the condition will not evaluate the variable to determine whether the loop body should continue until the NEXT time through the loop.

  10. #10
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Another Array Problem

    But myArray.length is 11 though

    so i=10;

    for int i=0; 10<11; i++

    so it should run again right? There shouldn't be an error.

  11. #11
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Another Array Problem

    myArray in GregBrannon's examply is not 11. When you get .length of an array you are getting the amount of elements found in the array. The elements in his array happens to be Strings not Characters.
    int[] i = {0, 4, 5, 29}; // 4 ints
    double[] d = {0.2, 0.3, 1.8, -28.39, 0.0002}; // 5 doubles
    Character[] c = {'h', 'k', 'f', 'r', 'e', 'n', 'c', 'h', 't', 'o', 'a', 's', 't'}; // What's the length?
    String[] s = {"hkfrenchtoast", "GregBrannon"}; // What's the length?

  12. #12
    Member
    Join Date
    Feb 2014
    Posts
    38
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Another Array Problem

    Oh! So the string length would be 2?

    and the character length would be 13?

Similar Threads

  1. Replies: 2
    Last Post: February 24th, 2014, 09:48 PM
  2. 2D array problem
    By tylin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 25th, 2013, 12:50 PM
  3. Array Problem
    By av8 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 27th, 2011, 07:28 AM
  4. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM