ProgrammingVery hardTCS NQT · Accenture
What is printed?
What is printed?
#include <stdio.h>
#include <stdlib.h>
int cmp(const void *a, const void *b) {
const int *x = a, *y = b;
if (x[1] != y[1]) return x[1] - y[1];
return y[0] - x[0];
}
int main(void) {
int r[][2] = {{1, 3}, {2, 3}, {5, 1}};
qsort(r, 3, sizeof r[0], cmp);
for (int i = 0; i < 3; i++) printf("%d %d ", r[i][0], r[i][1]);
}- A. 5 1 1 3 2 3
- B. 1 3 2 3 5 1
- C. 5 1 2 3 1 3
- D. 2 3 1 3 5 1
Correct answer: C. 5 1 2 3 1 3
The second column is the primary key ascending, so (5, 1) comes first. The two rows with second column 3 are ordered by the first column descending, giving (2, 3) before (1, 3).
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 3, 4, 5}, p = a; printf("%d", (p + (p + 2))); }
- How many ways are there to place 3 non-attacking rooks on a 3 by 3 board? Compute with this program. c #include <stdio.h> int used[3]; int…
- In the sorted array {1, 3, 4, 4, 7}, what index is printed by this lower-bound search for key 4? c #include <stdio.h> int main(void) { int…
- How many of these statements are true for unsigned x = 5? Print the count from this program. c #include <stdio.h> int main(void) {…
- What is printed? c #include <stdio.h> int main(void) { int a[] = {5, 6, 7}, p = a + 1; printf("%d", p[-1] + p + p[1]); }
- How many tilings does a 2 by 5 board have with 2 by 1 dominoes? Compute with this recursive program. c #include <stdio.h> int f(int n) {…