Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to turn simple math functions into Value-returning functions to find the

ID: 3631358 • Letter: I

Question

I need to turn simple math functions into Value-returning functions to find the dimensions of a circle. How do I turn my code from simple math functions to stand alone user and value returning functions in the following code?


#include <iostream> //Headers
#include <cmath> //Needed for math functions

using namespace std;

int main()
{
double x1, x2, y1, y2, radius, pi = 3.1416; //Initialize the variables for the coordinates that are on the plane and the variables that will be defined via user input and math.

cout << "Enter the coordinates of the center of the circle on the cartesian plane " << endl;
cout << "(No parentheses or commas): ";
cin >> x1 >> y1; //User input of first coordinate
cout << endl;

cout << "Enter the coordinates of the edge of the circle on the cartesian plane " << endl;
cout << "(No parentheses or commas): ";
cin >> x2 >> y2; //User input of second coordinate
cout << endl;

radius = (sqrt((pow(x2 - x1, 2.0)) + (pow(y2 - y1, 2.0))));

cout << "Distance between points (radius): " << radius << endl; // Radius will be printed

cout << "Diameter of circle: " << radius * 2 << endl; //A circle's diameter is just the radius times two

cout << "Circumference of circle: " << 2 * radius * pi << endl; //A Circumference is just the diameter times pi

cout << "Area of circle: " << pi * pow(radius, 2) << endl; //Area is calculated by pi times the radius squared

cout << "Area of 3 dimensional sphere represented by circle: " << 4 * pi * pow(radius, 2) << endl; //Area of a sphere is 4 times pi times the radius squared

cout << "Volume of 3 dimensional sphere represented by circle: " << 1.3333 * pi * pow(radius, 3) << endl; //Volume of sphere is 4/3 times pi times the radius cubed

system("pause");

return 0;
}

Explanation / Answer

#include <iostream>

#define _USE_MATH_DEFINES  //to use the M_PI defined in <cmath>

#include <cmath>

 

using namespace std;

 

int main()

{

   //PROTOTYPES

   double radius(double x1, double y1, double x2, double y2);

   double diameter(double radius);

   double circumference(double radius);

   double area(double radius);

   double surfaceArea(double radius);

   double volume(double radius);

 

   //LOCAL DECLARATIONS

   double x1, x2, y1, y2, r;

 

   //PROCEDURES

   cout << "Enter the coordinates of the center of the circle on the cartesian plane " << endl;

   cout << "(No parentheses or commas): ";

   cin >> x1 >> y1;

   cout << endl;

 

   cout << "Enter the coordinates of the edge of the circle on the cartesian plane " << endl;

   cout << "(No parentheses or commas): ";

   cin >> x2 >> y2;

   cout << endl;

 

   r = radius(x1, y1, x2, y2);  //store radius for later uses

 

   cout << "Distance between points (radius): " << r << endl;

 

   cout << "Diameter of circle: " << diameter(r) << endl;

 

   cout << "Circumference of circle: " << circumference(r) << endl;

 

   cout << "Area of circle: " << area(r) << endl;

 

   cout << "Area of 3 dimensional sphere represented by circle: " << surfaceArea(r) << endl;

 

   cout << "Volume of 3 dimensional sphere represented by circle: " << volume(r) << endl;

 

   system("pause");

 

   return 0;

}

 

//---------------------------------------------------------

// FUNCTION DEFINITIONS

//---------------------------------------------------------

double radius(double x1, double y1, double x2, double y2)

{

   return sqrt((pow(x2 - x1, 2.0)) + (pow(y2 - y1, 2.0)));

}

//---------------------------------------------------------

double diameter(double radius)

{

   return radius * 2.0;

}

//---------------------------------------------------------

double circumference(double radius)

{

   return 2.0 * radius * M_PI;

}

//---------------------------------------------------------

double area(double radius)

{

   return M_PI * pow(radius, 2.0);

}

//---------------------------------------------------------

double surfaceArea(double radius)

{

   return 4.0 * M_PI * pow(radius, 2.0);

}

//---------------------------------------------------------

double volume(double radius)

{

   return 4.0 / 3.0 * M_PI * pow(radius, 3.0);

}