So I need some help with this problem. I need to calculate the area of a circle by doing this:
If the user selects option A, implement the Inscribed Polygons method as follows:
Initialize a variable called n to 3
Compute angle as angle=360 / nsides
Compute the area of the polygon by adding up the areas of all triangles, as
area=n×1/2 r^2 sin(angle)
*note: you must convert angle to radian in order to use trig functions.
The equation to compute the error is given below.
error=|area-π∙r^2 |
If the absolute value of error is less than or equal to the acceptable error entered by user then consider the current area value as close enough to be the real area of the circle. Otherwise, increase n by 1 and then loop to step 2.
Display the computed area, the absolute value of error, and the number of iterations it took to reach the solution.
So this is what I've come up with, but it gives me bad values or errors. I've cut some things out that I know work. Can anyone tell me what I'm doing wrong? Thank you in advance!!
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
|
double radius, error, angle, angle_final, area, error_final;
int count;
char method_choice = 'x';
char rerun_choice = 'z';
cout << showpoint << fixed;
cout << setprecision(4);
radius = 0;
angle = 0;
count = 0;
angle_final = 0;
area = 0;
error_final = 0;
cout << "Compute Area of a Circle" << endl << endl;
cout << "Enter Radius: ";
cin >> radius;
cout << "Enter Acceptable Error: ";
cin >> error;
cout << "Select a method for computation:" << endl;
cout << "A. Use Inscribed Regular Plygons" << endl;
cout << "B. Use Circumscribed Regular Polygons" << endl;
cout << "C. Use Middle Rectangles" << endl;
cout << "Enter Choice: ";
cin >> method_choice;
method_choice = toupper(method_choice);
switch (method_choice)
{
case 'A':
for (int n = 3; abs(error_final) <= error; n++)
{
count++;
angle = 360 / n;
angle_final = angle * (M_PI / 180);
area = (n * (0.5 * pow(radius, 2.0)) * sin(angle_final));
error_final = abs(area - M_PI*pow(radius, 2.0));
}
cout << "Computed Area: " << area << " Error: " << error_final << " Number of Iteration: " << count << endl;
break;
}
|