Thread: Area of Circle Without Pi

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    South Windsor, Connecticut, United States
    Posts
    4

    Area of Circle Without Pi

    Hi,

    I am new to the forums and new to C++ and need some help. I was given an assignment for class to calculate the area of a circle using only the radius as a user input and not using Pi in the code. I need to do this by calculating the areas of a series of rectangles under the curve and adding them together. Using nested loops to continuously reduce the size of these rectangles until the approximated area value is within a margin of error less than 0.1%.

    I've spent hours trying to figure out how to get this done and so far this is what ive come up with but I don't know what im doing wrong... If someone could please help me out id greatly appreciate it!


    Code:
    #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 <= 0.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 > 0.1) {
    oldvalue=newvalue;
    }
    
    
    n++;
    
    
    
    
    }
    // Result
    area = 2*newvalue;
    
    
    cout << "\nThe estimated area of the circle is: " << area << endl;
    
    
    system("pause");
    return(0);
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If computing the areas of multiple rectangles to estimate the area of a circle, you need to make sure the rectangles don't overlap.

    Adding up the areas of overlapping rectangles will give an area that exceeds the area they actually cover.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    South Windsor, Connecticut, United States
    Posts
    4
    That makes sense... Is there something in my code I need to remove or add to prevent this from happening? This is all very new to me and I just wanna pass my midterm .... Thanks!

  4. #4
    Registered User
    Join Date
    Mar 2013
    Location
    South Windsor, Connecticut, United States
    Posts
    4
    Also... Dont know if it helps but 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

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I wouldn't do it by rectangles. I would do it using 2^n triangles.
    Initially calculate 4x the area of a triangle that covers 1/4 of the area, using Heron's Formula.
    Then each iteration step, split the triangle into half its size, project the new point to the circumference, calculate the new area of the smaller triangle using 8x the area given by Heron's formula.
    The attached image shows the second and third iterations.
    etc.Area of Circle Without Pi-circle-jpg
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Mar 2013
    Location
    South Windsor, Connecticut, United States
    Posts
    4
    Thanks... I appreciate the help, and while using triangles seems a lot more understandable and easy to me, this being my midterm, I'd rather learn what I'm doing wrong and correct it, then modify the assignment that was given.

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    1
    Jhernandez860- do you have the completed C++ program?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Do not bump threads. (Bumping: Posting messages on threads to move them up the list or to post on a thread that has been inactive for two weeks or longer).
    Announcements - C++ Programming

    The purpose of these board is not for other people to do your homework for you. Try things out work on your own, homework has a purpose. If you still have trouble with a specific piece of code or concept please feel free to ask. But please do not ask people to do your entire homework for you, it simply annoys people most of the time.
    Announcements - General Programming Boards

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with area of circle program
    By Drezden in forum C Programming
    Replies: 4
    Last Post: 01-27-2013, 07:42 PM
  2. calculating area of a circle using user-defined function
    By jackson6612 in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2011, 03:17 PM
  3. Area of a circle error, help
    By spazx in forum C Programming
    Replies: 16
    Last Post: 09-14-2010, 06:25 PM
  4. area of a circle
    By wise_ron in forum C Programming
    Replies: 2
    Last Post: 10-02-2006, 03:15 PM
  5. Circle Area
    By Zophixan in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2002, 12:50 PM