#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) {
// Your logic here using function pointer array
int (*operations[4])(int, int) = {add, sub, mul, divide};
return operations[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
Create an array of pointers of size 4, represented by *operations[4]
Then to specify them as fucntions pointers, give the number of arguments
wtite the function name as it is in correct index to point to the fucntion
operations[cmd] gets the correct fucntion, then pass arguments in brackets
Input
10 5 0
Expected Output
15