ProgrammingVery hardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
int nd(int n) {
int c = 0;
for (int d = 1; d <= n; d++)
if (n % d == 0) c++;
return c;
}
int cmp(const void *a, const void *b) {
int x = *(const int *)a, y = *(const int *)b;
int dx = nd(x), dy = nd(y);
if (dx != dy) return dx - dy;
return x - y;
}
int main(void) {
int v[] = {12, 7, 9, 8};
qsort(v, 4, sizeof v[0], cmp);
for (int i = 0; i < 4; i++) printf("%d ", v[i]);
}- A. 7 9 8 12
- B. 7 8 9 12
- C. 12 8 9 7
- D. 9 7 8 12
Correct answer: A. 7 9 8 12
Divisor counts are 6 for 12, 2 for 7, 3 for 9 and 4 for 8. Sorting by divisor count gives 7, 9, 8, 12.
More C Programming questions
- How many binary strings of length 4 have exactly two 1s? The program counts them recursively. What is printed? c #include <stdio.h> int…
- What is printed? c #include <stdio.h> int v[] = {1, 2, 3}; int cnt(int i, int s) { if (i == 3) return s % 2 == 0; return cnt(i + 1, s) +…
- What is printed? c #include <stdio.h> int used[3]; int count(int d) { if (d == 3) return 1; int t = 0; for (int i = 0; i < 3; i++) { if…
- What is printed? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t x = 0x0F; uint8t r =…
- How many set bits does this loop count in 0xFFFF0000? c #include <stdio.h> int main(void) { unsigned x = 0xFFFF0000u; int c = 0; while (x)…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned a = 65536u; printf("%u", a a); }