Challenge - 5 Problems
Multiple Input and Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with multiple input parameters
What is the output of this C program when calling
sum_and_product(3, 4, &sum, &product);?C
#include <stdio.h> void sum_and_product(int a, int b, int *sum, int *product) { *sum = a + b; *product = a * b; } int main() { int sum = 0, product = 0; sum_and_product(3, 4, &sum, &product); printf("Sum = %d, Product = %d\n", sum, product); return 0; }
Attempts:
2 left
💡 Hint
Remember that pointers allow functions to modify variables outside their scope.
✗ Incorrect
The function calculates sum and product and stores them via pointers. So sum = 3 + 4 = 7 and product = 3 * 4 = 12.
❓ Predict Output
intermediate2:00remaining
Output of function returning multiple values via struct
What will be printed by this program?
C
#include <stdio.h>
typedef struct {
int sum;
int diff;
} Result;
Result calculate(int x, int y) {
Result r;
r.sum = x + y;
r.diff = x - y;
return r;
}
int main() {
Result res = calculate(10, 4);
printf("Sum: %d, Difference: %d\n", res.sum, res.diff);
return 0;
}Attempts:
2 left
💡 Hint
Check how sum and difference are calculated inside the struct.
✗ Incorrect
Sum is 10 + 4 = 14 and difference is 10 - 4 = 6, stored in the struct and printed.
❓ Predict Output
advanced2:00remaining
Output of function with multiple pointer parameters and side effects
What is the output of this program?
C
#include <stdio.h> void update_values(int *a, int *b) { *a = *a + *b; *b = *a - *b; } int main() { int x = 5, y = 3; update_values(&x, &y); printf("x = %d, y = %d\n", x, y); return 0; }
Attempts:
2 left
💡 Hint
Trace the pointer updates step by step.
✗ Incorrect
Initially x=5, y=3. After *a = *a + *b, x=8. Then *b = *a - *b = 8 - 3 = 5. So x=8, y=5.
❓ Predict Output
advanced2:00remaining
Output of function returning multiple outputs via array parameters
What will this program print?
C
#include <stdio.h> void min_max(int arr[], int size, int *min, int *max) { *min = arr[0]; *max = arr[0]; for (int i = 1; i < size; i++) { if (arr[i] < *min) *min = arr[i]; if (arr[i] > *max) *max = arr[i]; } } int main() { int numbers[] = {7, 2, 9, 4, 5}; int minimum, maximum; min_max(numbers, 5, &minimum, &maximum); printf("Min: %d, Max: %d\n", minimum, maximum); return 0; }
Attempts:
2 left
💡 Hint
Look carefully at how min and max are updated in the loop.
✗ Incorrect
The smallest number is 2 and the largest is 9 in the array.
🧠 Conceptual
expert2:00remaining
Why use pointers for multiple outputs in C functions?
Which is the main reason C functions use pointers to return multiple outputs instead of returning multiple values directly?
Attempts:
2 left
💡 Hint
Think about how many values a C function can return directly.
✗ Incorrect
C functions can return only one value directly. Using pointers allows the function to change variables outside its scope, effectively returning multiple outputs.