edited by
16,810 views
54 votes
54 votes

Consider the following snippet of a C program. Assume that swap $(\&x, \&y)$ exchanges the content of $x$ and $y$:

int main () {
    int array[] = {3, 5, 1, 4, 6, 2};
    int done =0;
    int i;
    while (done==0) {
        done =1;
        for (i=0; i<=4; i++) {
            if (array[i] < array[i+1]) {
                swap(&array[i], &array[i+1]);
                done=0;
            }
        }
        for (i=5; i>=1; i--) {
            if (array[i] > array[i-1]) {
                swap(&array[i], &array[i-1]);
                done =0;
            }
        }
    }
    printf(“%d”, array[3]);
}

The output of the program is _______

edited by

7 Answers

Best answer
67 votes
67 votes

Well, the above program is sorting the array in descending order.

Initially, while loop starts execution by evaluating the iniatial condition

while(done==0)
 

For the first time the first for loop will be executed completey, the content of array will be as follows :

$5,3,4,6,2,1$

After the second for executed completey the content of array will be as follows:

$6,5,3,4,2,1$

But the value variable done is still $0$ so while loop will execute again,so now the content of array after executing the first for loop will be $6,5,4,3,2,1$ and no change in second for loop but still the done variable is $0$.

So, while loop execute again,now done variable is modified to $1$ and there will be no change in done variable because inside first and second for loop no if condition will satisfied .

Finally, the while condition is evaluted false and value of $array[3]$ will be printed which is $3$. 

edited by
11 votes
11 votes

when first for is executed then, array will be - 5,3,4,6,2,1

and when the second for loop is executed then, array will be - 6,5,3,4 2,1

but the execution of while loop is not terminated here, because the value of variable done is 0.

so the first for  loop is second time executed then the array will be - 6,5,4,3,2,1  . Till here the whole array is sorted in descending order so when the second for loop is second time executed then there will be no change in the array .

but the value of variable done is 0 now , so the while loop condition is true and while loop executes again but this time no any for loop condition will be true so the value of the variable done will be 1. The execution of while loop is termintes

so, array[3]= 3

3 votes
3 votes

Here's the code with a full explanation.

int main () {
    int array[] = {3, 5, 1, 4, 6, 2}; /*any array starts with 0 index*/
    int done =0;
    int i;
    /*
    This program actually sorted the array in descending order.
    */
    while (done==0) {
        done =1; /* It will break the while loop
                   if the value of done is unchanged  */

        /*
        This for loop below actually sorts the array from
        the index 0 to 4 in descending order.
        */
        for (i=0; i<=4; i++) {
            if (array[i] < array[i+1]) { 

            /*If the array becomes sorted in descending order,
              this condition will not hold.*/

                swap(&array[i], &array[i+1]);
                done=0;
            }
        }

        /*
        This for loop below actually sorts the array from
        the index 1 to 5 in descending order.
        */

        for (i=5; i>=1; i--) {
            if (array[i] > array[i-1]) {

            /*If the array becomes sorted in descending order,
              this condition will not hold.*/

                swap(&array[i], &array[i-1]);
                done =0;
            }
        }

        /*
        So when the array is fully sorted in descending order,
        the value of done remains 1. Thus it will break the while loop.
        Otherwise, the sorting continues.
        */
    }

    /*
    After finishing the while loop, the array becomes
    array[]={6,5,4,3,2,1}

    So array[0]=6, array[1]=5, array[2]=4,..., array[5]=1

    */
    printf(“%d”, array[3]); /*It will give the output as 3*/
}

So the correct answer is 3.

Answer:

Related questions

30 votes
30 votes
11 answers
1
Madhav asked Feb 14, 2017
9,539 views
Consider the following function implemented in C:void printxy(int x, int y) { int *ptr; x=0; ptr=&x; y=*ptr; *ptr=1; printf(“%d, %d”, x, y); }The output of invoking $...
36 votes
36 votes
6 answers
2
26 votes
26 votes
3 answers
3
khushtak asked Feb 14, 2017
5,766 views
Match the following:$$\begin{array}{|ll|ll|}\hline P. & \text{static char var ;} & \text{i.} & \text{Sequence of memory locations to store addresses} \\\hline Q. & \text...
41 votes
41 votes
4 answers
4
Arjun asked Feb 14, 2017
20,968 views
A message is made up entirely of characters from the set $X=\{P, Q, R, S, T\}$. The table of probabilities for each of the characters is shown below:$$\begin{array}{|c|c|...