ProgrammingVery hardTCS NQT · Infosys
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
int v[] = {30, 10, 20};
int cmp(const void *a, const void *b) {
int i = *(const int *)a, j = *(const int *)b;
return (v[i] > v[j]) - (v[i] < v[j]);
}
int main(void) {
int idx[] = {0, 1, 2};
qsort(idx, 3, sizeof idx[0], cmp);
for (int i = 0; i < 3; i++) printf("%d ", idx[i]);
}- A. 1 2 0
- B. 0 1 2
- C. 2 0 1
- D. 1 0 2
Correct answer: A. 1 2 0
The array is sorted by the values it indexes, not by the indices. Values 10, 20, 30 sit at indices 1, 2, 0, so that is the printed order.
More C Programming questions
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { unsigned x = (const unsigned )a, y =…
- 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…