The output code is at the bottom of main():
The code is commented for my Math teacher; just ignore
Code:
// Pi.cpp : Defines the entry point for the console application.
#define NULL 0x0 //Telling the computer what zero is... because it doesn't know
#include "stdafx.h" //Includes a file into this project that includes MORE files into this project!
using namespace std;
class coord
{
public:
coord(){x = NULL; y = NULL;}
~coord(){}
long double x;
long double y;
};
long double lDpow(long double base,int power);
int main(int argc, char* argv[])
{
coord point; //Point to be calculated
unsigned int r; //The radius of the circle
coord* Points = new coord[3];//Pointer to array of points that is the circle
int PointsPosition = 0; //Current empty slot in the coord array, declared above
double inc = 1; //the incrament at which x values will be calculated
long double circ = NULL; //A quarter of the cirumfrence of our circle
bool FileOut = false;
std::cout.setf(std::ios::showpoint | std::ios::fixed);//Setup i/o parameters
//These next i/o calls prompt the user for information, and take it from the user.
std::cout << "Please input the length of the radius: ";
std::cin >> r;
std::cout << endl;
std::cout << "Please input the resolution of the x values\n (Smaller yeilding more accuracy, .001 to 100): ";
std::cin >> inc;
std::cout << endl;
//This next loop loops through incrimental 'x' values, finding their 'y' couterparts
for(long double loop = 0; loop <= r; loop += inc)
{
point.y = sqrtl( (r * r) - (loop * loop)); //Set y value using the x value, radius and centerpoint (0,0)
point.x = loop; //The x value changes with every cycle of the loop by an incrament of inc.
Points[PointsPosition] = point;//Pass value from the temporary variable to the one that will be used to calculate cirumfrence.
if(loop > 0) //if the loop has run twice or more
circ += sqrtl(lDpow((Points[PointsPosition].x - Points[PointsPosition - 1].x),2) + lDpow((Points[PointsPosition].y - Points[PointsPosition - 1].y),2));
//(above) add to the circumfrence a value equal to the distance between the two most recent points calculated.
PointsPosition++; //Update the variable that keeps track of what slot we are in in the Points[] array (add 1).
//This brilliant little feature below is what allows gigabytes of information to be calculated while only using about 10 bytes.
if(PointsPosition == 2) //If the next slot to be written into is the THIRD slot
{
Points[0] = Points[1]; //these two lines basically reset the array, so we're recycling memory
PointsPosition = 1;
}
}//the end of the loop statement
//output all of out data to the user!
//Remember, only 1/4 of the circumfrence of a circle was calculated, so we must multiply by four.
std::cout << "The circumfrence of your circle (rounded): " << circ*4 << endl;
std::cout << "Pi, according to your circle (c / 2r): " << std::setprecision(25) << ((circ*4) / (r*2)) << endl;
point.~coord();
delete[] Points;
std::cout << endl << "Press any key and enter to exit";
std::cin >> inc;
return 0;
}
long double lDpow(long double base,int power)
{
long double Answer = 1.00;
for(int x = 0; x < power; x++)
{
Answer *= base;
}
return Answer;
}