In: Computer Science
Task #1 void Methods
Copy the file geometry.cpp. This program will compile, but when you run it , it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this.
Above the main, write the prototype for a function called PrintMenu that has no parameter list and does not return a value.
Below the main, write the function definition for PrintMenu. It should simply print out instructions for the user with a menu option for the user to choose from. The menu should appear to the user as:
This is a geometry calculator.
Choose what you would like to calculate.
Find the area of a circle
Find the area of a rectangle
Find the area of a triangle
Find the circumference of a circle
Find the perimeter of a rectangle
Find the perimeter of a triangle
Enter the number of your choice:
Add a line in the main method that calls the PrintMenu function as indicated by the comments.
Compile, debug and run. You should be able to choose any option, but you will always get 0 for the answer. We will fix this in the next task.
Task #2 Value-Returning Functions
Write a function prototype and a function definition for CircleArea that takes the radius of the circle and returns the area using the formula A = πr2.
Write a function prototype and a function definition for RectangleArea that takes in the length and width of the rectangle and returns the area using the formula A = lw.
Write a function prototype and a function definition for TriangleArea that takes in the base and height of the triangle and returns the area using the formula A = ½ bh.
Write a function prototype and a function definition for CircleCircumference that takes in the radius of the circle and returns the circumference using the formula C = 2πr.
Write a function prototype and a function definition for RectanglePerimeter that takes the length and the width of the rectangle and returns the perimeter of the rectangle using the formula P = 2l + 2w.
Write a function prototype and a function definition for TrianglePerimeter that takes the lengths of the three sides of the triangle and returns the perimeter of the triangle which is calculated by adding up the three sides.
Task #3 Calling Functions
Add lines in the main which will call the functions. The comments indicate where to place the function calls.
Compile, debug, and run. Test out the program using your sample data.
Task #4 Reusable and Well-documented Functions
Create a header file called formulas.
Remove #pragma once as this is non-standard and will not work in every environment. Add the header file guards
#ifndef FORMULAS_H
#define FORMULAS_H
Cut the function prototypes for the six functions that calculate values from the geometry file and paste after the header file guards.
End the file with #endif
In the geometry file, add a preprocessor directive (after the other #include lines)’
#include “formulas.h”
Add comments for each of the function prototypes you just moved. They should include:
A one-line summary of what the function does.
A description of what the function requires to operate and what the result of that operation is.
A listing of each parameter and what that parameter represents.
If a value is returned, what that value represents
Create a new file called formulas.cpp.
Cut the function definitions that match the prototypes you just moved and paste them into the new file.
Copy the comments from each function prototype to the corresponding function definition.
Compile, debug, and run. It should operate as previously.
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
#include <iostream>
using namespace std;
int main()
{
int choice;
//the user's choice
double value = 0; //the value returned
from the method
char letter; //the Y or
N from the user's decision to exit
double radius; //the
radius of the circle
double length; //the
length of the rectangle
double width; //the
width of the rectangle
double base; //the base
of the triangle
double height; //the
height of the triangle
double side1; //the
first side of the triangle
double side2; //the
second side of the triangle
double side3; //the
third side of the triangle
//do loop was chosen to allow the menu to be
displayed first
do
{
//call the printMenu function
cin >> choice;
switch (choice)
{
case 1:
cout <<
"Enter the radius of the circle: ";
cin >>
radius;
//call the circleArea function and store the result in value
cout <<
"The area of the circle is " << value << endl;
break;
case 2:
cout <<
"Enter the length of the rectangle: ";
cin >>
length;
cout <<
"Enter the width of the rectangle: ";
cin >>
width;
//call the rectangleArea function and store the result in value
cout <<
"The area of the rectangle is " << value << endl;
break;
case 3:
cout <<
"Enter the height of the triangle: ";
cin >>
height;
cout <<
"Enter the base of the triangle: ";
cin >>
base;
//call the triangleArea function and store the result in value
cout <<
"The area of the triangle is " << value << endl;
break;
case 4:
cout <<
"Enter the radius of the circle: ";
cin >>
radius;
//call the circleCircumference function and store the result in value
cout <<
"The circumference of the circle is " << value <<
endl;
break;
case 5:
cout <<
"Enter the length of the rectangle: ";
cin >>
length;
cout <<
"Enter the width of the rectangle: ";
cin >>
width;
//call the
rectanglePerimeter function and store the result in value
cout <<
"The perimeter of the rectangle is " << value <<
endl;
break;
case 6:
cout <<
"Enter the length of side 1 of the triangle: ";
cin >>
side1;
cout <<
"Enter the length of side 2 of the triangle: ";
cin >>
side2;
cout <<
"Enter the length of side 3 of the triangle: ";
cin >>
side3;
//call the trianglePerimeter function and store the result in value
cout <<
"The perimeter of the triangle is " << value <<
endl;
break;
default:
cout <<
"You did not enter a valid choice." << endl;
}
cout << "Do you want to
exit the program (Y/N)? ";
cin >> letter;
} while (letter != 'Y' && letter !=
'y');
}
Here is the solution,
// geometry.cpp file
#include "formulas.h"
#include <iostream>
using namespace std;
int main()
{
// variable decalrations
int choice; //the user's choice
double value = 0; //the value returned from the method
char letter; //the Y or N from the user's decision to exit
double radius; //the radius of the circle
double length; //the length of the rectangle
double width; //the width of the rectangle
double base; //the base of the triangle
double height; //the height of the triangle
double side1; //the first side of the triangle
double side2; //the second side of the triangle
double side3; //the third side of the triangle
//do loop was chosen to allow the menu to be displayed
first
do
{
//call the printMenu function
printMenu();
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the radius of the circle: ";
cin >> radius;
//call the circleArea function and store the result in
value
value = CircleArea(radius);
cout << "The area of the circle is " << value <<
endl;
break;
case 2:
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
//call the rectangleArea function and store the result in
value
value = RectangleArea(length, width);
cout << "The area of the rectangle is " << value
<< endl;
break;
case 3:
cout << "Enter the height of the triangle: ";
cin >> height;
cout << "Enter the base of the triangle: ";
cin >> base;
//call the triangleArea function and store the result in
value
value = TriangleArea(base,height);
cout << "The area of the triangle is " << value
<< endl;
break;
case 4:
cout << "Enter the radius of the circle: ";
cin >> radius;
//call the circleCircumference function and store the result in
value
value = CircleCircumference(radius);
cout << "The circumference of the circle is " << value
<< endl;
break;
case 5:
cout << "Enter the length of the rectangle: ";
cin >> length;
cout << "Enter the width of the rectangle: ";
cin >> width;
//call the rectanglePerimeter function and store the result in
value
value = RectanglePerimeter(length,width);
cout << "The perimeter of the rectangle is " << value
<< endl;
break;
case 6:
cout << "Enter the length of side 1 of the triangle: ";
cin >> side1;
cout << "Enter the length of side 2 of the triangle: ";
cin >> side2;
cout << "Enter the length of side 3 of the triangle: ";
cin >> side3;
//call the trianglePerimeter function and store the result in
value
value = TrianglePerimeter(side1,side2,side3);
cout << "The perimeter of the triangle is " << value
<< endl;
break;
default:
cout << "You did not enter a valid choice." <<
endl;
}
cout << "Do you want to exit the program (Y/N)? ";
cin>>letter;
} while (letter != 'Y' && letter != 'y');
}
// geometry.cpp ends
// your formulas.cpp
#include <iostream>
#include <string>
#include "formulas.h"
using namespace std;
// function to calculate area of circle
// the function requires radius.
float CircleArea(float radius){
return (2 * 3.14 * radius * radius);
}
// function to calculate are of rectangle
// the function requires length and width
float RectangleArea(float length, float width){
return length * width;
}
// function to calculate area of triangle
// the function requires base and height
float TriangleArea(float base, float height){
return (1/2)* base * height;
}
// function to calculate circumference of circle
// the function requires radius
float CircleCircumference(float radius){
return (2 * 3.14 *radius);
}
// function to calculate perimeter of rectangle
// the function requires length and width
float RectanglePerimeter(float length, float width){
return (2*length) + (2*width);
}
// function to calculate perimeter of triangle
// the function requires 3 sides.
float TrianglePerimeter(float side1, float side2, float side3){
return side1 + side2 + side3;
}
// function to display the menu
void printMenu()
{
cout<<"\n\nThis is a geometry calculator."<<endl;
cout<<"Choose what you would like to
calculate."<<endl<<endl;
cout<<"1) Find the area of a circle"<<endl;
cout<<"2) Find the area of a rectangle"<<endl;
cout<<"3) Find the area of a triangle"<<endl;
cout<<"4) Find the circumference of a
circle"<<endl;
cout<<"5) Find the perimeter of a
rectangle"<<endl;
cout<<"6) Find the perimeter of a
triangle"<<endl;
cout<<"\n\nEnter the number of your choice:";
}
// formulas.cpp ends here
// your formulas.h
#ifndef FORMULAS_H
#define FORMULAS_H
void printMenu();
float CircleArea(float );
float RectangleArea(float , float);
float TriangleArea(float, float );
float CircleCircumference(float);
float RectanglePerimeter(float, float );
float TrianglePerimeter(float , float , float );
#endif
// formulas.h ends here
here is the sample output:-
Thank You.