Question

In: Computer Science

C++ Write a program that has two functions. The 1st function is the main function. The...

C++

Write a program that has two functions.

The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator.

The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.

Solutions

Expert Solution

CODE: C++ Programming Language

#include <iostream>
using namespace std;
int calculate(int, int, char); // Function Prototype
// Main function
int main(){
   // Declaring required variables
   int num1, num2;
   char optr;
   cout << "Enter number1: "; // Prompt the user for number 1
   cin >> num1;
   cout << "Enter number2: "; // Prompt the user for number 2
   cin >> num2;
   cout << "Enter operator: "; // Prompt the user for Operator (+, -, *, /)
   cin >> optr;
   int result = calculate(num1, num2, optr); // Calling the calculate function
   cout << result;
   return 0;
}
// Second function Calculate
int calculate(int num1, int num2, char optr){
   // Switch statment to evaluate operator
   switch(optr){
       case '+':
           return num1 + num2;
           break;
       case '-':
           return num1 - num2;
           break;
       case '*':
           return num1 * num2;
           break;
       case '/':
           return num1 / num2;
           break;
   }
}

======================================================================

SCREENSHOT OF THE CODE:

======================================================================

OUTPUT:

Thank you. Please ask me if you have any doubt.


Related Solutions

Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main –...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main – Should accept input of five numeric grades from the user USING A LOOP.   It should then calculate the average numeric grade.    The numeric average should be passed to the determine_grade function. determine_grade – should display the letter grade to the user based on the numeric average:        Greater than 90: A 80-89:                 B 70-79:                 C 60-69:              D Below 60:           F Modularity:...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
In C++ prototype functions above "main" and define them below "main"; Write a program that uses...
In C++ prototype functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count of...
Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
Language Python with functions and one main function Write a program that converts a color image...
Language Python with functions and one main function Write a program that converts a color image to grayscale. The user supplies the name of a file containing a GIF or PPM image, and the program loads the image and displays the file. At the click of the mouse, the program converts the image to grayscale. The user is then prompted for a file name to store the grayscale image in.
Write a program in c++, with at least four functions, including main, which must do the...
Write a program in c++, with at least four functions, including main, which must do the following: Ask user whether they want to encode or decode a message – if no, then terminate Take the input string from the user, store it in dynamic memory (use new) As appropriate, encode or decode the message using Rot13. Output the encoded/decoded message Delete the input string from dynamic memory (use delete) Input will be a string of no more than 25 characters....
Write a small C++ program with 4 functions (and main(): getNumbers()- what is the return, what...
Write a small C++ program with 4 functions (and main(): getNumbers()- what is the return, what are the parameters? findMax()- what is the return, what are the parameters? findMin()-what is the return, what are the parameters? find()- should return the index of the element or a -1 indicating not found The main function will call those methods and print the results of each. 1 // declare necessary variables 2 // declare array 3 double numbers[SIZE]; 4 // Function prototypes 5...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function...
C++ Recursive Functions: Please call functions in a main function as well. 1. A recursive function that print the reverse of a string. (e.g., void printReverse(string exp)). For example, if exp =”coding”, then the function should print out “gnidoc”. 2. Implement a non-recursion-based binary search function. Convert this function into a recursion-based function. 3. Implement a recursive and non-recursive Fibonacci function.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT