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);
}