In: Computer Science
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).
//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: