ProgrammingVery hardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
void rev(int *a, int l, int r) {
while (l < r) {
int t = a[l]; a[l] = a[r]; a[r] = t;
l++; r--;
}
}
int main(void) {
int a[] = {1, 2, 3, 4, 5};
rev(a, 0, 4);
rev(a, 0, 1);
rev(a, 2, 4);
for (int i = 0; i < 5; i++) printf("%d ", a[i]);
}- A. 5 4 3 2 1
- B. 4 5 1 2 3
- C. 3 4 5 1 2
- D. 4 5 3 2 1
Correct answer: B. 4 5 1 2 3
Reversing the whole array, then the first k elements, then the rest rotates the array right by k. Right-rotating {1,2,3,4,5} by 2 gives 4, 5, 1, 2, 3.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { static const unsigned char t[16] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4}; unsigned x =…
- How many times does the value change between neighbours in {1, 1, 2, 2, 2, 3, 1}? Compute with this program. c #include <stdio.h> int…
- How many numbers from 1 to 100 are divisible by 4 but not by 3? Compute with this program. c #include <stdio.h> int main(void) { int c =…
- What is printed by this memoised recursion? The static memo table starts with every entry zero, and calls counts each call to f. c…
- What is printed? c #include <stdio.h> int main(void) { int arr[2 3 + 1]; printf("%zu", sizeof arr / sizeof arr[0]); }
- How many paths go from the top-left to the bottom-right of a 3 by 3 grid of points, moving right or down, without passing through the…