![]() |
|
#1
|
|||
|
|||
area of n-sided regular polygonOK, this might be more of a math question, but here goes. According to Wikipedia, the formula for the area of a regular n-sided polygon is
Area = n(t to the 2nd power) / 4tan(PI/n) where n is the number of sides and t is the length of each side. Implemented in my code: CPP / C++ / C Code:
When I test it, I get bad values: Quote:
|
|
#2
|
||||
|
||||
Re: area of n-sided regular polygonCheck the types of your variables. Don't forget the problems between int and float values during division...
Where did you get this formula? I can't find it, and is doesn't seem to work. __________________
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough. -- Pearl Williams |
|
#3
|
|||
|
|||
Re: area of n-sided regular polygonQuote:
|
|
#4
|
|||
|
|||
Re: area of n-sided regular polygonQuote:
I figured out the problem. I had multiple constructors (default - no arguments, 2nd - one argument, 3rd - 3 arguments, 4 - 5 arguments). I instantiated an object using the third constructor, which passed number of sides, length of sides, and height as arguments. Where I screwed up was not including a function call (private member function) to compute the area of the base from that constructor. I'd been meaning to ask about defining multiple constructors - my textbook says to, but Deitel says not to. Unfortunately had to return Deitel to the library as I had too many renewals on it. Is there more than one school of thought on defining multiple constructors? BTW, the formula does work, now that I've called it correctly..... |
|
#5
|
|||
|
|||
Re: area of n-sided regular polygonQuote:
I can think of several ways that you could get bad values 1. You aren't feeding the formula with what you think you are. 2. The formula is incorrect (or you have implemented it incorrectly). 3. The calculation is correct, but, somehow, the correct value is not getting back to where it is used. 4. You aren't calling the function that you think you are. 5. A combination of some or all of the above. When you have a program that runs but gives you the wrong answer and you can't see how that could happen, then make the program tell you what it is working on. Print out values of variables and intermediate expressions for the steps of the calculation. Run the program for something simple (that you can easily calculate the correct answer by hand). CPP / C++ / C Code:
Regards, Dave |
|
#6
|
|||
|
|||
Re: area of n-sided regular polygonQuote:
See #4 above. I hadn't actually called the function to compute the base area from the constructor, thus was getting garbage values. D'oh!!! |
|
#7
|
|||
|
|||
Re: area of n-sided regular polygonQuote:
Regards, Dave |
Recent GIDBlog
Writing a book by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Endless recursive function | teflon | C++ Forum | 3 | 26-Jul-2005 12:31 |
| Restricting text modifications in text area | Sadia | MS Visual C++ / MFC Forum | 3 | 23-Jun-2005 23:09 |
| Re: GIDPanel - Member's Area is now open | JdS | GIDNetwork™ | 2 | 17-Jun-2005 21:35 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The