ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
int ds(int n) {
int s = 0;
while (n) { s += n % 10; n /= 10; }
return s;
}
int cmp(const void *a, const void *b) {
int x = *(const int *)a, y = *(const int *)b;
if (ds(x) != ds(y)) return ds(y) - ds(x);
return y - x;
}
int main(void) {
int v[] = {10, 22, 9, 31};
qsort(v, 4, sizeof v[0], cmp);
for (int i = 0; i < 4; i++) printf("%d ", v[i]);
}- A. 9 31 22 10
- B. 9 22 31 10
- C. 31 22 9 10
- D. 10 9 31 22
Correct answer: A. 9 31 22 10
Digit sums are 1, 4, 9 and 4. The order is by digit sum descending, and the tie between 22 and 31 is broken by value descending, so 31 comes first.
More C Programming questions
- How many ordered ways are there to write 6 as a sum of parts 1, 2 and 3? Compute with this program. c #include <stdio.h> int f(int n) { if…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 7; int i = 0; while (x & 1u) { x >>= 1; i++; } printf("%d", i); }
- What is printed? Assume unsigned short is 16 bits. c #include <stdio.h> int main(void) { unsigned short a = 255, b = 255; unsigned c = a…
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { char w[] = {"ab", "cde"}; char p = w; printf("%d"…
- What is printed? c #include <stdio.h> int main(void) { int a[5] = {0}; int p = a + 4; printf("%ld", (long)(p - &a[0])); }
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 3, 2, 5}, c = 0; for (int p = a; p < a + 4; p++) if (p > 2) c++…