Control Flow & Loop Tracing
![Question 42, GATE 2014: A sorted ascending array listA has n>0 entries. ProcessArray sets i=0,j=n−1, then repeatedly sets k=(i+j)/2 using integer division; if x≤listA[k], sets j=k−1; independently, if listA[k]≤x, sets i=k+1; repeats while i≤j. Afterward returns k if listA[k]==x, else −1. Which is correct? A absent x causes an infinite loop; B this is binary search; C it always finds the maximum; D it returns −1 even when x is present.](../../../assets/questions/CS-2014-S3-Q52-1.webp)
Answer & solution
Official answer: B
Step 1: If x is smaller than the midpoint, only the first comparison succeeds, eliminating the midpoint and upper half. If x is larger, only the second succeeds, eliminating the lower half. Each unsuccessful iteration strictly shrinks the candidate interval. Step 2: If x equals the midpoint, both independent tests succeed: j=k−1 and i=k+1. The loop terminates immediately, and the final equality test returns the matching k. Step 3: Therefore it implements binary search, B, including for duplicates where any matching index suffices. Assume a valid nonempty sorted array and arithmetic without overflow in i+j.
![Question 84, GATE 2008: This erroneous historical C binary-search function uses integer division:
1. f(int Y[10], int x) {
2. int i,j,k;
3. i=0; j=9;
4. do {
5. k=(i+j)/2;
6. if(Y[k]<x) i=k; else j=k;
7. } while((Y[k]!=x) && (i<j));
8. if(Y[k]==x) printf("x is in the array");
9. else printf("x is not in the array");
10. }
For which input does it fail?
(A) Y=[1,2,3,4,5,6,7,8,9,10], x<10
(B) Y=[1,3,5,7,9,11,13,15,17,19], x<1
(C) Y=[2,2,2,2,2,2,2,2,2,2], x>2
(D) Y=[2,4,6,8,10,12,14,16,18,20], 2<x<20 and x even](../../../assets/questions/CS-2008-Q84-1.webp)
![Question 84, GATE 2008: This erroneous historical C binary-search function uses integer division:
1. f(int Y[10], int x) {
2. int i,j,k;
3. i=0; j=9;
4. do {
5. k=(i+j)/2;
6. if(Y[k]<x) i=k; else j=k;
7. } while((Y[k]!=x) && (i<j));
8. if(Y[k]==x) printf("x is in the array");
9. else printf("x is not in the array");
10. }
For which input does it fail?
(A) Y=[1,2,3,4,5,6,7,8,9,10], x<10
(B) Y=[1,3,5,7,9,11,13,15,17,19], x<1
(C) Y=[2,2,2,2,2,2,2,2,2,2], x>2
(D) Y=[2,4,6,8,10,12,14,16,18,20], 2<x<20 and x even](../../../assets/questions/CS-2008-Q84-2.webp)
![Question 85, GATE 2008: This erroneous historical C binary-search function uses integer division:
1. f(int Y[10], int x) {
2. int i,j,k;
3. i=0; j=9;
4. do {
5. k=(i+j)/2;
6. if(Y[k]<x) i=k; else j=k;
7. } while((Y[k]!=x) && (i<j));
8. if(Y[k]==x) printf("x is in the array");
9. else printf("x is not in the array");
10. }
Which correction makes the program work properly?
(A) Replace line 6 with if(Y[k]<x) i=k+1; else j=k−1;
(B) Replace line 6 with if(Y[k]<x) i=k−1; else j=k+1;
(C) Replace line 6 with if(Y[k]<=x) i=k; else j=k;
(D) Replace line 7 with } while((Y[k]==x) && (i<j));](../../../assets/questions/CS-2008-Q85-1.webp)
![Question 85, GATE 2008: This erroneous historical C binary-search function uses integer division:
1. f(int Y[10], int x) {
2. int i,j,k;
3. i=0; j=9;
4. do {
5. k=(i+j)/2;
6. if(Y[k]<x) i=k; else j=k;
7. } while((Y[k]!=x) && (i<j));
8. if(Y[k]==x) printf("x is in the array");
9. else printf("x is not in the array");
10. }
Which correction makes the program work properly?
(A) Replace line 6 with if(Y[k]<x) i=k+1; else j=k−1;
(B) Replace line 6 with if(Y[k]<x) i=k−1; else j=k+1;
(C) Replace line 6 with if(Y[k]<=x) i=k; else j=k;
(D) Replace line 7 with } while((Y[k]==x) && (i<j));](../../../assets/questions/CS-2008-Q85-2.webp)
![Question 85, GATE 2008: This erroneous historical C binary-search function uses integer division:
1. f(int Y[10], int x) {
2. int i,j,k;
3. i=0; j=9;
4. do {
5. k=(i+j)/2;
6. if(Y[k]<x) i=k; else j=k;
7. } while((Y[k]!=x) && (i<j));
8. if(Y[k]==x) printf("x is in the array");
9. else printf("x is not in the array");
10. }
Which correction makes the program work properly?
(A) Replace line 6 with if(Y[k]<x) i=k+1; else j=k−1;
(B) Replace line 6 with if(Y[k]<x) i=k−1; else j=k+1;
(C) Replace line 6 with if(Y[k]<=x) i=k; else j=k;
(D) Replace line 7 with } while((Y[k]==x) && (i<j));](../../../assets/questions/CS-2008-Q85-3.webp)
![Question 18, GATE 1996: For N>1, the following Pascal-like program searches an array a[1..N]: initialize i=1,j=N; repeatedly set k=floor((i+j)/2), then if a[k]<x set i=k, otherwise set j=k; stop when a[k]=x or i>=j. Finally print success if a[k]=x and failure otherwise. Determine when it fails. For the intended binary-search interpretation assume a is sorted in nondecreasing order. GFG prints the success message correctly; GateForum instead prints failure in both final branches.](../../../assets/questions/CS-1996-Q80-1.webp)
![Question 18, GATE 1996: For N>1, the following Pascal-like program searches an array a[1..N]: initialize i=1,j=N; repeatedly set k=floor((i+j)/2), then if a[k]<x set i=k, otherwise set j=k; stop when a[k]=x or i>=j. Finally print success if a[k]=x and failure otherwise. Determine when it fails. For the intended binary-search interpretation assume a is sorted in nondecreasing order. GFG prints the success message correctly; GateForum instead prints failure in both final branches.](../../../assets/questions/CS-1996-Q80-2.webp)