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

1. ## 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. ## 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?

```            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. ## 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. ## Re: Another Array Problem

Originally Posted by dicdic
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?

```            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. ## 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. ## Re: Another Array Problem

Originally Posted by dicdic
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. ## 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. ## 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. ## 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. ## 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. ## 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. ## Re: Another Array Problem

Oh! So the string length would be 2?

and the character length would be 13?