In: Computer Science
Exercise 5.5
PART A
Copy or cut and paste program P5_2.cpp (located below) to a new program called ex55.cpp. Make PI a constant value. Compile and run the program for the following values:
r = 2 cm, h = 10 cm
The correct answer should be:
The cross-section area of the
cylinder is 3.89556 inch-sq
The side area of the cylinder is
19.4778 inch-sqr
Did you get the correct answer? You didn't! Explain the reason for this logic error in your report. Now add the instruction that fixes the problem (yes, you need just one instruction) and save your file in ex55.cpp file. HINT: r=r*0.3937; //converting r to inch
PART B
Modify the ex55.cpp to include a new function called total_area that computes the total surface area of a cylinder. The total surface area is the sum of the side area and cross-section area. Call your new program ex56.cpp.
For r=2 and h=10, the total area must be: 23.373383 inch-sqr.
Program P5_2.cpp:
// P 5_2.cpp This program illustrates the local and global
variables and call-by-value.
// This program computes the side area and the cross section area
of a cylinder
#include
#include
using namespace std;
//Let’s declare first any global constant, if any required
// This variable is defined globally, i.e. it is known to all functions in this program as PI
// To declare a global constant you must write it outside the main() function
const double PI = 3.14159;
//Now we declare any programmer defined function
double cross_area(double
r); //
Function prototype for function cross_area
double side_area(double r, double
h); //
Function prototype for function Side_area
// Start defining the main function
int main(void)
{
double h, r; //variables local to the main
function
cout << "Enter the radius
and the height of the cylinder in Cm ";
cin >> r >> h;
cout << endl;
cout << "Before I do any
computation or call any function, I want to let you know that
\n";
cout << "you have entered r =
" << r << " and h = " << h << "." <<
endl;
cout << "I am planning to use
inch, thus in the first function, I will convert r, and " <<
endl;
cout << "in the second one I
will convert h \n";
cout << "The cross section
area of the cylinder is " << cross_area(r) << "
inch-sqr" << endl;
cout << "The side area of the
cylinder is " << side_area(r,h) << " inch-sqr
\n\n";
return 0;
}
// Definition of all programmer defined functions
double cross_area(double r)
{
//Cross section area includes the disks at
the bottom and the top
r = r * 0.3937; // converting r
to inch
return 2*PI*pow(r,2);
}
double side_area(double r, double h)
{
double area; //variable local to
side_area function
h = h * 0.3937; // converting h to
inch
area = 2*PI*r*h;
return area;
}
Solution:
PART A
#include<bits/stdc++.h>
using namespace std;
const double PI = 3.14159;
//Now we declare any programmer defined function
double cross_area(double r); // Function prototype for function
cross_area
double side_area(double r, double h); // Function prototype for
function Side_area
// Start defining the main function
int main(void)
{
double h, r; //variables local to the main function
cout << "Enter the radius and the height of the cylinder
in Cm ";
cin >> r >> h;
cout << endl;
cout << "Before I do any computation or call any function, I
want to let you know that \n";
cout << "you have entered r = " << r << " and h =
" << h << "." << endl;
cout << "I am planning to use inch, thus in the first
function, I will convert r, and " << endl;
cout << "in the second one I will convert h \n";
cout << "The cross section area of the cylinder is "
<< cross_area(r) << " inch-sqr" << endl;
cout << "The side area of the cylinder is " <<
side_area(r,h) << " inch-sqr \n\n";
return 0;
}
// Definition of all programmer defined functions
double cross_area(double r)
{
//Cross section area includes the disks at the bottom and the
top
r = r * 0.3937; // converting r to inch
return 2*PI*pow(r,2);
}
double side_area(double r, double h)
{
double area; //variable local to side_area function
h = h * 0.3937; // converting h to inch
//new line which is added is below
r=r*0.3937; //converting r to inch
area = 2*PI*r*h;
return area;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
PART B
#include<bits/stdc++.h>
using namespace std;
const double PI = 3.14159;
//Now we declare any programmer defined function
double cross_area(double r); // Function prototype for function
cross_area
double side_area(double r, double h); // Function prototype for
function Side_area
double total_area(double r, double h); // Function prototype for
function total_area
// Start defining the main function
int main(void)
{
double h, r; //variables local to the main function
cout << "Enter the radius and the height of the cylinder
in Cm ";
cin >> r >> h;
cout << endl;
cout << "Before I do any computation or call any function, I
want to let you know that \n";
cout << "you have entered r = " << r << " and h =
" << h << "." << endl;
cout << "I am planning to use inch, thus in the first
function, I will convert r, and " << endl;
cout << "in the second one I will convert h \n";
cout << "The cross section area of the cylinder is "
<< cross_area(r) << " inch-sqr" << endl;
cout << "The side area of the cylinder is " <<
side_area(r,h) << " inch-sqr \n";
cout << "The total surface area of the cylinder is
"<<setprecision(8) << total_area(r,h) << "
inch-sqr \n\n";
return 0;
}
// Definition of all programmer defined functions
double cross_area(double r)
{
//Cross section area includes the disks at the bottom and the
top
r = r * 0.3937; // converting r to inch
return 2*PI*pow(r,2);
}
double side_area(double r, double h)
{
double area; //variable local to side_area function
h = h * 0.3937; // converting h to inch
r=r*0.3937; //converting r to inch
area = 2*PI*r*h;
return area;
}
double total_area(double r, double h)
{
double area= side_area(r,h)+cross_area(r); // calculating the total
surface area
return area;
}