The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+25 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]);
        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 _______

in Programming by Veteran (416k points)
edited by | 4.7k views
My answer was 4 in this ques. someone confirm??
answer is 3.. i was wrong. the trick was that u have to run that loop again which i had not done as when you are running that 2nd for loop , it will make done equals 0 and then that while loop will again run. i think whosoever have written this ques. answer as 4, most of them would have done this mistake. one more wrong. it was an easy ques., feeeling disappointed. :(
Code mentioned in the question is a variant of Bubble Sort.
int array[] = \{3, 5, 1, 4, 6, 2};

some one correct it.

very true @mohit

I also did the same mistake and so did the most of us, because as soon as we start doing loop execution through pen and paper we think that 'I got this question and it's so easy'. but we tend to forgot the little things (which are indeed converted into the huge blunder obviously) and lost good 1 or 2 marks just because of laziness :(
Someone correct the question ...

6 Answers

+38 votes
Best answer

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

Initially, while loop starts execution by evaluating the iniatial condition


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


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


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$. 

by Boss (38.3k points)
edited by
nice explanation @manojk and thanks for your help

1 correction needed

swap(&array[i],& array[i-1])
I did a very little mistake and get an answer $4$.Because I run while loop only once.

Actually, while loop run $3$ times($1$st for loop $+ 2$nd for loop), $4$th time when done$=1$,condition false.
Other way to look at this problem

variable done is set to 0 initially and once we enter into while loop it is 1. Now While loop will be executing till done is 0. Snippet responsible for making our done =1 to 0 are two if conditions i.e when they are always false done will never become 0 and both the conditions are violated simultaneously when array is in descending order.
+8 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

by Active (2.2k points)
0 votes


output is 3

by (101 points)
0 votes

3 is the answer.

I just ran the code here:

by Active (2.5k points)
0 votes

answer is 3

by (221 points)
–1 vote
Is it 1?
by (153 points)

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
49,807 questions
54,713 answers
79,700 users