Question

Using the code snippet below, complete the missing sections using the comments as a guide ********************************************************************************...

Using the code snippet below, complete the missing sections using the comments as a guide

********************************************************************************

#include <stdio.h>
#include <stdlib.h>

// The four arithmetic operations ... one of these functions is selected
// at runtime with a switch or a function pointer
float Plus (float a, float b) { return a+b; }
float Minus (float a, float b) { return a-b; }
float Multiply(float a, float b) { return a*b; }
float Divide (float a, float b) { return a/b; }


// Solution with a switch-statement
// <opCode> specifies which operation to execute
void Switch(float a, float b, char opCode) {
float result;

// execute operation
switch(opCode) {
case '+' : result = Plus (a, b); break;
case '-' : result = Minus (a, b); break;
case '*' : result = Multiply (a, b); break;
case '/' : result = Divide (a, b); break;
}

printf("Switch: 2+5=%.2lf\n", result); // display result
}


// Solution with a function pointer
// <pt2Func> is a function pointer and points to a function that takes two floats and returns a float


// 4. fill in the parameters, 3 of them, the 3rd one should be the function pointer


void Replace_Switch( ) {
float result;


// 5. call the function using the function pointer sent in as the third argument //
result =   

printf("Switch replaced by function pointer: 2+5=");
printf("%.2lf\n", result);
}


int main() {
float result;
// 1. declare a function pointer and assign it to point to the plus function //
  

// 2. call function with function pointer assigning result to the function call and sending 2 and 5 as the arguments //
  
printf("%.2lf\n", result);

// the function using switch and no function pointer //
Switch(2,5, '+');


// 3. call function using the function that replaces switch //
    

return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include <stdlib.h>

// The four arithmetic operations ... one of these functions is selected
// at runtime with a switch or a function pointer
float Plus(float a, float b) { return a + b; }

float Minus(float a, float b) { return a - b; }

float Multiply(float a, float b) { return a * b; }

float Divide(float a, float b) { return a / b; }


// Solution with a switch-statement
// <opCode> specifies which operation to execute
void Switch(float a, float b, char opCode) {
    float result;

// execute operation
    switch (opCode) {
        case '+' :
            result = Plus(a, b);
            break;
        case '-' :
            result = Minus(a, b);
            break;
        case '*' :
            result = Multiply(a, b);
            break;
        case '/' :
            result = Divide(a, b);
            break;
    }

    printf("Switch: 2+5=%.2lf
", result); // display result
}


// Solution with a function pointer
// <pt2Func> is a function pointer and points to a function that takes two floats and returns a float


// 4. fill in the parameters, 3 of them, the 3rd one should be the function pointer
void Replace_Switch(float a, float b, float (*pt2Func)(float, float)) {
    float result;

// 5. call the function using the function pointer sent in as the third argument //
    result = pt2Func(a, b);

    printf("Switch replaced by function pointer: 2+5=");
    printf("%.2lf
", result);
}


int main() {
    float result;
// 1. declare a function pointer and assign it to point to the plus function //
    float (*pt2Func)(float, float) = &Plus;

// 2. call function with function pointer assigning result to the function call and sending 2 and 5 as the arguments //
    result = pt2Func(2, 5);
    printf("%.2lf
", result);

// the function using switch and no function pointer //
    Switch(2, 5, '+');

// 3. call function using the function that replaces switch //
    Replace_Switch(2, 5, pt2Func);
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Using the code snippet below, complete the missing sections using the comments as a guide ********************************************************************************...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I must call multiply, divide and remainder functions inside of SUBTRACT function.. I am stuck at...

    I must call multiply, divide and remainder functions inside of SUBTRACT function.. I am stuck at this point. Thank you #include<stdio.h> int getDifference(int a, int b); int main() { int a, b, result, multiply, divide, rem; printf("Enter the two integer numbers : "); scanf("%d %d", &a, &b); //Call Function With Two Parameters result = getDifference(a, b);    printf("Difference of two numbers is : %d\n" , result);    return (0); } int getDifference(int a, int b) { int c, multiply; float...

  • In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static...

    In Java. Please use the provided code Homework 5 Code: public class Hw05Source { public static void main(String[] args) { String[] equations ={"Divide 100.0 50.0", "Add 25.0 92.0", "Subtract 225.0 17.0", "Multiply 11.0 3.0"}; CalculateHelper helper= new CalculateHelper(); for (int i = 0;i { helper.process(equations[i]); helper.calculate(); System.out.println(helper); } } } //========================================== public class MathEquation { double leftValue; double rightValue; double result; char opCode='a'; private MathEquation(){ } public MathEquation(char opCode) { this(); this.opCode = opCode; } public MathEquation(char opCode,double leftVal,double rightValue){...

  • Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[],...

    Finish function to complete code. #include <stdio.h> #include <stdlib.h> #include<string.h> #define Max_Size 20 void push(char S[], int *p_top, char value); char pop(char S[], int *p_top); void printCurrentStack(char S[], int *p_top); int validation(char infix[], char S[], int *p_top); char *infix2postfix(char infix[], char postfix[], char S[], int *p_top); int precedence(char symbol); int main() { // int choice; int top1=0; //top for S1 stack int top2=0; //top for S2 stack int *p_top1=&top1; int *p_top2=&top2; char infix[]="(2+3)*(4-3)"; //Stores infix string int n=strlen(infix); //length of...

  • The code snippet below is part of the restaurant menu program you previously used in the...

    The code snippet below is part of the restaurant menu program you previously used in the lab. Add a choice for a drink. Add the necessary code to allow the program to calculate the total price of order for the user. Assume the following price list: Hamburger $5 Hotdog $4 Fries $3 Drink $2 The program should allow the user to keep entering order until choosing to exit. At the end the program prints an order summary like this: You...

  • SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1...

    SEE THE Q3 for actual question, The first Two are Q1 and Q2 answers . Q1 #include<iostream> using namespace std; // add function that add two numbers void add(){    int num1,num2;    cout << "Enter two numbers "<< endl;    cout << "First :";    cin >> num1;    cout << "Second :";    cin >>num2;    int result=num1+num2;    cout << "The sum of " << num1 << " and "<< num2 <<" is = "<< result;   ...

  • QUESTION 9 What will be the output of following code snippet? int a[3] = {1, 2,...

    QUESTION 9 What will be the output of following code snippet? int a[3] = {1, 2, 3};         int *p = a;         int **r = &p;         printf("%p %p", *r, a); A. Different memory addresses printed B. 1 2 C. Same memory address printed twice D. 1 1 2 points    QUESTION 10 What will be the output of following code snippet? int arr[4] = {1, 2, 3, 4};         int *p;         p = arr + 3;         *p = 5;         printf("%d\n", arr[3]); A....

  • /* •   The following code consists of 5 parts. •   Find the errors for each part...

    /* •   The following code consists of 5 parts. •   Find the errors for each part and fix them. •   Explain the errors using comments on top of each part. */ #include <stdio.h> #include <string.h> int main( ) { ////////////////////////////////////////////////////////////////////////// //////////////        Part A. (5 points)                ////////////////// int g(void) { printf("%s", Inside function g\ n " ); int h(void) {       printf(" % s ", Inside function h\ n "); } } printf("Part A: \n "); g(); printf(" \n"); ////////////////////////////////////////////////////////////////////////// //////////////       ...

  • This code in c evaluates a mathematical expression and checks that it is correctly balanced, then...

    This code in c evaluates a mathematical expression and checks that it is correctly balanced, then converts the expression to its postfix form (This part works). Now I need to evaluate the postfix expression with a function. I've been compiling it using dev c ++. #include #include #define SIZE 20 #include int profundidad(char expresion[]); int prec(char op1, char op2); void postfijo(char expresion[]); char funcion[SIZE]; float convierte(char car); float resultado(float opnd1, char symb, float opnd2); float evaluar(char expresion[]); #define SIZE 20...

  • Using the code below, Complete the C program main.c to stop execution if the user attempts...

    Using the code below, Complete the C program main.c to stop execution if the user attempts a divide by 0. #include <stdio.h> int main() { float a, b; printf("Input two integers to divide the first by the second\n"); scanf("%f%f", &a, &b); printf("%f/%f = %.2f\n", a, b, a/b);    return 0; }

  • ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE...

    ASSIGNMENT DUE DATE GOT PUSHED BACK TO LATE THIS WEEK. PLEASE READ COMMENTS AND CODE BEFORE ANSWERING CODING SECTIONS HW07 #Q1-Q5 HW08 #Q1-Q2 // READ BEFORE YOU START: // Please read the given Word document for the project description with an illustrartive diagram. // You are given a partially completed program that creates a list of students for a school. // Each student has the corresponding information: name, standard, and a linked list of absents. // Please read the instructions...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT