LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Simple polygon area c++ prog. problems (https://www.linuxquestions.org/questions/programming-9/simple-polygon-area-c-prog-problems-223434/)

Hal 08-28-2004 05:48 AM

Simple polygon area c++ prog. problems
 
I made a c++ program a while ago, but never did understand why it only partly worked.

Code:

# include <iostream>
# include <cmath>
using namespace std;

double angle(int sides);
double area(double tan_angle, int sides);

int main () {
        int sides;
        double base_angle;
        double deg_to_rad;
        double tan_angle;

        cout << "Sides of polygon: ";
        cin >> sides;
        if (sides < 0) { 
                cout << "The shape cannot have minus sides.";
                return 0;
        }
        base_angle = angle(sides);
        deg_to_rad = 3.1415926536/180;
        tan_angle = tan(base_angle * deg_to_rad);


        cout << "The base angles of the shape's isoscles triangles are: " << base_angle;
        cout << " degrees \n";
        cout << "The tangent of the one of these angles is:            " << tan_angle << "\n";
        cout << "The area of the polygon is:                            " << area(tan_angle, sides) << " m^2" << "\n";
       
return 0;
}

double angle(int sides) {
        return 90-180/sides;
}
double area(double tan_angle, int sides) {
        double area;
        double height;
        double base;
 

        height = 500/sides * tan_angle;
        base = 1000/sides;
        area = base * height / 2;
        return area * sides;

}

Don't worry too much about the maths, as I think it is a problem elsewhere.

I enter in 5 at a DOS screen (I just happen to be using Windows), and get the correct info back. The area of a regular 5 sided poly of perimeter 1000m is 68819.1 m^2.

Quote:

Sides of polygon: 5
The base angles of the shape's isoscles triangles are: 54 degrees
The tangent of the one of these angles is: 1.37638
The area of the polygon is: 68819.1 m^2
Now when I enter in 6, I get something which doesn't match up to my hand calculations.

Quote:

Sides of polygon: 6
The base angles of the shape's isoscles triangles are: 60 degrees
The tangent of the one of these angles is: 1.73205
The area of the polygon is: 71592.6 m^2
The area of a 6 sided poly of perimetre 1000m is not 71592.6. It is more like 72168.

I'm still a newb to programming, so any help as to why this is happening would be gladly accepted.

Stor_G 08-28-2004 06:12 AM

Hi
me and math don't really go well together, but there's something i noticed in your code:
Quote:

Code:

height = 500/sides * tan_angle;

in general, when doing a multiple operators calculation, it's better to add parenthesis to define precedence. even if the natural precedence is the requested one.
there are 2 reasons for that:

1) to increase readability of the code.
if someone else reads the code, they know that you indeed meant what you wrote and not did a mistake(and when debugging someone else's code something like that can cause a LOT of trouble based on my experience)

2) to make sure you don't accidently ignore the precedence of a certain operator. for example, i once ignored the precedence of the operator & as a bitwise AND. something which caused my program to return erroneous results in several locations.

i don't know if it has anything to do with the problem you have, but IMHO i believe that's a more clear and safe way to write code.

Hal 08-28-2004 06:33 AM

Thanks for the pointer, I added some parenthesis to my code, if anyone as a notion of what is going wrong and wants me to post the code with parenthesis just say the word. However, the problem of erroneous returns is still there. I'm compiling with the Borland bcc32 btw.

kev82 08-28-2004 07:06 AM

by Hal
height = 500/sides * tan_angle;
base = 1000/sides;


youve been caught out by integer division, an expression evaluates to the type of its most precise component, your dividing two ints and thus getting an int back, either make the numbers floating point ie 500.0, 1000.0 or cast them to floats/doubles

as a side note your formula can be simplified, generally the less fp math you do the more accurate your results so you might want to use

area = 250000*tan(angle)/n

Hal 08-28-2004 08:09 AM

Aha! Thank you Stor_G and Kev82! I made my formula "dumb" so that I could make sure that things were working before I simplified it. I can cut down a lot of maths now. (: Again, thanks a lot, I never did get it working until now.


All times are GMT -5. The time now is 06:17 PM.