+25 votes
4.7k views

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 | 4.7k views
0
My answer was 4 in this ques. someone confirm??
+5
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. :(
+1
Code mentioned in the question is a variant of Bubble Sort.
0
int array[] = \{3, 5, 1, 4, 6, 2};

some one correct it.

+1
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 :(
0
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

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

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

1 correction needed

swap(&array[i],& array[i-1])
+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.
0
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

the

output is 3

by (101 points)
0 votes

3 is the answer.

I just ran the code here:

http://ideone.com/cp2VkX

by Active (2.5k points)
0 votes

answer is 3

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

+18 votes
3 answers
1
+17 votes
5 answers
2
+14 votes
11 answers
3
+14 votes
5 answers
4
+31 votes
8 answers
5
+15 votes
7 answers
6
+58 votes
14 answers
7