GATE CS 2017 (Afternoon) — Question 43

Programming in CFunctions, Parameter Passing, Scope Rules, RecursionNAT2 marksMedium

Consider the following snippet of a C program. Assume that swap (&x, &y) exchanges the contents of x and y.
c
1int main() {
2    int array[] = {3, 5, 1, 4, 6, 2};
3    int done = 0;
4    int i;
5    while (done == 0) {
6        done = 1;
7        for (i = 0; i <= 4; i++) {
8            if (array[i] < array[i + 1]) {
9                swap(&array[i], &array[i + 1]);
10                done = 0;
11            }
12        }
13        for (i = 5; i >= 1; i--) {
14            if (array[i] > array[i - 1]) {
15                swap(&array[i], &array[i - 1]);
16                done = 0;
17            }
18        }
19    }
20    printf("%d", array[3]);
21}
22
The output of the program is ________.
Practise the full 2017-AN paper
Challenge a friend: WhatsApp Telegram
From RENYXERAPractise this subject in the exam simulatorTimed, GATE-style tests with analytics that find your weak topics.
Practise more on Functions, Parameter Passing, Scope Rules, RecursionTimed, in the real exam interface · free to start

A GATE question every morning

One real past-paper question every morning for your branch, answer hidden until you tap. Free on Telegram.

Join
Stuck? Ask in the student group