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...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area...
C++ Write a program that displays the follow menu: Geometry Calculator    1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle    3. Calculate the Area of a Triangle 4. Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle then display it's area using the following formula: area = PIr2 Use 3,14159 for PI and the radius of the circle for r. If the...
Programming Language C++ Task 1: Write a program to calculate the volume of various containers. A...
Programming Language C++ Task 1: Write a program to calculate the volume of various containers. A base class, Cylinder, will be created, with its derived classes, also called child classes or sub-classes. First, create a parent class, Cylinder. Create a constant for pi since you will need this for any non-square containers. Use protected for the members. Finally, create a public function that sets the volume. // The formula is: V = pi * (r^2) * h Task 2: Create...
Write a program in C that computes the area of a circle (Area = pi *...
Write a program in C that computes the area of a circle (Area = pi * r2) and the volume of a sphere (Volume = 4/3 * pi * r3). Both formulas use r which is the radius. Declare a float variable pi = 3.14159. Get the value of r from the keyboard and store it in a float variable. Display both the area of the circle and the volume of the sphere.
Write most basic C++ program possible that calculates volume and surface area of five shapes (Cube,...
Write most basic C++ program possible that calculates volume and surface area of five shapes (Cube, Sphere, Prism, Cylinder, Cone), where each shape has its own class (with a volume function, surface area function, constructor, as well as get and set functions), and the shape dimensions are private class members. Include input and display functions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT