This program gives some examples of how one might use pseudo code in a comment. Refer to the Pseudo Code Style Guide for details on the text in bold. This pseudo code is at the level of detail I would expect on an assignment. This program
also purposely lacks function header comments and comments within the code. I want you to focus on the pseudo code
comments.
#include <iostream.h>
#include <iomanip.h>
#include <math.h>
double computeCirclePI(long iterations);
double computeLeibnizPI(long iterations);
void displayHeader(void);
void displayPI(long iterations, double firstPI, double secondPI);
const long MAX_ITERATIONS = 1000000L;
///////////////////////////////////////////////////////////////////
// Function: main
//
// Call displayHeader
// For iterations from 1 to MAX_ITERATIONS (step by factor of 10)
// Call computeCirclePI with iterations returning piCircle
// Call computeLeibnizPI with iterations returning piLeibniz
// Call displayPI with iterations, piCircle, piLeibniz
// End For
//
void main(void)
{
long iterations;
double piCircle, piLeibniz;
displayHeader();
for (iterations = 1; iterations <= MAX_ITERATIONS; iterations *= 10)
{
piCircle = computeCirclePI(iterations);
piLeibniz = computeLeibnizPI(iterations);
displayPI(iterations, piCircle, piLeibniz);
}
}
////////////////////////////////////////////////////////////////
// Function: computeCirclePI
//
// Set radius to 2.0
// Set pi to 0.0
// Compute width
// For index from 1 to iterations
// Calculate x coordinate (midpoint of rectangle)
// Calculate height of rectangle
// Calculate area of rectangle
// Add area to pi
// End For
// Return pi
//
double computeCirclePI(long iterations)
{
const double radius = 2.0;
double x, height, pi = 0.0;
double width, area;
width = radius / iterations;
for (long index = 1; index <= iterations; index++)
{
x = (index * width) - (width / 2);
height = sqrt(radius * radius - x * x);
area = height * width;
pi += area;
}
return pi;
}
////////////////////////////////////////////////////////////////////
// Function: computerLeibnizPI
//
// Set pi to 0.0
// Set denominator to 1.0
// Set index to 1
// While (index less than or equal to iterations)
// Calculate next term
// If (index is odd)
// Add term to pi
// Else
// Subtract term from pi
// End If
// Calculate denominator
// Increment index
// End While
// Multiply pi by 4
// Return pi
//
double computeLeibnizPI(long iterations)
{
double term, pi = 0.0;
long denominator = 1.0, index = 1L;
while(index <= iterations)
{
term = 1.0 / denominator;
if (index % 2)
pi = pi + term;
else
pi = pi - term;
denominator += 2;
index++;
}
return 4 * pi;
}
///////////////////////////////////////////////////////////////
// Function: displayHeader
//
// Display column headings
// Display separator line
//
void displayHeader(void)
{
cout << "Iterations Circle Method Leibniz Method";
cout << endl;
cout << "---------------------------------------------------";
cout << endl;
}
/////////////////////////////////////////////////////////////////
// Function: displayPI
//
// Set cout flags
// Display iterations
// Set precision for floating point
// Display firstPI
// Display secondPI
// Display new line
//
void displayPI(long iterations, double firstPI, double secondPI)
{
cout.setf(ios::fixed|ios::showpoint);
cout << setw(10) << iterations;
cout << setprecision(10);
cout << setw(20) << firstPI;
cout << setw(20) << secondPI;
cout << endl;
}