Home » Programming & Data Structure » Programming and data structure miscellaneous » Question

Programming and data structure miscellaneous

Programming & Data Structure

  1. Consider the following snippet of a C program. Assume that swap (&x, &y) exchanges the contents 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>=l; 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 _______.
    1. 0
    2. 1
    3. 2
    4. 3
Correct Option: D

The initial contents of the Array is :-

For first loop : i very from (0 to 4), then the contents of the Array are :

For second loop : i very from (5 to 1) then the contents of the Array are :

Now, since done = 0, then the for loops will execute again.
For first loop : again i is very from 0 to 4, then the contents of the Array are :

For second loop : Again i is very from (5 to 1) then the contents of the array are :

The value of done is still "zero". Hence the for loop will execute again.
First for loop :
This time there will be no change by the for loop.
Then the value of done is '1', hence the loop terminate here, then the final contents of the Array are :

Hence, the output of the program array [3] is '3'. Hence answer is 3.



Your comments will be displayed only after manual approval.