GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-May-2006, 14:19
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

area of n-sided regular polygon


OK, 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:
float prism::computeBaseArea()
{
    baseArea = ( (n * pow(t, 2)) / (4 * (tan(PI / n))) );
    return baseArea;
}

When I test it, I get bad values:

Quote:
The attributes for the prism are as follows:
Number of sides: 4
Length of each side: 1
Base area: -1.9998

The attributes for the prism are as follows:
Number of sides: 5
Length of each side: 1
Base area: -1.69474e+38

The attributes for the prism are as follows:
Number of sides: 6
Length of each side: 1
Base area: 0

Any ideas on what I'm doing wrong?
  #2  
Old 12-May-2006, 15:12
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: area of n-sided regular polygon


Check 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  
Old 12-May-2006, 15:24
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: area of n-sided regular polygon


Quote:
Originally Posted by WaltP

Where did you get this formula? I can't find it, and is doesn't seem to work.
http://en.wikipedia.org/wiki/Regular_polygon#Area
  #4  
Old 12-May-2006, 15:37
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: area of n-sided regular polygon


Quote:
Originally Posted by WaltP
Check the types of your variables. Don't forget the problems between int and float values during division...

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  
Old 12-May-2006, 15:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: area of n-sided regular polygon


Quote:
Originally Posted by earachefl
When I test it, I get bad values:


Any ideas on what I'm doing wrong?

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:
float prism::computeBaseArea()
{
    cout << "In computeBaseArea():"<< endl;
    cout << "   n                 = " << n << endl;
    cout << "   t                 = " << t << endl;
    cout << "   pow(t, 2)         = " << pow(t, 2) << endl;
    cout << "   PI                = " << PI << endl;
    cout << "   PI / n            = " << PI / n << endl;
    cout << "   tan(PI / n)       = " << tan(PI / n) << endl;
    cout << "   4 * (tan(PI / n)) = " << 4 * tan(PI / n) << endl;
    cout << "   n * pow(t, 2)     = " << n * pow(t, 2) << endl;

    baseArea = ( (n * pow(t, 2)) / (4 * (tan(PI / n))) );

     cout << "   baseArea          = " << baseArea << endl << endl;

    return baseArea;
}

Regards,

Dave
  #6  
Old 12-May-2006, 15:47
earachefl earachefl is offline
Member
 
Join Date: Feb 2006
Posts: 178
earachefl is on a distinguished road

Re: area of n-sided regular polygon


Quote:
Originally Posted by davekw7x
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.


Dave

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  
Old 12-May-2006, 15:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,793
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: area of n-sided regular polygon


Quote:
Originally Posted by earachefl
See #4 above.
Actually, I saw your good news after I posted my suggestion (and I am really, really glad that you found the problem--- congratulations). I thought about deleting my post, as I sometimes do when I see that the advice is redundant. But then I thought I would leave it there in case other people might want to see some clues about debugging things like this.

Regards,

Dave
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 11:40.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.