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

As noted above, you are only required to write functions for this assignment. A

ID: 3529339 • Letter: A

Question

As noted above, you are only required to write functions for this assignment. A driver program that contains the code necessary formain()can be foundat >>> http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm5pt2.cpp. <<< Download the driver program and add your functions to the end of it.

The following functions are required for this assignment:

This function displays a menu and gets a choice from the user. It takes no arguments and returns an integer: the user's menu choice.

After displaying the menu, the function should accept the user's choice (as an integer) and check it for validity - that is did the user enter a value between 1 and 5, inclusive? If not, an error message should be displayed and the user should be given another chance to make a choice - this should continue until the user enters a valid value. Once a valid choice is made, the integer should be returned to the calling function (main()).

The menu should resemble the following:

This function will get an integer value from the user that is within a specified range. It takes three arguments: a string that will be part of the prompt that is displayed to the user, and two integers that represent the lower and upper bound for a specified range. It returns an integer value that is within the specified range.

The function should display a prompt to the user and accept a value. If the value is within the range, it should be returned to the calling function. If the value is invalid, display an error message and ask the user to to re-enter a value - this should be repeated until a valid value is entered.

A calling statement like:

should produce the following prompt to the user:

This function will calculate the surface area of a sphere. It takes one argument: an integer that holds the radius of the sphere. It returns a floating point value which is the calculated surface area.

The formula for calculating the surface area of a sphere is:

This function will calculate the surface area of a rectangular prism. It takes three arguments: an integer that holds the length of the side of the prism, an integer that holds the width of the side of the prism, an integer that holds the height of the side of the prism. It returns a floating point value which is the calculated surface area.

The formula for calculating the surface area of a rectangular prism is:

This function will calculate the surface area of a cone. It takes two arguments: an integer that holds the radius of the base of the cone, and an integer that holds the length of the side of the cone. It returns a floating point value which is the calculated surface area.

The formula for calculating the surface area of a cone is:

This function will calculate the surface area of pyramid. It takes two arguments: an integer that holds the length of the base of the pyramid, and an integer that holds the slant height of the pyramid. It returns a floating point value which is the calculated surface area.

The formula for calculating the surface area of a pyramid is:

Explanation / Answer

/******************************************************************

CSCI 240 Program 5 Part 2 Spring 2013


Programmer: ( o_o)


Section:


Date Due:


Purpose: This program uses functions to calculate the surface

area of various shapes.


It is an exercise in learning to write functions.

******************************************************************/


#include <iostream>

#include <iomanip>


using namespace std;



//Symbolic constant for the value of PI

const int PI=3.14159;




//Function Prototypes

int Menu();

int getValue( string,int,int);

float calcSphere(int);

float calcPrism(int,int,int);

float calcCone(int,int);

float calcPyramid(int,int );



int main()

{

int menuChoice, //The users choice from the menu

radius, //The radius for the sphere and cone

length, //The length of the side of the cone, prism, and pyramid

height, //The height of the side of the prism and pyramid

width; //The width of the side of the prism


float surfArea; //The surface area of all of the shapes



//Print the surface areas with 4 digits after the decimal points


cout << fixed << showpoint << setprecision(4);



//Display the menu to the user and get their choice


menuChoice = Menu();



//While the user does not want to quit


while( menuChoice != 5 ){


//If the user wants to calculate the surface area of a sphere, get the

//radius of the sphere and then display the calculated surface area


if( menuChoice == 1 ){


radius = getValue( "Enter the radius for the sphere", 1, 10 );


surfArea = calcSphere( radius );


cout << endl << "The surface area of a sphere with radius " << radius

<< " is " << surfArea;

}



//If the user wants to calculate the surface area of a cone, get the

//radius of the base and the length of the side of the cone, and then

//display the calculated surface area


else if ( menuChoice == 2 ){

radius = getValue( "Enter the radius of the base of the cone", 1, 7 );


length = getValue( "Enter the length of the side of the cone", 1, 12 );


surfArea = calcCone( radius, length );


cout << endl << "The surface area of a cone with radius " << radius

<< " and side length " << length << " is " << surfArea;

}



//If the user wants to calculate the surface area of a prism, get the

//length, width, and height of the side of the prism, and then display

//the calculated surface area


else if ( menuChoice == 3 ){

length = getValue( "Enter the length of the side of the prism", 1, 20 );

width = getValue( "Enter the width of the side of the prism", 1, 20 );

height = getValue( "Enter the height of the side of the prism", 1, 20 );


surfArea = calcPrism( length, width, height );


cout << endl << "The surface area of the prism with length " << length

<< " width " << width << " and height " << height << " is " << surfArea;

}



//Otherwise, the user wants to calculate the surface area of a pyramid.

//Get the length of the base and the slant height of the pyramid, and

//then display the calculated surface area


else{

length = getValue( "Enter the length of the base of the pyramid", 1, 10 );

height = getValue( "Enter the slant height of the pyramid", 1, 15 );


surfArea = calcPyramid( length, height );


cout << endl << "The surface area of the pyramid with base " << length

<< " and slant height " << height << " is " << surfArea;

}



//Get another menu choice from the user


menuChoice = Menu();

}

return 0;

}




//********** Code the functions below this line **********

/***************************************************************

Function: Menu


Use: displays menu choices and gets valid input between [1,5]


Arguments: none


Returns: choice


Notes: uses getValue as helper function

***************************************************************/


int Menu(){


int choice;


cout<<"Surface Area Calculator"<<endl

<<"1) Sphere"<<endl

<<"2) Cone"<<endl

<<"3) Rectangular Prism"<<endl

<<"4) Pyramid"<<endl

<<endl

<<"5) Quit"<<endl

<<endl;


choice=getValue("Enter your choice ",1,5);


return choice;

}


/***************************************************************

Function: getValue


Use: displays a prompt and loops until a valid choice is entered


Arguments: prompt-a string prompt that asks for an integer number between lowerBound and upperBound


Returns: an integer between [lowerBound,upperBound]


Notes:

***************************************************************/


int getValue( string prompt, int lowerBound, int upperBound ){


int value;


cout<<prompt<<"("<<lowerBound<<" - "<<upperBound<<"): ";

cin>>value;



while((value<lowerBound)|| (value>upperBound)){

cout<<value<<"is an invalid value. Try again:";

cin>>value;

}


return value;

}



/***************************************************************

Function: calcSphere


Use: to calculate surface area of a sphere


Arguments: radius of sphere


Returns: surface are of sphere


Notes: Surface Area of Sphere = 4 * pi * radius^2

***************************************************************/

float calcSphere( int radius ){


return 4.0*PI*radius*radius;

}



/***************************************************************

Function: calcPrism


Use: calculates surface area of prism


Arguments: length, width and height of prism


Returns: surface area of prism


Notes: Surface Area of Rectangular Prism = ( 2 * length * width ) + ( 2 * width * height ) + ( 2 * length * height )

***************************************************************/

float calcPrism( int length, int width, int height ){

return ( 2 * length * width ) + ( 2 * width * height ) + ( 2 * length * height );

}


/***************************************************************

Function: calcCone


Use: calculates surface area of cone


Arguments: radius and length of cone


Returns: surface area of cone


Notes: Surface Area of Cone = ( pi * radius * length ) + ( pi * radius^2 )

***************************************************************/

float calcCone( int radius, int length ){

return ( PI * radius * length ) + ( PI * radius * radius );

}


/***************************************************************

Function: calcPyramid


Use: calculates surface area of pyramid


Arguments: length and slantHeight of pyramid


Returns: surface area of pyramid


Notes: Surface Area of Pyramid = ( 2 * length * height ) + length^2

***************************************************************/


float calcPyramid( int length, int slantHeight ){

return ( 2 * length * slantHeight ) + length*length;

}