Question

Problem: Write a short C++ program that gets the side of a cube and the radius...

Problem: Write a short C++ program that gets the side of a cube and the radius of a sphere from the keyboard and writes to a file the surfaces of these shapes.

           

------------------------------------------------------------------------------------------------------------------------

Your task: implement in C++ the algorithm solution shown below.

------------------------------------------------------------------------------------------------------------------------

Part A (79 points)

Algorithm solution (in pseudocode):

To accomplish this task, your program will have to take the following steps:

1. Declare a variable named outFile that represents an output stream.

2. Declare a named constant PI that holds value 3.141592.

3. Declare variables named side, radius, s_cube, and s_sphere that hold double precision real numbers.

4. Open the file output.txt.

5. Prompt the user to “Enter side of cube: “.

6. Read from keyboard the value entered by the user and store it in side.

7. Prompt the user to “Enter radius of sphere: “.

8. Read from keyboard the value entered by the user and store it in radius.

9. Calculate the surface of the cube using the formula 6 * s 2 (where s is the side of the cube)

and assign the result to s_cube.

10. Calculate the surface of the sphere using the formula 4 * pi* r 2 (where r is the radius of the sphere) and assign the result to s_sphere.

11. Format the output to display the numbers in fixed format with two decimal digits.

12. Print to output.txt the message

“The surface of a cube of sides “,side, “ is “, s_cube.

“The surface of a sphere of radius “,radius, “ is “,s_sphere.

13. Close the file.

To calculate the squares of a number you must define a value-returning function named square( ) that receives a real number and returns its square as a real number. Then use it to calculate the squares of side and radius. Do NOT use pow() in your program.

------------------------------------------------------------------------------------------------------------------------

Part B (10 points)

Modify the above program so that function square( ) rounds off the value to the nearest one hundredths (second decimal digit) before returning it.

------------------------------------------------------------------------------------------------------------------------

Part C (11 points)

Modify the above program so that, instead of calculating the surfaces in main( ) you use a couple of value_returning functions named cube_surf( ) and sphere_surf( ) to do it. The former must receive the side of the cube while the latter must receive the radius of the sphere.

------------------------------------------------------------------------------------------------------------------------

Example:

Enter side of cube: 2.123456789 (entered by the user at the keyboard)

Enter radius of sphere: 3.987654321 (entered by the user at the keyboard)

Content of output.txt after running part A solution

The surface of a cube of sides 2.12 is 27.05

The surface of a sphere of radius 3.99 is 199.82

            Your program must compile without errors or warnings.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question.


Here is the completed code for this problem.

Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change.

If you are satisfied with the solution, please rate the answer. Thanks


===========================================================================

#include<iostream>
#include<fstream>

#include<iomanip>
using namespace std;


// PART C
double cube_surf(double side){
   return 6*side*side;
}

double sphere_surf(double radius){
   const double PI = 3.141592;
   return 4*PI*radius*radius;
}

int main(){
  
   ofstream outFile;
  
   double side, radius, s_cube,s_sphere;
   outFile.open("output.txt");
  
   cout<<"Enter side of cube: "; cin>>side;
   cout<<"Enter radius of sphere: ";cin>>radius;
  
   s_cube = cube_surf(side);
   s_sphere = sphere_surf(radius);
  
   // PART B the below line square off to 2 decimals
   outFile<<fixed<<setprecision(2)<<showpoint;
   outFile<<"The surface of a cube of sides "<<side<<" is "<<s_cube<<endl;
   outFile<<"The surface of a sphere of radius "<<radius<<" is "<<s_sphere;
   outFile.close();
  
}

===========================================================================

thanks : )

Add a comment
Know the answer?
Add Answer to:
Problem: Write a short C++ program that gets the side of a cube and the radius...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program named program51.py that defines a value-returning function named cuber that returns both the...

    Write a program named program51.py that defines a value-returning function named cuber that returns both the surface area and volume of a cube. A cube is a rectangular prism with all sides equal. Prompt the user to enter the cube's side length from the keyboard in the main function and then call the cuber function. Moving forward for all assignments and for this program, all other code would be in a main function, called at the very end of your...

  • Problem: Write a program to calculate the force of gravitational attraction between two objects of known...

    Problem: Write a program to calculate the force of gravitational attraction between two objects of known mass at a known distance. Use the formula developed by Isaac Newton known as Law of Universal Gravitation. F G*m *m2/d2 Where F (Force of gravity) is expressed in Newtons (N), mi and m2 (masses of the objects) are expressed in kilograms (kgs) and d (distance from the center of one object to the center of the other) is expressed in meters (m) and...

  • c++ I am suppose to write a function that gets a number from a user, say...

    c++ I am suppose to write a function that gets a number from a user, say 3.123456789 and then the program gets another number from the user which is used to find out how many decimal digits we want rounded to. So if the user inputs 3 then the number would turn into 3.123 then we are suppose to add two digits next to the rounded number so it would turn into 3.12300. the rounding is suppose to be done...

  • 5. (30 pts) Write a program that calculates the surface area of a cube (see the...

    5. (30 pts) Write a program that calculates the surface area of a cube (see the formula below). The user inputs the cube's side length and your program will then print out the surface area of that cube. Your code must follow the output in the sample run below. Your program must be complete and correct, but you do not need to include comments. Here is the formula to calculate surface area given a cube's side length: surface area 6...

  • Problem 3 (5 points) Write a program that prompts the user for the radius of a...

    Problem 3 (5 points) Write a program that prompts the user for the radius of a sphere. Your program should then compute values for the area, and volume of the sphere. Your program should then output the results for the sphere area, and volume. Enter the radius of the sphere as a real number? 10.5 The area of a sphere with radius 10.5 is 1385.44 The volume of a sphere with radius 10.5 is 4849.05

  • Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent...

    Implement the Shape hierarchy -- create an abstract class called Shape, which will be the parent class to TwoDimensionalShape and ThreeDimensionalShape. The classes Circle, Square, and Triangle should inherit from TwoDimensionalShape, while Sphere, Cube, and Tetrahedron should inherit from ThreeDimensionalShape. Each TwoDimensionalShape should have the methods getArea() and getPerimeter(), which calculate the area and perimeter of the shape, respectively. Every ThreeDimensionalShape should have the methods getArea() and getVolume(), which respectively calculate the surface area and volume of the shape. Every...

  • Menu-driven programs will display the menu options to the user and then prompt them for a...

    Menu-driven programs will display the menu options to the user and then prompt them for a menu choice. This program will display the following menu in the following format: Calculator Options: Calculate the area of a circle Calculate the area of a rectangle Calculate the area of a triangle Calculate the area of a trapezoid Calculate the area of a sphere Exit Enter your choice (1-6) Once the user enters a choice for the menu, the program should use a...

  • C++ Programming question Problem: 5. Write a program that reads in a list of integers into...

    C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...

  • package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task...

    package _solution; /** This program demonstrates how numeric types and operators behave in Java Do Task #1 before adding Task#2 where indicated. */ public class NumericTypesOriginal { public static void main (String [] args) { //TASK #2 Create a Scanner object here //identifier declarations final int NUMBER = 2 ; // number of scores int score1 = 100; // first test score int score2 = 95; // second test score final int BOILING_IN_F = 212; // boiling temperature double fToC;...

  • Write a C++ program to analyze a variety of triangles. The program should determine all angles...

    Write a C++ program to analyze a variety of triangles. The program should determine all angles and all sides for a triangle for three different options (give the user a menu of choices): 1) Given two sides and the angle between 2) Given two angles and one side 3) Given three sides More details for each option is provided below. Option 1: Given two sides and the angle between First check to be sure that the three values entered are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT