JavaBat AP-1 scoresIncreasing help http://codingbat.com/prob/p146974
Given an array of scores, return true if each score is equal or greater than the one before. The array will be length 2 or more. scoresIncreasing({1, 3, 4}) → true scoresIncreasing({1, 3, 2}) → false scoresIncreasing({1, 1, 4}) → true
This is what I currently have: public boolean scoresIncreasing(int[] scores) { int j = 0; while(j < scores.length - 1){ if(scores[j] >= scores[j - 1]){ return true; } }return false; } I am getting an out-of-bound error, and I can't find out why.
When you check the first element of the array you're then looking to (j-1) which is outside of the bounds of the array, forward. so position is 0 and you're trying to check position -1 which doesn't exist. Also j is never incremented.
backwards*
Thanks! That helped fixed the out-of-bounds problem. Now my code is like this: public boolean scoresIncreasing(int[] scores) { int j = 1; while(j < scores.length - 1){ if(scores[j] >= scores[j - 1]){ return true; } j++; }return false; } And the error now is:
@ganeshie8 I appreciate the answer, but I would like for someone to help me walk through these stuff so I can learn. Thanks! :D
ok. you're not checking all the scores, you returning just after comparing first two scores.
You'll also never reach the last element of the array because the check is if j is less than array.length - 1, a <= or a < array.length could corrcet that
Okay, I did. I'm still getting the same error though.
@ganeshie8 Can you explain what you mean? I didn't quite get that.
personally i would place another if inside the while, if (j = 0) then set another local integer variable to its value. Then you loop through and check [j] >= newInt, set [j] to newInt and continue the loop because we're true, else break because we're already les than the previous score.
public boolean scoresIncreasing(int[] scores) { int j = 0, k = -1; while (j < scores.length) { if (j == 0) { k = scores[j]; } else { if (j == (scores.length - 1)) { if (scores[j] >= k) { return true; } else { return false; } } else if (scores[j] >= k) { k = scores[j]; } else { return false; } } }
oops i never increment j so just add j++; before the while loops closing bracket
If the code is unclear I can explain.
Yes, please. :D
okay, the while loop condition was adjusted so that we can actually see the last element in the array. (scores.length -1) is the last element so being less than scores.length will allow us to reach (scores.length -1) element. We then check if our element is 0, or the first element (to prevent the first issue you were having with out of bounds) and set scores[0] to a temp variable k, and allow the loop to cycle. else we're not at scores[0] so we need to check some stuff before we can confirm anything further. Inside the else is more like your original code except we're checking a local variable k instead of the array element scores[j-1] directly. We first want to make sure we're not at the last element of the array (scores.length -1) if we are we see if this array element is greater than k (which reflects the array element before this one) if so return true. We've made it this far on true conditions so if the last element is also true we return true. else return false because our last element is lesser score in the array. if we're not at the last element in the array we simply check if scores[j]'s value is greater than local variable k, if it is this is a true condition so we set local variable k to scores[j]'s value and continue the loop. This allows us to check all array elements. If scores[j] isn't greater than local variable k return false. There isn't a need to continue looping because we a;ready know our array values continuously increasing. Then of course increment j, (which I forgot to do) so we're looking at the next array element and not doing the same check.
because we already know our array values aren't* continuously increasing
Ah, I get it now. Thanks for your hard work! I'm working on these problems to prepare for the AP exam, but these are so difficult. I wonder if I can get a good score... D:
You'll be just fine, it takes some trial and error before some of these things stick in your head. But when you see an error similar to this in the future you'll think to check if you're looking beyond the array or if it's something else. No one writes perfect code especially not while learning to code. I say just practice to gain that experience so you feel a little more comfortable. Good luck on your exam.
Thank you!
you're welcome
Join our real-time social learning platform and learn together with your friends!