Reputation: 379
I would like to write a program which adds up all the numbers in an integer array - with an exception! Since the number 6 isn't the nicest, I propose that we exclude all sections of numbers beginning with a 6 and ending with a 7 - inclusive. Every 6 will always be followed by a 7, but not necessarily vice versa.
Here are a few examples of the input arrays and their expected output:
sum67([1, 2, 2, 6, 99, 99, 7]) = 5
All numbers between a 6 and a 7 are excluded.
sum67([1, 2, 2]) = 5
No 6's or 7's here.
sum67([1, 1, 6, 7, 2]) → 4
Neither the 6 or the 7 is included.
All the above tests passed.
Once again, the method header is fixed, and I may only change the body of the method. Here is my attempt at the code:
public int sum67(int[] nums) {
int sum = 0;
for (int i = 0; i < nums.length; i++) {
// Adding all numbers that are not a 6
if (nums[i] != 6) sum += nums[i];
}
for (int j = 0; j < nums.length; j++) {
// check for the sixes - the lower bound exclusive
if (nums[j] == 6) {
for (int k = j + 1; k < nums.length; k++) {
// check for the sevens - the upper bound inclusive
if (nums[k] == 7) {
// take away all the numbers between the 2 bounds, including the 7
for (int m = j + 1; m <= k; m++) {
sum -= nums[m];
}
}
}
}
}
return sum;
}
The above program not only does not work, but is clearly extremely messy. In particular, it fails the following tests, among others:
sum67([1, 6, 2, 6, 2, 7, 1, 6, 99, 99, 7]) = 2
Actual output is -463
!
sum67([2, 2, 6, 7, 7]) = 11
Actual output is -3
.
So essentially:
Where is the error line or lines in my code?
Is there a better way of writing this program that doesn't have as many loops and nested if
s?
Upvotes: 0
Views: 172
Reputation: 272845
public int sum67(int[] arr) {
int sum = 0;
boolean isIn67 = false;
for (int i = 0 ; i < arr.length ; i++) {
if (arr[i] == 6) {
isIn67 = true;
continue;
} else if (arr[i] == 7 && isIn67) {
isIn67 = false;
continue;
}
if (!isIn67) {
sum += arr[i];
}
}
return sum;
}
The above is my attempt. Explanation:
isIn67
that indicates whether arr[i]
is inside a 6-7 range and so shouldn't be added to the sumisIn67
to true.isIn67
is true, we set isIn67
to false.continue
is used so that we don't count those 7sin67
, we add the element to the sum.The problem with your code is probably that you are nesting too much. The line to subtract the unwanted numbers is nested in the outer loop so it might get executed for many more times than expected, making the results negative.
Upvotes: 2
Reputation: 25980
The logic you are using is not representative of what you want to do. Take your first example, and consider the second loop (so you have summed up everything)
<=
- this is the reason for the -3
in the second example)I suggest following your code with pen and paper to see how it works. A simpler solution is to do exactly what you want:
int sum = 0;
boolean noLikey = false;
for (int i = 0; i < nums.length; i++) {
if (! noLikey && nums[i] == 6 ) noLikey = true;
if ( noLikey ) {
if ( nums[i] == 7 ) noLikey = false;
continue;
}
sum += nums[i];
}
This of course assumes a 6
is always followed by a 7.
Upvotes: 1