ProgrammingHardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
unsigned x = *(const unsigned *)a, y = *(const unsigned *)b;
return (y > x) - (y < x);
}
int main(void) {
unsigned v[] = {4000000000u, 5u, 3000000000u};
qsort(v, 3, sizeof v[0], cmp);
for (int i = 0; i < 3; i++) printf("%u ", v[i]);
}- A. 5 3000000000 4000000000
- B. 4000000000 3000000000 5
- C. 3000000000 4000000000 5
- D. 4000000000 5 3000000000
Correct answer: B. 4000000000 3000000000 5
The comparator returns a positive value when the second argument is larger, which sorts the unsigned values in descending order. Using a signed subtraction here would wrap around for these values.
More C Programming questions
- How many numbers from 1 to 100 have bit 3 set (value 8)? Compute with this program. c #include <stdio.h> int main(void) { int c = 0; for…
- What is printed? Assume uint16t is 16 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint16t x = 60000; uint16t y = x 2…
- How many ways are there to make 10 from unlimited coins of value 2, 3 and 5? Compute with this program. c #include <stdio.h> int ways(int…
- What is the length of the longest common subsequence of ABCBDAB and BDCABA? The program computes it by plain recursion without a table…
- How many islands does this 4-connected flood-fill count find in the 3 by 3 grid "101", "010", "101"? Compute with the program. c #include…
- How many of these statements are true when x = 255? Count them with this program. c #include <stdint.h> #include <stdio.h> int main(void)…