Finding Area of Circle Without Pi

Hi,
I am taking an intro C++ class and was running into a little trouble in one of my assignments. I need to write a code to calculate the area of a circle without using Pi, only by using the radius which is input by the user. Using nested loops it will continually reduce the size of these rectangles until the approximated area value is within a margin of error less than 0.1%.

Below is a copy of my code... I feel like I am on the right track but am not sure where I'm messing up... If anyone could help out I'd greatly appreciate it!!



#include<iostream>
#include<cmath>

using namespace std;

int main ()
{
// Initialize
double radius, difference, newvalue, oldvalue, area, n(2), i, square;

cout << "Enter the radius of the circle...\n";

cout << "Radius: ";
cin >> radius;

oldvalue=((radius*radius)*3)/2;

difference = abs(((newvalue - oldvalue/oldvalue)))*100;

// Summon Loop
while (difference <= .1)
{
for (i=1; i==n; i++) {

square = (radius / n) * (sqrt((radius*radius) - (i*(radius / n)*(i*(radius / n)))));

newvalue += square;

}

// Check Loop
difference = abs(((newvalue - oldvalue)/oldvalue))*100;

if (difference > .1) {
oldvalue=newvalue;
}

n++;


}
// Result
area = 2*newvalue;

cout << "\nThe estimated area of the circle is: " << area << endl;

system("pause");
return(0);

}
When I enter a radius of 1, difference becomes 100, the loop never initiates, and area gets the value of 2 time junk.
Any idea on what I need to fix? Im still new to all of this :-/
I'm not too sure about the algorithm, so I'm not much help there.

This line is trouble:
difference = abs(((newvalue - oldvalue/oldvalue)))*100;

First, newvalue is uninitialized, so thats a problem.

This:
newvalue - oldvalue/oldvalue
The division is done first, you might as well do:
newvalue - 1
Thanks for pointing that out... I have a feeling I have a problem with 2 lines

oldvalue=((radius*radius)*3)/2;

difference = abs(((newvalue - oldvalue/oldvalue)))*100;

But I am unsure on how to start the loop when the first time the old value isn't established.

If it helps you, to help me, lol.. these are the equations I was given...

Area under a curve.... y=√(r^2-x^2 )

Margin of Error..... % Error=|(New Value-Old Value)/(Old Value)|×100%

Finding the area of the recatangles.... ∆X=r/n




One thing in the above version, while (difference <= .1), the condition is the wrong way round, it should be while (difference > .1)

I just had a go at this, and used a do-while loop, so that the condition is tested after the first pass.

But I am unsure on how to start the loop when the first time the old value isn't established.

Just use a primitive estimate, assume the shape is a square rather than a circle, then oldvalue = 4*radius*radius;

I'm less sure about the correctness of the mathematics, I worked this out from scratch using x2 + y2 = radius2 and a method I'd previously tried for integration using rectangles.

My equations looked somewhat different to this:
 
square = (radius / n) * (sqrt((radius*radius) - (i*(radius / n)*(i*(radius / n)))));

I think that line may be wrong.

One other point, each time around the while loop, newvalue needs to be initialised to zero before using it to accumulate the total area.
Last edited on
Here:

The bolded part is optional. Total time programming = 5 min

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;

#define PI 3.14159265

void calculate(double radius)
{
    double Result = ((9.0/4.0) + (sqrt(3) / 2)) * radius * radius;
    double realResult = PI * radius * radius;

    printf("The area of the circle with radius %lf is %lf.\n",radius,Result);
    printf("The real area of the circle is %lf.",realResult);
}

int main(int nNumberofArgs,char* pszArgs[])
{
    double radius;
    cout << "Enter radius: ";
    cin >> radius;

    calculate(radius);

    system("PAUSE");
    return 0;
}
@greenleaf800073 I'm not sure what you just posted, but it isn't in any sense an answer to the original question. The requirement is to approximate the area using a series of rectangles, see this page for example, the diagrams give the idea:
http://www.maa.org/joma/volume7/aktumen/Rectangle.html
Um, oh rectangles, woops, but mine works though
Topic archived. No new replies allowed.