ProgrammingHardTCS NQT · Infosys
What is printed?
What is printed?
#include <stdlib.h>
#include <stdio.h>
int cmp(const void *a, const void *b) {
int x = *(const int *)a, y = *(const int *)b;
int dx = abs(x - 10), dy = abs(y - 10);
if (dx != dy) return dx - dy;
return (x > y) - (x < y);
}
int main(void) {
int v[] = {7, 13, 10, 3, 17};
qsort(v, 5, sizeof v[0], cmp);
for (int i = 0; i < 5; i++) printf("%d ", v[i]);
}- A. 10 7 13 3 17
- B. 10 13 7 3 17
- C. 10 7 13 17 3
- D. 7 13 10 3 17
Correct answer: A. 10 7 13 3 17
Elements are ordered by distance from 10, which gives 10 (0), then 7 and 13 (both 3), then 3 and 17 (both 7). Ties are broken by value ascending, so 7 comes before 13 and 3 before 17.
More C Programming questions
- What is printed? c #include <stdio.h> unsigned fib(unsigned n) { unsigned a = 0, b = 1; for (unsigned i = 0; i < n; i++) { unsigned t = a…
- What is printed? c #include <stdio.h> void rev(const char s) { if (s == '0') return; rev(s + 1); putchar(s); } int main(void) {…
- What is printed? c #include <stdio.h> int calls = 0; int A(int m, int n) { calls++; if (m == 0) return n + 1; if (n == 0) return A(m - 1…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0x16D; printf("%u", x & 0xF); }
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = 32, b1 = (x >> 1) & 1u, b5 = (x >> 5)…
- What is printed? c #include <stdio.h> int main(void) { unsigned a = 0xC, b = 0xA, c = 0x9; unsigned m = (a & b) | (b & c) | (a & c)…