Thread: Volume always coming 0

  1. #1
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52

    Volume always coming 0

    In a nutshell,this is the code of the area,lateral area and volume of a cone from a class structure -:
    Code:
    else if(g[0] == 'N' || g[0] == 'n')
                            {
                                   cout << "Enter radius of the cone - ";
                                   cin >> a;
                                   cout << "\nEnter slant height of the cone - ";
                                   cin >> b;
                                   cout << "\nEnter perpendicular height of the cone - ";
                                   cin >> x;
                                   float Area = (M_PI * a) * (a + b);
                                   float L_area = M_PI * a * b;
                                   float Volume = 1/3 * M_PI * pow(a,2) * x;
                                   cout << "\nThe CSA is " << L_area << endl;
                                   cout << "The TSA is " << Area << endl;
                                   cout << "The volume is " << Volume << endl;
                            }
    But the volume keeps coming 0 for whatever value I put..I don't seem to find anything wrong..help please
    Last edited by SVXX; 10-04-2007 at 05:19 AM.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    float Volume = 1/3 * 22/7 * pow(a,2) * x;
    Integer division... 1/3 is 0, not 0.3333 which means that the result for Volume is always 0.

    Try this instead:
    Code:
    float Area = (22.0/7.0 * a) * (a + b);
    float L_area = 22.0/7.0 * a * b;
    float Volume = 1.0/3.0 * 22.0/7.0 * pow(a,2) * x;
    ...and you'd be better off using double instead of float.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Not only is 22/7 a fairly poor approximation for pi, since it's evaluated using integer arithmetic, it will be evaluated as 3, exactly. Similarly, 1/3 evaluates as 0, which is probably the source of your problem. Also, you can use a*a instead of pow(a,2), which is both shorter and faster.

  4. #4
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    I'm using M_PI,which is a pi constant declared in cmath. K thanks its working now.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    M_PI isn't guaranteed to exist by the standard. Something like
    Code:
    #include <cmath>
    
    const double Pi = 4.*std::atan(1.);
    would be 100&#37; portable.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > it will be evaluated as 3, exactly.
    Nothing wrong with that

    > would be 100% portable.
    But what about the accuracy?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    lol Salem...I have another problem now. I made a loading screen for the program with the following code
    Code:
    for(int i = 0; i < 100; i ++)
            {
                   gotoxy(35,12);
                   setcolor(12);
                   cout << "Loading the Math-O-Matic 5000";
                   gotoxy(38,14);
                   cout << i << "% done";          
                   Sleep(100);
                   setcolor(7);
                   clrscr();
            }
    clrscr() and gotoxy() I defined on my own. Those work. The thing is,if I want to break or skip the loading screen,how would I do so?

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by Salem View Post
    > would be 100% portable.
    But what about the accuracy?
    On my Linux box, GCC's <cmath> is implemented by #including <math.h>, which defines M_PI as

    # define M_PI 3.14159265358979323846 /* pi */

    which is the correct value to 20 places. On the other hand, a 64-bit IEEE double uses 52 bits for the mantissa, which is roughly 15 or 16 decimal digits.
    Code:
    #include <iostream>
    #include <cmath>
    
    int main() {
      const double Pi = 4.*std::atan(1.);
      std::cout.precision(25);
      std::cout << "Pi == " << Pi << '\n';
      std::cout << "M_PI == " << M_PI << '\n';
    }
    Pi == 3.141592653589793115997963
    M_PI == 3.141592653589793115997963

    So for a 64-bit IEEE double, the values are essentially the same to machine precision.

  9. #9
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Uhh you could go on loggerheads about M_PI later...some help with my problem please? >.>

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by SVXX View Post
    lol Salem...I have another problem now. I made a loading screen for the program with the following code
    Code:
    for(int i = 0; i < 100; i ++)
            {
                   gotoxy(35,12);
                   setcolor(12);
                   cout << "Loading the Math-O-Matic 5000";
                   gotoxy(38,14);
                   cout << i << "% done";          
                   Sleep(100);
                   setcolor(7);
                   clrscr();
            }
    clrscr() and gotoxy() I defined on my own. Those work. The thing is,if I want to break or skip the loading screen,how would I do so?
    Why have it in the first place - it doesn't do any "loading", and waiting ten seconds (10000 ms) just to write out 100 percentage values is pretty meaningless, don't you think? If it was ACTUALLY loading something, that's a differnet matter, but you're just pretending to load something... Pointless...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    Well,as the guy who suggested it says, it "gives a pro look to the program". Lmao.
    Yer really too serious about this Mat...I just added it for fun,it gives a more interactive look to the program and thats it. Who likes wadding about DOS these days? All of my pals work on Windows,and the purpose of this program is to pwn every math concept we have in our textbook. Thats why I'm making this. This is only basic ......... I'll be adding trigonometric equation solving and more later..I should make a Win32 API,yes, but unfortunately I'm not so fluent in Win32 programming. Anything else?
    Last edited by SVXX; 10-04-2007 at 06:46 AM.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by SVXX View Post
    Well,as the guy who suggested it says, it "gives a pro look to the program". Lmao.
    Yer really too serious about this Mat...I just added it for fun,it gives a more interactive look to the program and thats it. Who likes wadding about DOS these days? All of my pals work on Windows,and the purpose of this program is to pwn every math concept we have in our textbook. Thats why I'm making this. This is only basic ......... I'll be adding trigonometric equation solving and more later..I should make a Win32 API,yes, but unfortunately I'm not so fluent in Win32 programming. Anything else?
    But you can't skip the "loading" as it stands. If you want to you could do a "if (kbhit()) break;" type thing. How you do that in a modern Windows system, I'm not entirely sure. I could find out if I needed to, but I still fail to see the point.

    In my opinion, it doesn't make it look professional, it makes it look pretentious - by which I mean "it pretends to be more important than it is" - like sticking a BIG wing on a 60 bhp not-so-sporty car sort of thing. The car can't even get to a speed where that wing is needed, but it's there to make it "look fast". Pointless rubbish.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If you know how to read input without blocking the execution this might be possible. But it is going to look very unprofessional if your program stops loading at 34&#37; is you press a key...

    You may try to reduce the sleep period and/or skip a few percentages.

    To make it look more professional, you should introduce some irregularity. E.g the first 86% get loaded really fast, then the next 4% take almost an eternity and the last 10% go fast again.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    Cryptanalyst
    Join Date
    Sep 2007
    Posts
    52
    And then again, you're not one of my classmates, neither do you know me.
    I'll get advanced as I progress, and probably feel the same about that crap.
    True, it may be pointless rubbish, but I don't really care.

    Lmao anon...if that makes it look professional.
    "I might be wrong" XD

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by SVXX View Post
    And then again, you're not one of my classmates, neither do you know me.
    I'll get advanced as I progress, and probably feel the same about that crap.
    True, it may be pointless rubbish, but I don't really care.

    Lmao anon...if that makes it look professional.
    "I might be wrong" XD
    Well, if this is for Windows, have a look at:
    http://msdn2.microsoft.com/en-us/library/ms684344.aspx

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-09-2009, 12:55 AM
  2. How to know if a volume is mounted with ACL enabled
    By rak1986 in forum Linux Programming
    Replies: 0
    Last Post: 04-08-2009, 12:45 AM
  3. Volume of a Cone Equation always equals 0
    By Devolution in forum C Programming
    Replies: 11
    Last Post: 01-28-2009, 03:13 AM
  4. Help! Right Math, Wrong Output
    By verd in forum C Programming
    Replies: 12
    Last Post: 03-15-2005, 07:49 PM
  5. What am I doing wrong? Help please...
    By SprinterSteve in forum C Programming
    Replies: 9
    Last Post: 04-17-2003, 09:35 PM