Tracing alternating decimal-digit extraction
Control Flow & Loop Tracing

Answer & solution
Official answer: 46
Approach: The first loop finds the highest decimal place value. The second consumes digits from left to right and retains alternating positions.
For n=14362, t grows through 1,10,100,1000,10000,100000. The condition then fails and t is divided by 10, giving 10000.
The digit iterations are: d=1, remainder 4362, turn=0, so skip; d=4, remainder 362, turn=1, so newnum=4; d=3, remainder 62, turn=0, so skip; d=6, remainder 2, turn=1, so newnum=10×4+6=46; d=2, remainder 0, turn=0, so skip. The place value becomes zero and the loop ends.
The program therefore prints 46, the digits in positions two and four from the left. The alternating flag is tested before it is toggled, so the first digit is skipped. For this concrete input all arithmetic fits in int; possible overflow for much larger arguments is irrelevant to this execution.



![Question 48, GATE 2021: ANSI C function: int SimpleFunction(int Y[],int n,int x){int total=Y[0],loopIndex; for(loopIndex=1;loopIndex<=n-1;loopIndex++) total=x*total+Y[loopIndex]; return total;} Array Z has ten elements, all equal to 1. Find SimpleFunction(Z,10,2).](../../../assets/questions/CS-2021-S1-Q58-1.webp)
![Question 48, GATE 2020: Find pp(3,4) for these C functions:
int tob(int b,int *arr) {
int i;
for(i=0;b>0;i++) {
if(b%2) arr[i]=1; else arr[i]=0;
b=b/2;
}
return i;
}
int pp(int a,int b) {
int arr[20]; int i,tot=1,ex,len;
ex=a; len=tob(b,arr);
for(i=0;i<len;i++) {
if(arr[i]==1) tot=tot*ex;
ex=ex*ex;
}
return tot;
}](../../../assets/questions/CS-2020-Q58-1.webp)
![Question 48, GATE 2020: Find pp(3,4) for these C functions:
int tob(int b,int *arr) {
int i;
for(i=0;b>0;i++) {
if(b%2) arr[i]=1; else arr[i]=0;
b=b/2;
}
return i;
}
int pp(int a,int b) {
int arr[20]; int i,tot=1,ex,len;
ex=a; len=tob(b,arr);
for(i=0;i<len;i++) {
if(arr[i]==1) tot=tot*ex;
ex=ex*ex;
}
return tot;
}](../../../assets/questions/CS-2020-Q58-2.webp)



![Question 34, GATE 2016: Fill the loop condition so this C function returns the maximum array element for all n≥1:
int max(int *p,int n){int a=0,b=n−1;while(____){if(p[a]<=p[b])a=a+1;else b=b−1;}return p[a];}
A: a!=n. B: b!=0. C: b>(a+1). D: b!=a.](../../../assets/questions/CS-2016-S1-Q44-1.webp)



![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)


![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 53, GATE 2006: Sorted integer arrays a[0…n−1],b[0…m−1] have positive lengths. Array c has capacity n+m. Execute:
i=j=k=0;
while (i<n && j<m) {
if (a[i]<b[j]) c[k++]=a[i++];
else c[k++]=b[j++];
}
Which printed postcondition is valid?
I: if i=n, then j<m, k=n+j−1, and a[n−1]<b[j].
II: if j=m, then i<n, k=m+i−1, and b[m−1]≤a[i].
The −1 terms are retained exactly as reproduced in both sources.
Options:
(A) Only I
(B) Only II
(C) Either I or II, but not both
(D) Neither I nor II](../../../assets/questions/CS-2006-Q53-1.webp)
![Question 53, GATE 2006: Sorted integer arrays a[0…n−1],b[0…m−1] have positive lengths. Array c has capacity n+m. Execute:
i=j=k=0;
while (i<n && j<m) {
if (a[i]<b[j]) c[k++]=a[i++];
else c[k++]=b[j++];
}
Which printed postcondition is valid?
I: if i=n, then j<m, k=n+j−1, and a[n−1]<b[j].
II: if j=m, then i<n, k=m+i−1, and b[m−1]≤a[i].
The −1 terms are retained exactly as reproduced in both sources.
Options:
(A) Only I
(B) Only II
(C) Either I or II, but not both
(D) Neither I nor II](../../../assets/questions/CS-2006-Q53-2.webp)



![Question 88, GATE 2003: In this C fragment j,k,n,TwoLog_n are integers, A is an integer array and n≥3. GFG defines TwoLog_n=2 floor(log2 n); GateForum instead uses 2 ceil(log2 n).
for(k=3;k<=n;k++) A[k]=0;
for(k=2;k<=TwoLog_n;k++)
for(j=k+1;j<=n;j++)
A[j]=A[j] || (j%k);
for(j=3;j<=n;j++)
if(!A[j]) printf("%d ",j);
What set is printed?
A. {m:m≤n and m=i! for some integer i}
B. {m:m≤n and m=i² for some integer i}
C. {m:m≤n and m is prime}
D. ∅](../../../assets/questions/CS-2003-Q88-1.webp)
![Question 88, GATE 2003: In this C fragment j,k,n,TwoLog_n are integers, A is an integer array and n≥3. GFG defines TwoLog_n=2 floor(log2 n); GateForum instead uses 2 ceil(log2 n).
for(k=3;k<=n;k++) A[k]=0;
for(k=2;k<=TwoLog_n;k++)
for(j=k+1;j<=n;j++)
A[j]=A[j] || (j%k);
for(j=3;j<=n;j++)
if(!A[j]) printf("%d ",j);
What set is printed?
A. {m:m≤n and m=i! for some integer i}
B. {m:m≤n and m=i² for some integer i}
C. {m:m≤n and m is prime}
D. ∅](../../../assets/questions/CS-2003-Q88-2.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)





