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 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 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 program that draws a fixed circle centered at (100, 60) with radius 50. Whenever...
Write a program that draws a fixed circle centered at (100, 60) with radius 50. Whenever the mouse is moved, display a message indicating whether the mouse point is inside the circle at the mouse point or outside of it. Write in Java please.
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given...
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given triangle ABC; this circle passes through points A, B, and C. These points will be specified by the user by clicking the mouse button. Remember, the three perpendicular bisectors of the three edges of a triangle all pass through one point, the circumcenter, which is the center of the circumscribed circle.
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given...
Write a program that draws the circumscribed circle (also known as the circumcircle) of a given triangle ABC; this circle passes through points A, B, and C. These points will be specified by the user by clicking the mouse button. Remember, the three perpendicular bisectors of the three edges of a triangle all pass through one point, the circumcenter, which is the center of the circumscribed circle.
Write a Java program for RSA encryption that has the following inputs and outputs: Given a...
Write a Java program for RSA encryption that has the following inputs and outputs: Given a message and an integer n = pq where p and q are odd primes and an integer e > 1 relatively prime to (p − 1)(q − 1), encrypt the message using the RSA cryptosystem with key (n, e).
Write a program Write a program whose inputs are three integers, and whose output is the...
Write a program Write a program whose inputs are three integers, and whose output is the smallest of the three values. Ex: If the input is: 7 15 3 the output is: 3 C++ please
(Display a Circle and Its Attributes) Write a program that displays a circle of random size...
(Display a Circle and Its Attributes) Write a program that displays a circle of random size and calculates and displays the area, radius, diameter and circumference. Use the following equations: diameter = 2 × radius, area = π × radius2, circumference = 2 × π × radius. Use the constant Math.PI for pi (π). All drawing should be done on a subclass of JPanel, and the results of the calculations should be displayed in a read-only JTextArea.
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs...
Using Python. Write a program that reads a sequence (unknown number of inputs) of integer inputs and prints the number of even and odd inputs in the sequence. please explain. Thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT