• Forum
  • Lounge
  • This program will find the area of a cir

 
This program will find the area of a circle without pi!

Pages: 1234
Plus/minus 1% ^_^

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
31
#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(6) / 2.747319)) * 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);
    cout << endl;
}

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

    calculate(radius);

    system("PAUSE");
    return 0;
}
Alright... Though, not to be negative, but that's more complex then Pi. And you can always simulate something by making it more complex. The key thing is to try to create an easier equation. Otherwise what's the point? You're re-inventing a worse wheel.
-_-


But the point is, what if you weren't allowed to use pi?
Why are you posting this in the lounge?
Um, because I don't really need help on this... well idk.
How is this not using pi? The former equation just uses an obfuscated estimation of pi. The latter uses an easily readable estimation to pi.
How about an integral? http://ideone.com/nr6jbm
instead of using PI = ((9.0/4.0) + (sqrt(6) / 2.747319)), you can just use PI = 355/113 ~.~


an implementation using Simpsons method:
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

double integralSimpsonsCircle(double r, double epsilon=1e-6);

int main()
{
   double r = 4.0;

   cout << "The integral is " << fixed << setprecision(15)
        << integralSimpsonsCircle(r, 1e-8)
        << endl;
   cout << fixed << setprecision(15) << r*r*3.141592653589793;

   //prints
   //The integral is 50.265482455014443
   //50.265482457436690
}

//---------------------------------------------------------
double integralSimpsonsCircle(double r, int n)
{
   if (n%2) ++n;
   double h = 2*r/n;
   double sum = 0; 
   double x = -r + h;
   double r2 = r*r;
   for (int i = 1; i < n; ++i, x+=h)
      sum += (i%2 ? 4 : 2) * sqrt(r2 - x*x);
   return 2*sum*h/3;
}
//---------------------------------------------------------
double integralSimpsonsCircle(double r, double epsilon)
{
   int n = 2;
   double s1 = integralSimpsonsCircle(r,n);
   double s2 = integralSimpsonsCircle(r,n<<=1);

   while (abs(s2-s1) > epsilon)
   {
      s1 = s2;
      s2 = integralSimpsonsCircle(r,n<<=1);
   }

   return s2;
}
Last edited on
closed account (D80DSL3A)
I saw that particular homework thread. As was pointed out there the method sought is to approximate the area as a sequence of narrow rectangles.
Using mid-point approximation over 2,000 rectangles gives a result accurate to 0.0015% regardless of the radius entered.
The value of PI is used only for finding the "exact" value of the area for comparison with the calculated value. No approximation of PI is used in the calculation.
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
#include <iostream>
#include<cmath>
using namespace std;

double PI = 3.141592654;

int main()
{
    double r;
    cout << "r = "; cin >> r;
    double dx = r/1000.0;// divide into 2,000 rectangles
    double area = 0.0;

    for( double x = dx/2.0 - r; x<=r - dx/2.0; x+=dx )
    {
        double y = sqrt(r*r - x*x);
        area += 2*y*dx;
    }

    cout << "est. area = " << area << '\n';
    cout << "actual area = " << PI*r*r;

    cout<< '\n';
	return 0;
}

The full problem required finding the smallest # of rectangles needed to achieve 0.1% accuracy.

@tntxtnt I see your method isn't using any approximation for PI either. Nice job.
Last edited on
Finding the area of a circle without PI can certainly be done. It's called finding PI.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

3.1415926535897932384626433832795028841971693993751058209749
445923078164062862089986280348253421170679821480865132823066
470938446095505822317253594081284811174502841027019385211055
596446229489549303819644288109756659334461284756482337867831
652712019091456485669234603486104543266482133936072602491412
737245870066063155881748815209209628292540917153643678925903
600113305305488204665213841469519415116094330572703657595919
530921861173819326117931051185480744623799627495673518857527
248912279381830119491298336733624406566430860213949463952247
371907021798609437027705392171762931767523846748184676694051
320005681271452635608277857713427577896091736371787214684409
012249534301465495853710507922796892589235420199561121290219
608640344181598136297747713099605187072113499999983729780499
510597317328160963185950244594553469083026425223082533446850
352619311881710100031378387528865875332083814206171776691473
035982534904287554687311595628638823537875937519577818577805
321712268066130019278766111959092164201989


Just because I was bored....

Anyways, I don't see the point of this. Why would you not be able to use 3.141592, or at the worse case you don't remember what PI is; 355/113 or 22/7 (though this one is the least accurate). At any rate, isn't it easier (or simpler) to enter a const variable than to enter an equation.
Last edited on
You do do 2 times the area under the curve of the square root of x squared plus r squared, which is taking the integral like naraku9333 suggested. It never even does anything that involves calculating any approximation of PI - you could approximate PI from the result but the process used doesn't ever involve any approximation of PI.
Why not just use Euler's identity: A = (ln(-1)/i)r^2?
@chrisname
Is that formula correct, AFAIK you can't take the log of a negative number.
Last edited on
@chrisname that works but that's still using PI.
Last edited on
Not directly :)
It evaluates an approximation of PI and uses it, so yes, directly.
You can't ever use PI in a computer program because it cannot fit in a finite amount of space.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdio>
#include <cmath>

int main() {

    const int DIM = 10000;
    long squareUnits = 0;

    for (long i = 0; i < DIM; ++i)
        for (long j = 0; j < DIM; ++j)
            if (sqrt( (double) ( i*i + j*j ) ) <= DIM)
                ++squareUnits;

    squareUnits = (squareUnits - DIM) * 4 - 3;

    double radius;
    scanf("%lf", &radius);

    double area = squareUnits / ((DIM / radius) * (DIM / radius));
    printf("radius: %lf\narea: %.8lf\n", radius, area);
}


@greenLeaf
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <math.h>

cstdio and stdio are pretty much the same thing, except you're supposed to use cstdio for C++ and stdio.h for C. You should also use cmath instead of math.h for C++. And it's cleaner to stick to either printf/scanf or cin/cout instead of a mixture of both.
Last edited on
Pages: 1234