A student in Programming 1 wants to know the surface area and volume of a cylindrical shape.
Figure 5.1: Example of a cylinder
Volume = PI(R^²) x height
Area = 2PI(R^²) x height + 2PI(R^²)
5.1.1 Write a program that will assist the students:
5.1.2Create a C++ source file called Cylinder and save it in a file called Cylinder.cpp.
5.2 Create the following functions:
5.2.1 calcVolume() This function will receive three parameters and it must then calculate
and return the volume of a cylinder using the information given
above.
5.2.2 calcArea() This function will receive three parameters and it must then calculate
and return the area of a cylinder using the information given above.
5.2.3 main() NB: The functions must be implemented above the main.
Add all necessary pre-processor directives.
Declare all constants and necessary variables.
Prompt the user for the height of the cylinder, and the radius of
the base of the cylinder.
Based on the users option :
o Do the relevant calculations by calling the correct
function.
Display the results of the desired measurement.
If an invalid option is selected an appropriate error message
must be displayed, see Figure 5.5.
NB: Use a switch for the selection
o This process must be repeated until there no calculations to
perform (N/n).
#include <iostream>
//*****************************************************************
using namespace std;
const double PI = 3.145;
//*****************************************************************
double Volume(double R, double h)
{
return PI*R*R*h;
}
//*****************************************************************
double Area(double R, double h)
{
return 2*PI*R*(h + R);
}
//*****************************************************************
int main()
{
cout << "Enter Cylinder radius: ";
double R;
cin >> R;
cout << "Enter Cylinder height: ";
double h;
cin >> h;
cout << "*****MENU*****" << endl;
cout << "1. Find Cylinder volume." << endl;
cout << "2. Find Cylinder area." << endl;
cout << "What is your choice (1 or 2)? ";
int choice;
cin >> choice;
if(choice == 1) cout << "Volume of this Cylinder is: " << Volume(R, h) << endl;
if(choice == 2) cout << "Area of this Cylinder is: " << Area(R, h) << endl;
if(choice != 1 && choice != 2) cout << "You enter wrong option." << endl;
return 0;
}
Comments
Leave a comment