1.
Q24: What does the following code accomplish? Assume that
alpha
is a correctly declared array with non-default values and that the variable target
contains a value.int place = -1;
int index = 0;
for (int x : alpha) {
if (x == target)
place = index;
index += 1;
}
place
contains the index of the first occurrence oftarget
withinalpha
, -1 otherwiseplace
contains the index of the last occurrence oftarget
withinalpha
, -1 otherwiseplace
always contains -1- the code "finds"
target
withinalpha
- the code does not work because you can’t get the index with an enhanced for loop