Function Pointer Dispatch Table

Code

#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) {
    int res;
    if(cmd==0){ res=add(a,b); return res;}
    else if(cmd==1){ res=sub(a,b); return res;}
    else if(cmd==2){ res=mul(a,b); return res;}
    else{
        res=divide(a,b);
        return res;
    }

}

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

 

 

 

Upvote
Downvote
Loading...

Input

10 5 0

Expected Output

15