Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to compute that volume of a cube or the volume of a sphere. For

ID: 3531141 • Letter: W

Question

Write a program to compute that volume of a cube or the volume of a sphere. For the cube, you will be need to prompt the user for the length of one side of the cube. For the sphere, you will need to prompt the user for the length of the radius. If you do not remember the formulas for the volume, look it up on the Internet. The output should look something like the following (using a precision of two decimal places): Enter c for the volume of a cube, or enter s for the volume of a sphere: s Enter the radius of the sphere: 2.5 The volume of the sphere is: 65.45

Explanation / Answer

#include <iostream> // required for input/output.
#include <math.h> // required for atan() function.

using namespace std; // required for console input/output.

// User-input control function (avoids garbage input).
unsigned int GetNum( char * prompt )
{
int iResult = 0;
cout << prompt << ": ";
while( !( cin >> iResult ) || iResult < 0 )
{
cin.clear();
cin.ignore( BUFSIZ, ' ' ); // If BUFSIZ not defined, use literal constant 512 instead.
cout << "Please enter numeric characters only." << endl << endl;
cout << prompt;
}
return( iResult );
}

// Forward declarations of overloaded functions.
void volume(unsigned int);
void volume(unsigned int,unsigned int);
void volume(unsigned int,unsigned int,unsigned int);

void volume(unsigned int s)
{
unsigned int cube = s*s*s;
cout << "Volume of cube is " << cube << endl << endl;
}

void volume( unsigned int r, unsigned int h)
{
// For the greatest accuracy across all platforms,
// PI is defined as 4 * arctan(1).
float cylinder = (4*atan((float)1)) * r*r*h;
cout<<"Volume of cylinder is " << cylinder << endl << endl;
}

void volume(unsigned int l, unsigned int b, unsigned int h)
{
unsigned int cuboid= l*b*h;
cout << "Volume of cuboid is " << cuboid << endl << endl;
}

int main()
{
unsigned int s;
s = GetNum( "Enter side of cube" );
volume( s );

unsigned int r, h;
r = GetNum( "Enter radius of cylinder" );
h = GetNum( "Enter height of cylinder" );
volume( r, h );

unsigned int l, b;
l = GetNum( "Enter length of cuboid" );
b = GetNum( "Enter breadth of cuboid" );
h = GetNum( "Enter height of cuboid" );
volume( l, b, h );

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote