Question

In: Computer Science

C++ program to perform each of the area calculations in separate functions. Your program will take...

C++ program to perform each of the area calculations in separate functions. Your program will take in the relevant information in the main (), call the correct function that makes the calculation, return the answer to the main () and then print the answer to the screen. The program will declare a variable called “choice” of type int that is initialized to 0. The program will loop while choice is not equal to 4. In the body of the loop first print a menu as follows:

1) Calculate Area of a Rectangle

2) Calculate Area of a Square

3) Calculate Area of a Triangle

4) Quit Then read in the user’s answer into the variable “choice”. Validate the user input to be an integer between 1 and 4. If the choice is 1, then call a function to perform the input and output given for the area of a rectangle, else if choice is 2 call a function to calculate the area of a square, else if choice is 3 call a function to perform the area of a triangle, else if the choice is 4 printout the message “Bye!”

Solutions

Expert Solution

Summary:

The code is given below save the file and run it .

#include<bits/stdc++.h>
using namespace std;

double areaRectangle(double length, double breadth){

        double areaOfRectangle =  length * breadth;
        return areaOfRectangle;
}

double areaSquare(double Length){
        double areaOfSquare = Length * Length;
        return areaOfSquare;
}

double areaTriangle(double base, double height){
        double areaOfTriangle =  (base * height)/2;
        return areaOfTriangle;
}

int main(){

        int choice =0;
        while(choice !=4){
                cout << "1) Calculate Area of a Rectangle" << endl;
                cout << "2) Calculate Area of a Square" << endl;
                cout << "3) Calculate Area of a Triangle"<< endl;
                cout << "4) Quit" << endl;
                cin >> choice ;
                if(choice <=0 || choice >= 5){
                        cout << "Please enter option between 1 to 4 " << endl;
                }

                if(choice ==1){
                         double length, breadth;
                         cout << "Enter the Length and Breadth of the Rectangle : "<<endl;
                         cin >> length >> breadth;  
                         double area_rectangle = areaRectangle(length,breadth);
                         cout << "Area of the Rectangle : "<<area_rectangle << endl;
                }else if(choice ==2){
                         double Length;
                         cout << "Enter the Length of the Square :"<<endl;
                         cin >> Length;   
                         double area_square=areaSquare(Length);
                         cout<< "Area of the Square : "<< area_square << endl;

                }else if(choice ==3){
                        double base, height;
                        cout << "Enter the base and height of the Triangle : " << endl;
                        cin >> base >> height;
                        double area_triangle=areaTriangle(base,height);
                        cout << "Area of the Triangle : " << area_triangle << endl;

                }
        }

        cout<< "Bye!" << endl;
        return 0;

}

Related Solutions

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...
C++ questions, Please make sure to divide your program into functions which perform each major task....
C++ questions, Please make sure to divide your program into functions which perform each major task. The game of rock paper scissors is a two player game in which each each player picks one of the three selections: rock, paper and scissors. The game is decided using the following logic: ROCK defeats SCISSORS (“smashes”) PAPER defeats ROCK (“wraps”) SCISSORS defeats PAPER (“slices”) If both players choose the same selection the game ends in a tie. Write a program that asks...
C++ questions, please make sure to dividet he program into functions which perform each major task,...
C++ questions, please make sure to dividet he program into functions which perform each major task, A leap year is defined as any calendar year which meets the following criteria: If the year is not divisible by 4, it is a common year If the year is not divisible by 100, it is a leap year If the year is not divisible by 400, it is a common year Otherwise it is a leap year For your program you will...
Take the following program and include overload functions into it. Display result of function. C++ Program:...
Take the following program and include overload functions into it. Display result of function. C++ Program: #include <iostream> using namespace std; // this is the additional function string read() {     string input;     cout << "Enter input: ";     cin >> input;     return input; } int main() {     // call function here     string output = read();     cout << "You entered: " << output; }
Design a program which uses functions to sort a list and perform a binary search. Your...
Design a program which uses functions to sort a list and perform a binary search. Your program should: Iinitialize an unsorted list (using the list provided) Display the unsorted list Sort the list Display the sorted list. Set up a loop to ask the user for a name, perform a binary search, and then report if the name is in the list. Use a sentinel value to end the loop. Do not use the Python built in sort function to...
Write a C++ program to perform the addition of two hexadecimal numerals each with up to...
Write a C++ program to perform the addition of two hexadecimal numerals each with up to 10 digits. If the result of the addition is more than 10 digits long, then simply give the output message "Addition overflow" and not the result of the addition. Use arrays to store hexadecimal numerals as arrays of characters. Input Notes: The program reads two strings, each a sequence of hex digits (no lowercase, however) terminated by a "q". Example: 111q AAAq Output Notes...
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions...
Complete the provided C++ program, by adding the following functions. Use prototypes and put your functions below main. 1. Write a function harmonicMeans that repeatedly asks the user for two int values until at least one of them is 0. For each pair, the function should calculate and display the harmonic mean of the numbers. The harmonic mean of the numbers is the inverse of the average of the inverses. The harmonic mean of x and y can be calculated...
, Excel allows us to create our own formulas and functions to perform calculations and solve...
, Excel allows us to create our own formulas and functions to perform calculations and solve problems within the spreadsheet. Which of the functions or formulas that you experimented with in Presentation 1 did you find the most valuable? How do you think this particular function will save time in using Excel spreadsheets? What other functions do you think you will use on a regular basis? Why? Explain. Was there a function that is new to you?
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT