Question

In: Computer Science

Create a function that takes two numbers and a mathematical operator +, –, / , *...

Create a function that takes two numbers and a mathematical operator +, –, / , * and will perform a calculation with the given numbers.

Examples:

calculator(2, "+", 2) ➞ 4

calculator(2, "*", 2) ➞ 4

calculator(4, "/", 2) ➞ 2

Notes If the input tries to divide by 0, return: "Can't divide by 0!"

Code in C++ language ...

Solutions

Expert Solution

C++ CODE

#include <iostream>
#include <stdlib.h>
using namespace std;

void calculator(int num1, char symbol, int num2)//function used to solve mathematical operation
{
        switch(symbol)
        {
                case '+'://addition operatiob
                        cout << num1 + num2;
                        break;
                case '-'://subtraction operation
                        cout << num1 - num2;
                        break;
                case '*'://multiplication operation
                        cout << num1 * num2;
                        break;
                case '/'://division operation
                        if ( num2 == 0 )
                                cout << "Can't divide by 0!";
                        else
                                cout << num1 / num2;
                        break;
        }
}

int main()
{
        int num1, num2;
        char symbol;

        cout << "Enter mathematical operator:";
        cin >> symbol;//get operator from user
        cout << "Enter first number: ";
        cin >> num1;//get first number from user
        cout << "Enter second number: ";
        cin >> num2;//get second number from user
        
        calculator(num1, symbol, num2);
        return 0;
}

C++ CODE SCREENSHOT

OUTPUT SCREENSHOT:


Related Solutions

Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Write a Scheme function that takes two integers and returns the list of all integer numbers...
Write a Scheme function that takes two integers and returns the list of all integer numbers between these two integers (inclusively) in increasing order. (numbers 10 20) (10 11 12 13 14 15 16 17 18 19 20) Please explain every step.
Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers,...
Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers, vals1 and vals2, and that uses recursion to construct and return a new list in which each element is the sum of the corresponding elements of vals1 and vals2. You may assume that the two lists have the same length. For example: >>> add([1, 2, 3], [3, 5, 8]) result: [4, 7, 11] Note that: The first element of the result is the sum...
Challenge 1 – 2D Array Maker Create a function that takes in two parameters and returns...
Challenge 1 – 2D Array Maker Create a function that takes in two parameters and returns a created two-dimension array. var twoD = Make2D(<width>, <height>); Challenge 2 – Fill 2D Array Create a function that takes a single parameter and fills the 2D array with that parameter Use Cases: Fill2D(<twoD array>, <fill value>); //fills twoD twoD = fill2D(<twoD array>, <fill value>); //fills twoD, but also returns reference Challenge 3 – Make 2D Array Write a function that takes 3 parameters...
In python I want to create a singular function that takes two parameters 'head; and 'skip'....
In python I want to create a singular function that takes two parameters 'head; and 'skip'. Head is a linked list. Skip is a non negative value. If skip is zero it should return the linked list unchanged. The skip amount determines the amount to skip over. I want to change the linked list accordingly and then return the linked list with the modifications, not a list. If you have a linked list 11 -> 12 -> 18 -> 20...
In python I want to create a singular function that takes two parameters 'head; and 'skip'....
In python I want to create a singular function that takes two parameters 'head; and 'skip'. Head is a linked list. Skip is a non negative value. If skip is zero it should return head unchanged. The skip amount determines the amount to skip over. I want to change the linked list accordingly. If you have a linked list 11 -> 12 -> 18 -> 20 -> 24 -> 32 -> 38 -> 44 and skip =2, then you should...
Create a function called merge that takes two int vectors (x and y) as argument and...
Create a function called merge that takes two int vectors (x and y) as argument and returns an int vector (z) that is the result of merging the two vectors x and y. Here is an example to explain how the merge function works: If the vector x has the following values: 1 2 3 4 5, and the vector y has the following: 6 7 8 9 10 11 12, then the merging vector z will have: 1 6...
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance.
In C++, create a function exchangesl that takes an argument of an array of integers (...
In C++, create a function exchangesl that takes an argument of an array of integers ( for C++ use implement void exchangesl(vector<int>& a) . Those integers need to be changed so that the smallest and largest values in the array are exchanged. Assume that there is at least one element, if the largest value occurs more than once then exchange the first instance, if the smallest value happens more than once then exchange the last instance. Use the following file:...
In basic C++ Create a function that takes in a vector, sorts it, then outputs to...
In basic C++ Create a function that takes in a vector, sorts it, then outputs to the console the result.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT