//////////////////////////////////////////////////////////////
//
// Student Name: Derek Smalls
// Student ID: 123-45-6789
// Cource: CS161
// Assignement: #4
// Due Date: 7-31-97 (Thur)
//
// File: program4.cpp
//
// Description: The program approximates the value of pi.
// The algorithm calculates pi by approximating
// the area of a circular arc. And for extra
// credit, the pi also can be approximated by
// a formula derived by Gottfried Leibniz.
//
//////////////////////////////////////////////////////////////
#include <iostream.h> // cin, cout
#include <iomanip.h> // setprecision
#include <math.h> // sqrt(), pow()
//Function declarations
double computeCirclePI ( long lcIterations ); //function to approximate
//pi value using circle
//method
double computeLeibnizPI ( long lcIterations ); //function to approximate
//pi value using Leibniz's
//formular
void displayPItable (long lcIterations, //function to display
double lcApproxCirclePI, //calculated pi values
double lcApproxLeibnizPI); //depending on different
//iteration numbers
// Global constant variable declaration
const long MAX_NUM_OF_ITERATION = 7L; //Number of different
//executions - 7 times for
//this assignment
//1, 10, 100,... 1000000
const long ITER_INCREMENT_FACTOR = 10L; //Above iteration
//incremeted by
//power of 10
const double RADIUS = 2.0; //Radius of circle in use
/////////////////////////////////////////////////////////////////
//
// main function start
//
/////////////////////////////////////////////////////////////////
void main (void)
{
//Local variable Definition
int iterIndex; //iteration numbers
double approxCirclePI; //approximated PI value by circle
//method
double approxLeibnizPI; //approximated PI value calculated
//by Leibniz'formular
long iterations = 1; //Initialize the iteration - start from 1
//Display the header of tables
cout << setw (11) << "Iterations"; //Reserve the space
//and display the text
cout << setw (20) << "Circle Method";
cout << setw (20) << "Leibniz Method" << endl;
//Header separator
cout << "-----------------------------------------------------" << endl;
//for loop to try 7 different iteration numbers
for (iterIndex = 1; iterIndex <= MAX_NUM_OF_ITERATION; iterIndex++)
{
//Call the function to calculate the pi using circle method
approxCirclePI = computeCirclePI ( iterations );
//Call the function to calculate the pi using Leibniz's formular
approxLeibnizPI = computeLeibnizPI ( iterations );
//Once calculated pi values are retunrd, diplay both pis from
//two different function
displayPItable (iterations, approxCirclePI, approxLeibnizPI);
//Before try to next number iteration, increment the iteration by
//power of ten
iterations = iterations * ITER_INCREMENT_FACTOR;
}
}
//////////////////////////////////////////////////////////////
//
// Function: computeCirclePI
//
// Input: lcIterarions - number of iterations
// Output: calcuated PI value from rectangle area method
//
// Description: The function calculates the value of PI
// using sumation of area for given iteration
// numbers of rectangles
//
/////////////////////////////////////////////////////////////
double computeCirclePI ( long lcIterations )
{
// Local variable definition
double circlePI = 0.0; //PI value using circle method
double width, halfWidth; //width and half of width for rectangle
double height; //Height of rectangle
double xDistance; //The x-axis to mid point of rectangle
double xDistanceSquare; //the square of x value
double areaOfRectangle; //area of reactangle
double radiusSquare; //square of radius
long iterIndex; //iteration index
width = RADIUS / lcIterations; //width of rectagle is given by
//radis divided by total number of
//iteration
halfWidth = width / 2; //haldwidth id given by width divided
//by 2
radiusSquare = RADIUS * RADIUS; //calculate Radius * Radius
//for loop to iterate by given iteration numbers
//start the iteration from 1 to given lcIteration number
//increment iteration by 1 whenever it loops
for (iterIndex = 1; iterIndex <= lcIterations; iterIndex++)
{
//calculate horizon distance to the midpoint of rectangle
xDistance = width * ( iterIndex - 1 ) + halfWidth;
//calculate the square of x value
xDistanceSquare = xDistance * xDistance;
// ________
// / 2 2
//height is given by the formular h = '\/ r - x
//
height = sqrt (radiusSquare - xDistanceSquare);
//calculate the area of rectangle
areaOfRectangle = width * height;
//PI is summation of areas of all finite rectangles
circlePI = circlePI + areaOfRectangle;
}
//Once the calculation is done, returns the calculatye PI
//value
return circlePI;
}
//////////////////////////////////////////////////////////////
//
// Function: computeLeibnizPI
//
// Input: lcIterations - number of iterations
// Output: computed Leibniz PI value
//
// Description: The function calculates the value of PI
// using Leibniz's method. The formular is
// given as a following form
//
// pi/4 = 1 - 1/3 + 1/5 - 1/7 + 1/9 + ...
//
/////////////////////////////////////////////////////////////
double computeLeibnizPI ( long lcIterations )
{
// Local variable definition
double quarterPI = 0.0; //quarter of PI value
double leibnizPI; //calculated leibniz PI value
long iterIndex = 1; //iteration Index
while (iterIndex <= lcIterations ) //begining of while loop
{ //until iterIndex <= iteration numbers
quarterPI = quarterPI + //calculate quarter PI
(pow (-1.0, iterIndex + 1) //alteranting signs by
//powering -1
* (1 / double(2 * iterIndex - 1))); //Denomiators are only
//odd numbers 1, 3, 5...
//(2n-1) where n is 1,
//2, 3,... will provide
//odd number series
iterIndex++; //Increment iteration
//index by 1
} //End of while loop
leibnizPI = 4 * quarterPI; //Mutiply quarter PI by 4 to get
//whole PI value
return leibnizPI; //Returns the calculated PI value
}
//////////////////////////////////////////////////////////////
//
// Function: displayPITable
//
// Input: lcIterarions - number of iterations,
// Approximated Circle PI value,
// Approximated Leibniz PI value
// Output: no retrun value
//
// Description: The function displays estimated PI calculated
// by both circle method (summation of rectangle
// area) and Leibniz's formula
//
/////////////////////////////////////////////////////////////
void displayPItable (long lcIterations,
double lcApproxCirclePI,
double lcApproxLeibnizPI)
{
cout.setf (ios::showpoint); //These will force to show
cout.setf (ios::fixed); //the 0s and decimal points
//Reserve 20 spaces and set 10 decimal points
//then display number of Iterations
cout << setw (11) << setprecision (10) << lcIterations;
//Reserve 20 space again then displays Appoximated PI value
//calcualted using Circle method
cout << setw (20) << lcApproxCirclePI;
//Reserve 20 space again then displays Appoximated PI value
//calcualted using Leibniz's formula
cout << setw (20) << lcApproxLeibnizPI << endl;
}
Output:
Iterations Circle Method Leibniz Method
-----------------------------------------------------
1 3.4641016151 4.0000000000
10 3.1524114333 3.0418396189
100 3.1419368579 3.1315929036
1000 3.1416035449 3.1405926538
10000 3.1415929980 3.1414926536
100000 3.1415926645 3.1415826536
1000000 3.1415926539 3.1415916536