The following code fragment is intended to find the smallest value in arr[0] . . . arr[n-1].
/** Precondition:
* - arr is an array, arr.length = n.
* - arr[0]...arr[n-1] initialized with integers.
* Postcondition: min = smallest value in arr[0]...arr[n-1].
*/
int min = arr[0];
int i = 1;
while (i < n)
{
i++;
if (arr[i] < min)
min = arr[i];
}
This code is incorrect. For the segment to work as intended, which of the following modifications could be made?
I Change the line
int i = 1;
to
int i = 0;
Make no other changes.
II Change the body of the while loop to
{
if (arr[i] < min)
min = arr[i];
i++;
}
Make no other changes.
III Change the test for the while loop as follows:
while (i <= n)
Make no other changes.
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III