ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int f(int *a, int n) {
if (n == 1) return a[0];
int m = f(a + 1, n - 1);
return a[0] > m ? a[0] - m : m - a[0];
}
int main(void) {
int a[] = {3, 8, 2, 7};
printf("%d", f(a, 4));
}- A. 4
- B. 6
- C. 2
- D. 0
Correct answer: D. 0
f folds from the right, taking absolute differences: f({7}) = 7, f({2, 7}) = 5, f({8, 2, 7}) = |8 − 5| = 3, f({3, 8, 2, 7}) = |3 − 3| = 0. Folding from the left instead (|3 − 8| = 5, |5 − 2| = 3, |3 − 7| = 4) gives the trap answer 4.
More C Programming questions
- What is printed? c printf("%.1f", 1 ? 3 : 2.5);
- What is printed? c int a = 0, b = 10; while (a < b) { a += 3; b -= 2; } printf("%d %d", a, b);
- After int q = realloc(p, 2 n sizeof q); succeeds (q is not NULL), where p pointed to n ints, which statement is true?
- With int i = 1, x; int a[5] = {0}; which of these statements have undefined behaviour? (i) i = i++ + 1; (ii) a[i] = i++; (iii) i = (i, i +…
- Which of the objects A to E have static storage duration (they live for the whole run of the program, in the data or BSS segment)? c int…
- What is printed? c int cmp(const void a, const void b) { return (const int )b - (const int )a; } int main(void) { int v[] = {4, 9, 1, 7}…