LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-28-2004, 05:48 AM   #1
Hal
Member
 
Registered: Oct 2003
Location: East Anglia, England
Distribution: Dapper Drake / Slackware
Posts: 151

Rep: Reputation: 30
Question 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.
 
Old 08-28-2004, 06:12 AM   #2
Stor_G
Member
 
Registered: Aug 2004
Posts: 50

Rep: Reputation: 15
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.
 
Old 08-28-2004, 06:33 AM   #3
Hal
Member
 
Registered: Oct 2003
Location: East Anglia, England
Distribution: Dapper Drake / Slackware
Posts: 151

Original Poster
Rep: Reputation: 30
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.
 
Old 08-28-2004, 07:06 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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
 
Old 08-28-2004, 08:09 AM   #5
Hal
Member
 
Registered: Oct 2003
Location: East Anglia, England
Distribution: Dapper Drake / Slackware
Posts: 151

Original Poster
Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to write a simple c prog to call a script RajRed Programming 1 10-05-2005 05:10 PM
how to write a simple c prog to call a script RajRed Linux - Software 1 10-05-2005 02:57 PM
2 simple shell prog questions provkitir Linux - Software 3 11-23-2004 03:00 AM
area of polygon questioner Programming 7 09-19-2003 11:24 AM
Something wrong with this simple hello world prog mandrakeroot Programming 9 08-26-2003 05:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration