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).
5.3 Re-write program 5.2.
5.3.1 calcValues() This function will receive four parameters and it must then calculate
and the volume and the area of a cylinder using the information given
above.
5.3.2 promptValues() This function will receive three parameters and it must then prompt
the user for the required input as indicated in Figure 5.2.
5.3.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.
Calculation the volume and area by calling the correct
function.
Display the results the volume and area.
#include <iostream>
//*****************************************************************
using namespace std;
const double PI = 3.145;
//*****************************************************************
double calcVolume(double R, double h)
{
return PI*R*R*h;
}
//*****************************************************************
double calcArea(double R, double h)
{
return 2*PI*R*(h + R);
}
//*****************************************************************
void promptValues(double &h, double &R)
{
cout << "Enter Cylinder radius: ";
double RT;
cin >> RT;
cout << "Enter Cylinder height: ";
double hT;
cin >> hT;
R = RT;
h = hT;
}
//*****************************************************************
void calcValues(double h, double R)
{
cout << "Volume of this Cylinder is: " << calcVolume(R, h) << endl;
cout << "Area of this Cylinder is: " << calcArea(R, h) << endl;
}
//*****************************************************************
int main()
{
double R, h;
promptValues(h, R);
calcValues(h, R);
return 0;
}
Comments
Leave a comment