C++ The volume of a cone is given by the formula: V = r2 h / 3 For the value of
ID: 3692070 • Letter: C
Question
C++
The volume of a cone is given by the formula:
V = r2 h / 3
For the value of use:
const double PI = 3.14159265358979323846;
Declare a structure named: Cone
containing:
Create the following functions:
main
Contains three variables:
A pointer named ptr which points to a Cone structure
A double named height
A double named radius
Uses new to obtain space for the data structure
Calls the the input, setUp, and output functions
Deletes the space that was obtained using new
input:
Takes the height of the cone and radius of the base as reference parameters
Reads the height and radius from the user
Has a return type of void
setUp:
Takes three parameters by value: height, radius, and a pointer to the Cone
Puts the data into the data structure
Has a return type of void
getVolume:
Takes one parameter by value: a pointer to the Cone
Computes the volume
Returns the volume
output:
Takes one parameter by value: a pointer to the Cone
Calls the getVolume function to get the volume
Prints the height, radius, and volume in a neat format
Has a return type of void
Put the main function first.
Use the function and variable names specified above.
Arrange the functions in the order listed above.
Explanation / Answer
#include<iostream>
using namespace std;
const double PI = 3.14159265358979323846;
struct Cone{
double height;
double radius;
};
void input(double &, double &);
void setUp(Cone *ptr, double , double);
void output(Cone *ptr);
double getVolume(Cone *ptr);
int main(){
Cone *ptr;
double height, radius;
ptr = new Cone;
input(height, radius);
setUp(ptr, height, radius);
output(ptr);
return 0;
}
void input(double &height, double &radius){
cout<<"Enter height: ";
cin>>height;
cout<<"Enter radius: ";
cin>>radius;
}
void setUp(Cone *ptr, double height, double radius){
ptr->height = height;
ptr->radius = radius;
}
double getVolume(Cone *ptr){
return PI*ptr->height*ptr->radius*ptr->radius/3;
}
void output(Cone *ptr){
cout<<"Height: "<<ptr->height<<endl;
cout<<"Radius: "<<ptr->radius<<endl;
cout<<"Volume: "<<getVolume(ptr)<<endl;
}
/*
Output:
Enter height: 12
Enter radius: 34
Height: 12
Radius: 34
Volume: 14526.7
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.