Answer:
It is a C++ program :
#include <iostream> // to use input output functions
using namespace std; //to identify objects like cin cout
//following are the prototypes for the four functions placed above main
void displayMenu();
void findSquareArea();
void findCircleArea();
void findTriangleArea();
//definition for constant PI out of main
const float PI = 3.14159;
int main(){ //start of main function
displayMenu(); //calls displayMenu() method
}
void displayMenu(){ // to make user selection and calls relevant function to compute area according to the choice of user
int selection=0; //to choose from 4 options
cout<<"Program to calculate areas of objects:\\" ; //displays this message at start
do { //this loop asks user to enter a choice and this continues to execute and calculate depending on users choice until user enters 4 to exit
cout<<"\\1.Square \\2.Circle \\3.Right Triangle \\4.Quit";
cout<<"\\Enter your choice:"; //prompts user to enter choice
cin>>selection; //reads the input choice
if(selection==1) //if user enters 1
{ //computes area of square by calling findSquareArea
findSquareArea(); //function call to the findSquareArea function
}
else if(selection==2) //if user enters 2
{ //computes area of circle by calling findCircleArea
findCircleArea(); //function call to the findCircleArea function
}
else if(selection==3) //if user enters 3
{ //computes area of triangle by calling findTriangleArea
findTriangleArea(); //function call to the findTriangleArea function
}
else if(selection==4) //if user enters 4
{
cout<<"\\Terminating!!."; //exits after displaying this message
break;
}
else // displays the following message if user enters anything other than the given choices
{
cout<<"\\Invalid selection"; //displays this message
}
}
while(1);
}
void findSquareArea(){ //function definition. this function has no parameters
float area=0.0; //variable to store the area of square
float side=0; //local variable of this method that stores value of side of square
cout<<"\\Length of side: "; //prompts user to enter length of side
cin>>side; //reads value of side from user
area=side*side; //formula to compute area of square and result is assigned to area variable
cout<<"\\Area of Square: "<<area<<endl; //displays are of the square
}
void findCircleArea(){ //function definition. this function has no parameters
float area=0.0; //variable to store the area of square
float radius=0.0; //local variable of this method that stores value of radius of circle
cout<<"\\Radius of circle: "; //prompts user to enter radius of circle
cin>>radius; // reads input value of radius
area=PI*radius*radius; //formula to compute area of circle
cout<<"\\Area of circle: "<<area<<endl;} //displays are of the circle
void findTriangleArea(){ //function definition. this function has no parameters
float area=0.0; //variable to store the area of triangle
float base=0.0; //local variable of this method that stores value of base of triangle
float height=0.0; //holds value of height of triangle
cout<<"\\Height of Triangle: "; //prompts user to enter height
cin>>height; //reads input value of height
cout<<"\\Base of Triangle: "; //prompts user to enter base
cin>>base; //reads input value of base
area=0.5*height*base; //computes area of triangle and assign the result to area variable
cout<<"\\Area of Triangle: "<<area<<endl; //displays area of triangle
}
Step-by-step explanation:
The program is modified as above. The prototypes for the four functions that are named as :
void displayMenu(); to display the Menu of choices and call relevant function to compute area
void findSquareArea(); To compute area of square
void findCircleArea(); To compute area of circle
void findTriangleArea(); To compute area of triangle
Note that these methods have no parameters and their return type is void. These 4 methods prototypes are placed above main() function.
Next the function definitions (consisting of a function header and initially empty body) for the four functions are written and placed below main() For example definition of findTriangleArea() is given below:
void findTriangleArea()
{
}
Next the code in the main is moved to the relevant methods. Then variables selection=0; is moved to displayMenu method, radius=0.0 to findCircleArea method , side=0; to findSquareArea float area=0.0; to each method, float height=0.0; and float base=0.0; to findTriangleArea method.
The relevant code moved to each method is shown in the above code.
The definition for the named constant PI is moved out of main and placed above the main function. const keyword before datatype of PI variable shows that PI is declared constant and its value is initialized once only to 3.14159.