ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
struct R { int a, b; };
int cmp(const void *p, const void *q) {
const struct R *x = p, *y = q;
if (x->a != y->a) return y->a - x->a;
return x->b - y->b;
}
int main(void) {
struct R r[] = {{3, 9}, {1, 2}, {3, 1}};
qsort(r, 3, sizeof r[0], cmp);
for (int i = 0; i < 3; i++) printf("%d %d ", r[i].a, r[i].b);
}- A. 3 1 1 2 3 9
- B. 3 1 3 9 1 2
- C. 3 9 3 1 1 2
- D. 1 2 3 1 3 9
Correct answer: B. 3 1 3 9 1 2
The first key a is sorted descending, so the two rows with a = 3 come first and are ordered by b ascending, then the row with a = 1. The output is 3 1, 3 9, 1 2.
More C Programming questions
- How many values of n in 1..50 have bit 2 set (the value 4)? Compute with this program. c #include <stdio.h> int main(void) { int c = 0…
- What is printed? Assume int is 4 bytes. c #include <stdio.h> int main(void) { int m[4][5]; printf("%zu %zu", sizeof m / sizeof m[0]…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned s = 0; for (unsigned i = 1; i <= 100000…
- How many partitions of 5 are there? The program counts partitions of n into parts of size at most m. Compute with it. c #include <stdio.h>…
- What is printed? c #include <stdio.h> int main(void) { char c = 'A'; c += 32; printf("%c %d", c, c); }
- What is printed? c #include <stdio.h> int main(void) { int x = 2, r = 0; switch (x) { case 1: r += 1; case 2: r += 10; case 3: r += 100…