//////////////////////////////////////////////////////////////
//
// Student Name: Bobbi Flekman
// Student ID: 123-45-6789
// Course: CS161
// Assignment: #4
// Due Date: July 31, 1997 at 6:00 pm
//
// File: program4.cpp
//
// Description: This program approximates pi using the
// algorithm described below
//
//////////////////////////////////////////////////////////////
// This program will calculate the value of PI according to the
// following logic:
//
// If the area of a circle = PI * radius squared, and we have a
// circle where the radius is 2, then the area of that circle = PI * 4.
// Therefore, the area of 1/4 of that circle = PI.
//
// Now we need to calculate the area of 1/4 of the circle. We'll do that
// by drawing rectangles side-by-side inside that quarter circle, calculating
// the area of each, and adding them together.
//
// So ... to find the area of each rectangle, we multiply length times
// width. The width is always the same -- the radius of the circle divided
// by the # of rectangles -- and the length is chosen so that the circle
// passes through the midpoint of the top of the rectangle. That is, the
// length is the square root of the radius (squared) * y (squared)
// [solving the distance formula, x2 + y2 = r2] - and y is different for
// each rectangle.
// Includes that allow setprecision, screen input/output, and sqrt
#include <iomanip.h>
#include <iostream.h>
#include <math.h>
// We've pre-set the values of these, so they are user-defined constants
// Note that RADIUS cannot change or the program will not calculate PI!!!!
const double RADIUS = 2;
const double NUMBER_OF_RECTANGLES = 1000;
// Begin main program
void main (void)
{
// So first we calculate the area of 1 rectangle
// Remember, the area of a rectangle = length * width
// We already know the width, so let's initialize that variable
double width = RADIUS / NUMBER_OF_RECTANGLES;
// We'll run a "for" loop that will calculate the y of
// each rectangle (the midpoint, as I mentioned above) and use it to
// calculate the length of each rectangle. The loop will run 1000 times.
// Let's declare the length
double length;
// We'll use the length and the width to calculate the area of each
// rectangle, so let's declare area
double area_of_rectangle;
// And the sum of all the areas of all the rectangles will be the
// area of the quarter circle. Let's initialize that and set it to 0
// (so we begin counting at 0).
double area_of_quarter_circle = 0;
// Now... First thing: calculate the value of the radius squared
double radius_squared = pow(RADIUS, 2);
// Here comes the loop, running 1000 times and incrementing by 1.
// length = square root of r2 - midpoint2
// n is just a placeholder
for (int n = 1; n <= NUMBER_OF_RECTANGLES; n++)
{
// Calculate the value of the midpoint
double midpoint = ((width / 2) + ((n - 1) * width));
// Calculate the value of the midpoint squared
double midpoint_squared = pow(midpoint, 2);
// Finally we are ready to calculate length
length = sqrt(radius_squared - midpoint_squared);
// Calculate the area of one rectangle
area_of_rectangle = length * width;
// Sum the areas of the rectangles to get the area of the
// quarter circle
area_of_quarter_circle = area_of_quarter_circle + area_of_rectangle;
}
// Now print the answer, to 4 decimal places
cout << "The value of PI is " << setprecision(5);
cout << area_of_quarter_circle << "." << endl;
}
// end of program
Output:
The value of PI is 3.1416.