Please Help with problem.

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;
}
Last edited on
You have a couple of simple issues.

Line 33: you should continue only while your error_final is ≥ error. You might also want to initialize your error_final to something larger than error before entering the loop.

Line 36: n is in integer, meaning you are doing integer division -- probably not what you want. Change it to angle = 360.0 / n;

Hope this helps.
I tried what you suggested, but now I'm just getting 0.0000 for all my values.
@Duoas probably forgot to mention that "n" is also an integer and that angle = 360.0 / n; is still integer division.

Change "n" to be double - for (double n = 3.0; abs(error_final) <= error; n++)
Last edited on
Still getting zeros.
Works fine for me, even without changing it to double -

Input:
Radius - 2
Error - 3

output:

Computer Area: 5.1993    Error: 7.3607    Number of Iteration: 1
Yeah I don't know what's wrong with mine.

The example output that is included they want:
Radius: 100
Error: 0.05

Computer Area= 31415.8766
Error= 0.0500
Number of Iteration= 2032
I am literally new to this site, I might post my question at the wrong place. I am trying my bets to figure out my homework, I will be posting what i have done so far in code blocks, but I can't build and run it. and also, my professor wants us to include a command sothat his program will automatically grade it but I don't know where to insert it.


Let's write p(n) for the population in year n and let M be the carrying capacity.

The population grows at a rate g, a certain percentage per year, as animals are born or wander into the area. The population grows every year by gp(n) animals.

Some animals will die or wander away every year, even if there is plenty of room left in the habitat. Let h be this minimum loss rate. The population loses hp(n) animals every year.
Verhulst added to this model a new negative term which has the effect of removing excess population when the population grows beyond the carrying capacity M. The Verhulst term is the product of gp(n) -- the number of new animals -- and p(n)/M -- the population expressed as a fraction of the carrying capacity. The product, gp(n)2/M, is our estimate of how many animals will be lost every year due to overcrowding of the habitat.
This is our completed Verhulst formula predicting next year's population from this year's:
p(n+1) = (1+g-h)p(n) - gp(n)2/M.


This is what i have done so far

// This program evaluates the value of Boom and Bust formula for different values.
#include <iostream>
using namespace std ;
int main ()
{ integer M, p(n), p(n+1)
Double g,h,
{p(n+1) = ((1+g-h)*p(n)) - (g*p(n)2/M))
cin >> h=.2
cin >> g=1.25
cin >> M=100
cin >> p(n)=10
cout << p(n+1) << endl;
}}

I've been working on it for a while now, and I've figured it out. Thanks guys for your help!!
Tarik Neaj wrote:
@Duoas probably forgot to mention that "n" is also an integer and that angle = 360.0 / n; is still integer division.

No, I did not forget to mention that because it is untrue. A double divided by an integer is floating point division.

@Spaceman Zeta
Glad you got it working.

@nl1
You are definitely posting in the wrong spot. Go to the Beginner's forum page (http://www.cplusplus.com/forum/beginner/) and click on the New topic button at the top.
Thanks for clarifying, that, didnt know =)
Topic archived. No new replies allowed.