Function Pointer Calculator

Function Pointer Calculator

Medium C Functions 34 views
Explanation Complexity

Problem Statement

Create char array of functions: add, subtract, multiply, divide. Take operator input from the user and call the corresponding function pointer. Dynamic function selection demo.

Input Format

Two numbers and an operator character (+, -, *, /).

Output Format

Result of the selected operation.

Example

Number 1: 10
Number 2: 5
Operator: *
Result = 50

Constraints

• Valid operators: +, -, *, /

• Division by zero must be handled

Concept Explanation

The program selects and calls a function dynamically using a function pointer based on the operator entered by the user.

Step-by-Step Explanation

1.Create four separate functions:

• add

• subtract

• multiply

• divide

2.Create an array of function pointers, where each pointer points to one operation function.

3.Create a corresponding array of operator characters (+, -, *, /).

4.Take two numbers as input from the user.

5.Take an operator character as input.

6.Traverse the operator array to find a match with the user input.

7.When a match is found:

• Use the same index to access the function pointer array.

• Call the function through the function pointer.

8.If the operator is division and the second number is zero:

• Show an error message.

9.If no matching operator is found:

• Show an invalid operator error.

Concept Explanation

The program selects and calls a function dynamically using a function pointer based on the operator entered by the user.

Step-by-Step Explanation

1.Create four separate functions:

• add

• subtract

• multiply

• divide

2.Create an array of function pointers, where each pointer points to one operation function.

3.Create a corresponding array of operator characters (+, -, *, /).

4.Take two numbers as input from the user.

5.Take an operator character as input.

6.Traverse the operator array to find a match with the user input.

7.When a match is found:

• Use the same index to access the function pointer array.

• Call the function through the function pointer.

8.If the operator is division and the second number is zero:

• Show an error message.

9.If no matching operator is found:

• Show an invalid operator error.

Input / Output Format

Input Format
Two numbers and an operator character (+, -, *, /).
Output Format
Result of the selected operation.
Constraints
• Valid operators: +, -, *, /

• Division by zero must be handled

Examples

Input:
Number 1: 10 Number 2: 5 Operator: *
Output:
Result = 50

Example Solution (Public)

C
#include <stdio.h>

/* Function prototypes */
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { 
    if (b == 0) {
        printf("Error: Division by zero!n");
        return 0;
    }
    return a / b; 
}

int main() {
    char op;
    int x, y;
    
    /* Array of function pointers */
    int (*funcPtr[4])(int, int) = {add, subtract, multiply, divide};
    char operators[4] = {'+', '-', '*', '/'};

    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);

    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &op);

    int i, found = 0;

    for (i = 0; i < 4; i++) {
        if (op == operators[i]) {
            int result = funcPtr[i](x, y);  // Call corresponding function
            printf("Result: %d %c %d = %dn", x, op, y, result);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("Error: Invalid operator!n");
    }

    return 0;
}

Official Solution Code

#include <stdio.h>

/* Function prototypes */
int add(int a, int b) { return a + b; }
int subtract(int a, int b) { return a - b; }
int multiply(int a, int b) { return a * b; }
int divide(int a, int b) { 
    if (b == 0) {
        printf("Error: Division by zero!n");
        return 0;
    }
    return a / b; 
}

int main() {
    char op;
    int x, y;
    
    /* Array of function pointers */
    int (*funcPtr[4])(int, int) = {add, subtract, multiply, divide};
    char operators[4] = {'+', '-', '*', '/'};

    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);

    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &op);

    int i, found = 0;

    for (i = 0; i < 4; i++) {
        if (op == operators[i]) {
            int result = funcPtr[i](x, y);  // Call corresponding function
            printf("Result: %d %c %d = %dn", x, op, y, result);
            found = 1;
            break;
        }
    }

    if (!found) {
        printf("Error: Invalid operator!n");
    }

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.