ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
struct Pair { int a, b; };
int cmp(const void *p, const void *q) {
const struct Pair *x = p, *y = q;
if (x->b != y->b) return y->b - x->b;
return x->a - y->a;
}
int main(void) {
struct Pair v[] = {{1, 5}, {2, 9}, {3, 5}, {4, 1}};
qsort(v, 4, sizeof v[0], cmp);
for (int i = 0; i < 4; i++) printf("%d ", v[i].a);
}- A. 2 1 3 4
- B. 2 3 1 4
- C. 2 1 4 3
- D. 1 2 3 4
Correct answer: A. 2 1 3 4
The comparator sorts by b descending, so b = 9 comes first, then the two entries with b = 5 are ordered by a ascending, and b = 1 comes last. The printed a values are 2, 1, 3, 4.
More C Programming questions
- What is printed? c #include <stdio.h> #include <stdlib.h> #include <string.h> int cmp(const void a, const void b) { const char x = (const…
- How many subarrays of {2, 1, 4, 3, 1, 2, 4} have a sum of exactly 7? Compute with this program. c #include <stdio.h> int main(void) { int…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 3, -1, -3, 5, 3, 6, 7}; int k = 3, dq[8], head = 0, tail = 0, total =…
- How many values x in 0..255 satisfy (x & (uint8t)-x) == x? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int…
- What is printed? c #include <stdio.h> int f(int n) { static int calls = 0; calls++; if (n <= 0) return calls; int r = f(n - 2); return r…
- What is printed? c #include <stdio.h> struct P { int v; }; int main(void) { struct P arr[3] = {{1}, {2}, {3}}; int sum = 0; for (struct P…