Modularizing a Program with Value-Returning Functions For this lab exercise you
ID: 3832112 • Letter: M
Question
Modularizing a Program with Value-Returning Functions For this lab exercise you will make additional improvements to the areas program you worked on in PA 4, PA 5.and earlier in this lab. In your PA6 folder make a copy of your areas3.cpp file. Name it areas4.cpp Remove choice.cpp from the project and add the areas 4.cpp program to the project. Copy the get choice function you just wrote in the choice.cpp file for the PA 6.4 exercise and paste it below the displayMenu function definition in the areas4.cpp file. Add a function prototype for the getchoice function at the top of the program where the other prototypes are located. Now, change the following line of code in main cin NestedGreaterGreater choice; to choice = getChoice (1, 4); This will ensure that choice is assigned a value between 1 and 4 Therefore the final else if can be removed from the if/else if statement that controls the branching. After doing this, test the program to make sure everything works so far, before going on to the next step. Now, make the findsquareArea, findCircleArea, and findTriangleArea functions into value-returning functions. They should each return a double value. Change their function headers and function prototypes to indicate this. Then, instead of having them print the area, have them return the area they have computed. Finally, change the call to each of these functions in main so that the value returned by the function call will be printed. For example, you will change if (choice ==1) find SquareArea(); to if (choice == 1) cout NestedLessLess "Area = " NestedLessLess find SquareArea ()NestedLessLess endl; Compile the code, fixing any errors until it compiles without errors. Then test it. Make sure it runs correctly for all menu choices.Explanation / Answer
/*areas4.cpp*/
#include<iostream>
#include<math.h>/*used for sqrt*/
#include<stdlib.h>/*used for exit(0).*/
/*functions prototype*/
void displayMenu();
double findSquareArea();
double findCircleArea();
double findTriangleArea();
int getChoice(int,int);
int main(void)
{
int choice;
displayMenu();
choice=getChoice(1,4);
if(choice==1)
{
std::cout<<"Area="<<findSquareArea();
}
else if(choice==2)
{
std::cout<<"Area="<<findCircleArea();
}
else if(choice==3)
{
std::cout<<"Area="<<findTriangleArea();
}
else
{
exit(0);
}
return 1;
}/*end of main*/
/*findSquareArea function which return area.(double)*/
double findSquareArea()
{
double side;
std::cout<<"Enter The Side:";
std::cin>>side;
return side*side;
}
/*findCircleArea function which return area(double)*/
double findCircleArea()
{
double radius;
std::cout<<"Enter The Radius:";
std::cin>>radius;
return 3.14*radius*radius;
}
/*findTriangleArea function retuning area(double)*/
double findTriangleArea()
{
double side1,side2,side3,s,Area;
std::cout<<"Enter The Three Sides of Triangle:";
std::cin>>side1;
std::cin>>side2;
std::cin>>side3;
s=(side1+side2+side3)/2;
Area=sqrt(s*(s-side1)*(s-side2)*(s-side3));
return Area;
}
/*getChoice function as per given in problem statement*/
int getChoice(int i,int j)
{
int choice;
std::cin>>choice;
if(choice>=i&&choice<=j)
return choice;
}
/*display menu function*/
void displayMenu()
{
std::cout<<"Area 1.Area of Square 2.Area of Circle 3.Area of Triangle 4.Exit Enter Your Choice:";
}
/***********OUTPUT***************
Area
1.Area of Square
2.Area of Circle
3.Area of Triangle
4.Exit
Enter Your Choice:3
Enter The Three Sides of Triangle:5
7
9
Area=17.4123
--------------------------------
Process exited after 7.927 seconds with return value 1
Press any key to continue . . .
********************************/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.