Question

In: Computer Science

Write a program in C++ that solves this problem Calculate the area and volume of a...

Write a program in C++ that solves this problem

Calculate the area and volume of a sphere problem.

Inside a for loop,

vary the radius from 10 to 40  with a step or increment of 5

and calculate the area and volume

Your radius will be equal to your loop counter.

All calculations should have 2 decimal places, but the radius should have zero decimal places and any number of 1,000 or more should have a comma.

Print the radius, area, and volume in a table format, with 3 columns. Note: you don’t have to print grid lines.

Print column headings above the loop and all the calculations inside the loop. The column headings would just be Radius, Area, and Volume. Right justify your data under your column headings

Below the loop, print the sum of the radii, the sum of the areas, and the sum of the volumes. The sum of the areas and the sum of the volumes should have 2 decimal places and commas. The sum of the radii should have zero decimal places.

Answer must be formatted like the following

Radius............Area...............Volume.

10            1,256.64            4,188.79

15            2,827.43           14,137.15

20            5,026.54           33,510.29

25            7,853.97           65,449.79

30           11,309.72          113,097.24

35           15,393.79          179,594.23

40           20,106.18          268,082.35

Totals:

Radius............Area...............Volume..

175           63,774.28          678,059.84

Radius             Area           Volume

       10      1,256.64          4,188.79

       15      2,827.43        14,137.17

       20      5,026.55        33,510.32

       25      7,853.98        65,449.85

       30    11,309.73      113,097.34

       35    15,393.80      179,594.38

       40    20,106.19      268,082.57

Sum of Radii:   175

Sum of Areas:   63,774.33

Sum of Volumes: 678,060.41

Solutions

Expert Solution

#include<iostream>
#include<iomanip>
using namespace std;
//structure to print comma after thousand place
struct comma_facet : public std::numpunct<char>
{ protected: string do_grouping() const { return "\003" ; } };
//driver program
int main()
{
    int i=10,sum=0;
    cout.imbue( locale( cout.getloc(), new comma_facet ) ) ;
    double area,volume,sumarea=0,sumvolume=0;
    //first part output
    //display the heading
    cout<<endl<<"Radius.........Area........Volume";
    //loop to compute the area and volume of sphere starting from radius 10 to 40
    //step value 5
    for(i=10;i<=40;i+=5)
    {
       area = 4 * 3.14159265 * i*i;//compute area
       volume = ((double)4/3) * 3.14159265 * (i*i*i);//compute volume
       sum=sum+i;//find the radius totals
       sumarea=sumarea+area;//compute the area totals
       sumvolume=sumvolume+volume;//compute the volume totals
       //display the details
       cout<<endl<<i<<"\t "<<fixed<<setprecision(2)<<area<<"\t "<<fixed<<setprecision(2)<<volume;
   }
   //display the totals
   cout<<endl<<"Totals : ";
   cout<<endl<<"Radius.........Area........Volume";
   cout<<endl<<sum<<"\t "<<sumarea<<"\t"<<sumvolume;
   //second part output
   sumarea=0;sumvolume=0;//set sumaea and sumvolume to 0
    cout<<endl<<"Radius Area Volume";
    //loop to compute the area and volume of sphere starting from radius 10 to 40
    //step value 5
    for(i=10;i<=40;i+=5)
    {
       area = 4 * 3.14159265 * i*i;
       volume = ((double)4/3) * 3.14159265 * (i*i*i);
       sum=sum+i;//find the radius totals
       sumarea=sumarea+area;//compute the area totals
       sumvolume=sumvolume+volume;//compute the volume totals
       //display the details
       cout<<endl<<i<<"\t "<<fixed<<setprecision(2)<<area<<"\t "<<fixed<<setprecision(2)<<volume;
   }
   //display the totals
   cout<<endl<<"Sum of Radii : "<<sum;
   cout<<endl<<"Sum of Areas : "<<sumarea;
   cout<<endl<<"Sum of Volumes : "<<sumvolume;
}

output


Related Solutions

Write a C++ program to calculate the sphere volume and surface area. Provided that the sphere...
Write a C++ program to calculate the sphere volume and surface area. Provided that the sphere volume = and the sphere surface area = , where r is the radius. 1. Read r from the user using cin statement. 2. Calculate the volume (Vol) and the surface area (Area) of a sphere. 3. Print r, Vol, and Area in a suitable messages.
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem:...
(IN C) Program Question 2: Write a program that solves for c in the Pythagorean Theorem: a2 + b2 = c2 The user will enter values for a and b, and you will calculate c. All of this code will go in just one source file.
1) Write a functional program in Java that can calculate the volume and surface area of...
1) Write a functional program in Java that can calculate the volume and surface area of a sphere and a cube 2) Write a procedural program in Java that can calculate the volume and surface area of a sphere and a cube 3) Write an Object Oriented Program in Java that can find the volume and surface area of a sphere and cube
Part One: Write an interactive program to calculate the volume and surface area of a three-dimensional...
Part One: Write an interactive program to calculate the volume and surface area of a three-dimensional object. Use the following guidelines to write your program: Create a word problem that involves calculating the volume and surface area of a three-dimensional object. Choose one of the following: Cube: surface area 6 s2 , volume s3 Sphere: surface area 4πr2 , volume (4.0/3.0) π r3 Cylinder: surface area 2π r2 + 2 π r h, volume π r2 h Cone: surface area...
Write a program that solves the Knapsack problem. Code to the following standards. Your source of...
Write a program that solves the Knapsack problem. Code to the following standards. Your source of items to put into the knapsack should consist of five randomly generated integers in the range of 1 to 20. Your knapsack can hold 20 lbs.
IN C++ Modify the above program to compute the side area, total area, and volume of...
IN C++ Modify the above program to compute the side area, total area, and volume of a cylinder and the area and volume of a sphere, depending on the choice that the user makes. Your program should ask users to enter 1 to choose cylinder or 2 for sphere, and display an "invalid choice error" for other values. For a cylinder, we want to compute: Side area: (2*PI*r) * h Total Area: 2*(PI*r2) + Side area Volume: (PI*r2)*h For a...
Write a program to calculate the area and circumference of a cuboid. Use printf to display...
Write a program to calculate the area and circumference of a cuboid. Use printf to display 2 digits after decimal point in all the results Program should be in java language.
This exercise requires designing a program which solves the problem described in the problem statement below....
This exercise requires designing a program which solves the problem described in the problem statement below. Provide comments in your pseudo-code and Java program as necessary. Your solution must include these components: UML Class diagram Flowchart Pseudo-code Program Coded Program Output Problem Statement Design a class named Pet, which should have the following fields: name: The name field holds the name of a pet. type: The type field hold the type of animal that a pet is (for example, “dog”,...
using C# Write a program named RectangleArea tocalculate the area of a rectangle with a...
using C# Write a program named RectangleArea to calculate the area of a rectangle with a length of 15.8 and a width of 7.9. The program should have two classes, one is theRectangleArea holding the Main(), and the other one is Rectangle. The Rectangleclass has three members: a property of length, a property of width, and a method with a name of CalArea() to calculate the area. You can invoke the auto-provided constructor or write a self-defined constructor to initialize...
Write a full program that solves the following equation and displays the value for x and...
Write a full program that solves the following equation and displays the value for x and y: 3.4x+50.2y=44.5 2.1x+.55y=5.9
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT