Consider the following C-function in which and are two sorted integer arrays and be another integer array,
void xyz(int a[], int b [], int c []){
int i,j,k;
i=j=k=0;
while ((i<n) && (j<m))
if (a[i] < b[j]) c[k++] = a[i++];
else c[k++] = b[j++];
}
Which of the following condition(s) hold(s) after the termination of the while loop?
- and if
- and if
View worked solution
Answer: D
To determine which conditions hold after the termination of the while loop, we can analyze the loop variables , , and .
In every iteration of the loop, exactly one of or is incremented, and is always incremented. Since all three variables are initialized to , the loop invariant relation is:
Let's evaluate the two cases upon loop termination:
- Case 1: (and )
Substituting into our invariant gives . Statement (i) claims that , which is incorrect. - Case 2: (and )
Substituting into our invariant gives . Statement (ii) claims that , which is also incorrect.
Since the relation for is incorrect in both statements, neither condition (i) nor (ii) holds true upon termination.
Free practice · Your answers on this page are not saved.
