#include <stdio.h>
int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
int divide(int a, int b) { return a / b; }
// int execute_command(int a, int b, int cmd) {
// // Declare the function pointer ONCE, outside the switch
// int (*fp)(int, int) = NULL;
// switch (cmd) {
// case 0:
// fp = add;
// break;
// case 1:
// fp = sub;
// break;
// case 2:
// fp = mul;
// break;
// case 3:
// fp = divide; // not 'div'
// break;
// default:
// return -1; // invalid command
// }
// // Call through the function pointer and return the result
// return fp(a, b);
// }
int execute_command(int a, int b, int cmd) {
// Array of function pointers indexed by command code
int (*const ops[])(int, int) = { add, sub, mul, divide };
// Direct dispatch via indexing — no if/switch
return ops[cmd](a, b);
}
int main() {
int a, b, cmd;
scanf("%d %d %d", &a, &b, &cmd);
int result = execute_command(a, b, cmd);
printf("%d", result);
return 0;
}Solving Approach
Input
10 5 0
Expected Output
15