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]);
}- A. 2 0 2 1 1 5
- B. 1 5 2 0 2 1
- C. 1 5 2 1 2 0
- 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
- What is the digit sum of 2^15 = 32768? The recursion below computes it. Assume int is 32 bits. What is printed? c #include <stdio.h> int…
- How many subsets of {1, 2, 3} (the empty set included) have a sum divisible by 3? The recursion below counts them. What is printed? c…
- How many bit positions are zero in both 0xAAAA and 0x5555 within 16 bits? Count them with this program. c #include <stdint.h> #include…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = 12; printf("%u", (x << 3) + (x << 1)); }
- How many adjacent pairs of 1 bits does 27 (binary 11011) have? The program counts the set bits of x & (x >> 1). c #include <stdio.h> int…
- What is printed? c #include <stdio.h> int main(void) { int b[] = {1, 1, 0, 1, 1, 1, 0, 1, 1, 1}, run = 0, best = 0; for (int i = 0; i <…