#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; }
typedef int (*op_func_t)(int, int);
int execute_command(int a, int b, int cmd) {
// Your logic here using function pointer array
op_func_t ops[4];
if (cmd < 0 || cmd >= (int)(sizeof(ops)/sizeof(ops[0]))) {
printf("Invalid command index!\n");
return 0;
}
ops[0] = add;
ops[1] = sub;
ops[2] = mul;
ops[3] = divide;
int result = ops[cmd](a, b);
return result;
}
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