Question: 2. A loop invariant is a statement that is true before a loop begins, and at the end of each run through the loop.
2. A loop invariant is a statement that is true before a loop begins, and at the end of each run through the loop. Consider the following method to find the index of the smallest entry of an array. (We can assume that the length of arr is at least 1) public static int findMin Index (int[] arr) { int cuMin = arr[0]; int curIndex = 0; for (int i=1; i < arr.length; i++){ if (arr[i] < cur_min) { curMin = arr[i]; curIndex = i; } } return curIndex; } Please explain why the following is a loop invariant: After i steps through the loop, curMin is the minimum value of the array arr from index 0 to index i, and curIndex contains the index of that minimum value. D
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
