ProgrammingVery hardTCS NQT · Accenture

What is printed?

What is printed?
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
    const int *x = a, *y = b;
    if (x[1] != y[1]) return x[1] - y[1];
    return y[0] - x[0];
}
int main(void) {
    int r[][2] = {{1, 3}, {2, 3}, {5, 1}};
    qsort(r, 3, sizeof r[0], cmp);
    for (int i = 0; i < 3; i++) printf("%d %d ", r[i][0], r[i][1]);
}
  1. A. 5 1 1 3 2 3
  2. B. 1 3 2 3 5 1
  3. C. 5 1 2 3 1 3
  4. D. 2 3 1 3 5 1

Correct answer: C. 5 1 2 3 1 3

The second column is the primary key ascending, so (5, 1) comes first. The two rows with second column 3 are ordered by the first column descending, giving (2, 3) before (1, 3).

More C Programming questions

Exams that ask this topic