ProgrammingHardCompetitive coding (CP)

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[0] != y[0]) return (x[0] > y[0]) - (x[0] < y[0]);
    return (x[1] > y[1]) - (x[1] < y[1]);
}
int main(void) {
    int r[][2] = {{2, 1}, {1, 5}, {2, 0}};
    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. 2 0 2 1 1 5
  2. B. 1 5 2 0 2 1
  3. C. 1 5 2 1 2 0
  4. D. 2 1 2 0 1 5

Correct answer: B. 1 5 2 0 2 1

The rows are compared lexicographically, first by the first column and then by the second. (1, 5) comes first, then (2, 0) and (2, 1).

More C Programming questions

Exams that ask this topic