Question

In: Computer Science

Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this...

Write a program, circleref.cpp, that inputs a double radius of circle. It should print, given this radius, the circumference of the circle, the area of the circle, the surface area of a sphere with that radius and the area of a sphere with that radius. Each of its computation functions returns its answers in call by reference parameters. Here are the formulas for computation:

Circumference = 2 • π • r Circle Area = π • r2
Sphere Surface Area = 4 • π • r2 Sphere Volume = 4/3 • π • r3

Define a constant for π before main in your program: const double pi = 3.14159265358; You are required to write functions that do the following:

  • ☺ compute the circumference of a circle given the radius (parameter) and return the circumference in a call by reference parameter (void function).

  • ☺ compute the area of a circle given the radius (parameter) and return the circumference in a call by reference parameter (void function).

  • ☺ compute the sphere surface area given the radius (parameter) and return the sphere surface area in a call by reference parameter (void function).

  • ☺ compute the sphere volume given the radius (parameter) and return the sphere volume in a call by reference parameter (void function).

  • ☺ print the results given the values of the circumference, area, surface, and volume (four parameters, no return value)

  • A sample run might be (user input in bold): Enter a radius: 7.5

Circumference .... 47.124
Circle Area ...... 176.715
Sphere Surface ... 706.858
Sphere Volume .... 1767.146

The output should follow the format shown above (values printed with 3 digits after the decimal, in a field width of 10 characters).

Solutions

Expert Solution

//circleref.cpp : C++ program to calculate the circumference, area of the circle and surface area and volume of the sphere for given radius
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

// constant for pi
const double pi = 3.14159265358;

// function declaration
void circle_circumference(double radius, double& circum);
void circle_area(double radius, double& area);
void sphere_surface_area(double radius, double& area);
void sphere_volume(double radius, double& volume);
void print(double circum, double area, double s_area, double volume);

int main()
{
double radius, area, circum, s_area, volume;
// input the radius
cout<<"Enter a radius: ";
cin>>radius;
// compute circumference, area of circle
circle_circumference(radius, circum);
circle_area(radius, area);
// compute surface area, volume of sphere
sphere_surface_area(radius, s_area);
sphere_volume(radius, volume);
// display the results
print(circum, area, s_area, volume);
return 0;
}

// function to calculate and return the circumference of the circle
void circle_circumference(double radius, double& circum)
{
circum = 2*pi*radius;
}

// function to calculate and return the area of the circle
void circle_area(double radius, double& area)
{
area = pi*pow(radius,2);
}

// function to calculate and return the surface area of the sphere
void sphere_surface_area(double radius, double& area)
{
area = 4*pi*pow(radius, 2);
}

// function to calculate and return the volume of the sphere
void sphere_volume(double radius, double& volume)
{
volume = 4*pi*pow(radius,3)/3;
}

// function to display the circumference, area of circle and surface area and volume of sphere
void print(double circum, double area, double s_area, double volume)
{
cout<<fixed<<setprecision(3);
cout<<left<<setw(15)<<"Circumference"<<right<<setw(10)<<circum<<endl;
cout<<left<<setw(15)<<"Circle Area"<<right<<setw(10)<<area<<endl;
cout<<left<<setw(15)<<"Sphere Surface"<<right<<setw(10)<<s_area<<endl;
cout<<left<<setw(15)<<"Sphere Volume"<<right<<setw(10)<<volume<<endl;
}

//end of program

Output:


Related Solutions

Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Class Write a Circle class that has the following member variables: • radius: a double...
Circle Class Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the...
Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the...
Circle Radius: double Circle() Circle(newRadius: double) getArea(): double setRadius(newRadius: double): void getRadius(): double After creating the Circle class, you should test this class from the main() method by passing objects of this class to a method “ public static void printAreas(Circle c, int times)” You should display the area of the circle object 5 times with different radii. 2 java.util.Random +Random() +Random(seed: long) +nextInt(): int +nextInt(n: int): int +nextLong(): long +nextDouble(): double +nextFloat(): float +nextBoolean(): boolean Constructs a Random object...
Write a Circle Class that has the following fields: • radius: a double • PI: a...
Write a Circle Class that has the following fields: • radius: a double • PI: a final double initialized with the value 3.14159 • Constructor. Accepts the radius of the circle as an argument • Constructor. A no-arg constructor that sets the radius field to 0.0. • setRadius. A mutator method for the radius field. • getRadius. An accessor method for the radius field. • getArea. Returns the area of the circle, which is calculated as area = PI *...
Write a C++ program that prompts the user for the radius of a circle and then...
Write a C++ program that prompts the user for the radius of a circle and then calls inline function circleArea to calculate the area of that circle. It should do it repeatedly until the user enters -1. Use the constant value 3.14159 for π Sample: Enter the radius of your circle (-1 to end): 1 Area of circle with radius 1 is 3.14159 Enter the radius of your circle (-1 to end): 2 Area of circle with radius 2 is...
Write a Circle class that has the following member variables: radius as a double PI as...
Write a Circle class that has the following member variables: radius as a double PI as a double initialized with 3.14159 The class should have the following member functions: Default constructor Sets the radius as 0.0 and pi as 3.14159 Constructor Accepts the radius of the circles as an argument setRadius A mutator getRadius An accessor getArea Returns the area of the circle getDiameter Returns the diameter of the circle getCircumference Returns the circumference of the circle Write a program...
Write a Circle class that has the following member variables: • radius: a double • pi:...
Write a Circle class that has the following member variables: • radius: a double • pi: a double initialized with the value 3.14159 The class should have the following member functions: • Default Constructor. A default constructor that sets radius to 0.0. • Constructor. Accepts the radius of the circle as an argument. • setRadius. A mutator function for the radius variable. • getRadius. An accessor function for the radius variable. • getArea. Returns the area of the circle, which...
PHP programming language Circle Area, Circumference and ratio:  For a circle with a given radius, write a...
PHP programming language Circle Area, Circumference and ratio:  For a circle with a given radius, write a program to compute 1) area of the circle, 2) circumference of the circle and 3) the ratio of the circumference to its diameter.  You will use 3 different values of radius a) 10cm, b) 50 cm, and c) 100cm for your computations. The output should print or echo the following statements identifying the writer of the program and the computation for each value of the...
Write c code program for the following Write a function, circle, which takes the radius of...
Write c code program for the following Write a function, circle, which takes the radius of a circle from the main function and assign the area of the circle to the variable, area, and the perimeter of the circle to the variable, perimeter. Hint: The function should have three variables as input. Since the contents of the variables are to be modified by a function, it is necessary to use pointers. Please type out the full usable program. Thank you.
Write a Java program named CircleZapper that displays a circle with a radius of 10 pixels,...
Write a Java program named CircleZapper that displays a circle with a radius of 10 pixels, filled with a random color at a random location on the screen. When you click the circle, it disappears and a new random color circle is displayed at another random location (see display below). After twenty circles are clicked, display the time spent in the pane. To detect whether a point is inside the circle, use the contains method defined in the Node class....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT