Task: Write two short programs in C++ . The first one will use procedural progra
ID: 2247727 • Letter: T
Question
Task: Write two short programs in C++. The first one will use procedural programming and the second one will use object-oriented programming (classes). Both programs will accept the radius and height of a right circular cylinder from the user and will calculate the volume and total surface area of the cylinder. Program A: Use at least one function with the procedural program.
Program B: Declare a cylinder class, and then define an object that is an instance of that class. Create appropriate accessor and mutator functions for the class. Both programs: Input at least 3 sets of radius/height data (within a loop), and for each set of data, display the radius, height, volume and surface area of the cylinder. The user may choose the input data. Output: Both programs may display the results to the monitor.
Formulas: Use the formula for Volume of a right circular cylinder and Total surface area:
If your program does not compile and run, it will not be graded!! Make sure your programs are well documented both internally and externally.
Explanation / Answer
Procedural code:
#include <iostream>
#include <math.h>
using namespace std;
double volume(double radius, double height){
return 3.14* pow(radius,2)* height;
}
double surfacearea(double radius, double height){
return 2*3.14* radius * (radius + height);
}
int main()
{
int radius=0;
int height=0;
while(true){
cout << "Enter radius" << endl;
cin>>radius;
cout << "Enter height" << endl;
cin>>height;
cout<<"Volume:"<<volume(radius,height)<<endl;
cout<<"Surface Area:"<<surfacearea(radius,height)<<endl;
}
return 0;
}
Output:
Enter radius
1
Enter height
1
Volume:3.14
Surface Area:12.56
Enter radius
2
Enter height
2
Volume:25.12
Surface Area:50.24
Enter radius
3
Enter height
3
Volume:84.78
Surface Area:113.04
Object oriented:
#include <iostream>
#include <math.h>
using namespace std;
class Cylinder{
double radius;
double height;
Cylinder(){}
public:
Cylinder(double radius,double height){
this->radius=radius;
this->height=height;
}
double volume(){return 3.14*pow(radius,2)*height;}
double surfacearea(){return 2*3.14*radius*(radius+height);}
};
int main()
{
double radius=0;
double height=0;
while(true){
cout << "Enter radius" << endl;
cin>>radius;
cout << "Enter height" << endl;
cin>>height;
Cylinder c(radius,height);
cout<<"Volume:"<<c.volume()<<endl;
cout<<"Surface Area:"<<c.surfacearea()<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.