Write method findZero, as started below. findZero should return the index of the first element of array A that contains the value zero, starting from position 0 up through position pos. If no element of A from position 0 to position pos contains the value zero, then findZero should return –1. For example: Array (A) Position (pos) Value returned by findZero(A, pos) 1 0 2 5 6 0 -1 1 0 2 5 6 1 1 1 0 2 5 6 2 1 1 0 2 0 6 2 1 1 0 2 0 6 4 1 1 2 3 4 5 4 -1 Complete method findZero below. Assume that it is called only with values that satisfy its assumptions as stated below. // Description: Returns smallest index k // such that (0 <= k <= pos) and (A[k] == 0) // Assumptions: 0 <= pos < A.length public int findZero(int[] A, int pos) {
Write method findZero, as started below. findZero should return the index of the first element of array A that contains the value zero, starting from position 0 up through position pos. If no element of A from position 0 to position pos contains the value zero, then findZero should return –1. For example:
Array (A) |
Position (pos) |
Value returned by findZero(A, pos) |
1 0 2 5 6 |
0 |
-1 |
1 0 2 5 6 |
1 |
1 |
1 0 2 5 6 |
2 |
1 |
1 0 2 0 6 |
2 |
1 |
1 0 2 0 6 |
4 |
1 |
1 2 3 4 5 |
4 |
-1 |
Complete method findZero below. Assume that it is called only with values that satisfy its assumptions as stated below.
// Description: Returns smallest index k
// such that (0 <= k <= pos) and (A[k] == 0)
// Assumptions: 0 <= pos < A.length
public int findZero(int[] A, int pos) {
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images