Volume of a Cone Equation always equals 0

This is a discussion on Volume of a Cone Equation always equals 0 within the C Programming forums, part of the General Programming Boards category; Hey, I'm new here and I have a question that's baffling me. I'm making an equation generator for the volume ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    14

    Volume of a Cone Equation always equals 0

    Hey, I'm new here and I have a question that's baffling me.

    I'm making an equation generator for the volume of a cone, but it won't stop equaling 0, and I have other equations that work, and I made them in the same fashion.

    Code:
    #include <stdio.h>
    
    #define PI 3.141592
    
    int main (void)
    {
    
    /* Volume of a cone:  1/3 * PI * radius^2 * height */
    	double v = 0, r = 0, h = 0;
    
    	printf("Enter 2 floating-point values (radius and height) for use in volume of a cone equation: ");
    	scanf("%lf%lf", &r, &h);
    
    	(double) v = (double) (1/3) * PI * (double) (r * r) * (double) h;
    
    	printf ("Volume of a cone: volume_cone = 1/3 * PI * radius^2 * height = 1/3 * %lf * (%lf^2) * %lf = %lf\n", PI, r, h, v);
    
    		return (0);
    }
    Thanks for any help that might come, and with any luck you guys will be seeing a lot of me as I pursue a career in computer science

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    (double) (1/3)
    is always zero, as 1 and 3 are integers. Use 1.0 and 3.0 to make the values doubles. No cast needed in this case.

    Code:
    (double) (r * r) * (double) h;
    since r and h are doubles already, there is no need to cast them.

    --
    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.

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    I would change everything to float for a start.
    I don't see why you need more than a float.

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Thanks for the quick response! I fixed the 2 issues and it works just fine now

  5. #5
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by Devolution View Post
    Thanks for any help that might come, and with any luck you guys will be seeing a lot of me as I pursue a career in computer science
    [Off Topic]
    Hey, you aren't associated with the OF gaming clan, are you?
    [/Off Topic]

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by esbo View Post
    I would change everything to float for a start.
    I don't see why you need more than a float.
    And the benefit of that is? Aside from saving 12 bytes of stack, I doubt you'd be able to measure the difference.

    --
    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.

  7. #7
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    Quote Originally Posted by kermit View Post
    [Off Topic]
    Hey, you aren't associated with the OF gaming clan, are you?
    [/Off Topic]

    Mmm nope, I do game though.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    IIRC that is the equation that Archimedes was most proud of, and is on his grave marker.

  9. #9
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by matsp View Post
    And the benefit of that is? Aside from saving 12 bytes of stack, I doubt you'd be able to measure the difference.

    --
    Mats
    Well not much except it make things look more complicated you have to worry about if the formatting is correct and then you have casts to doubles all over the case and then you can't see the woods for the trees when you get a problem.

    All I am saying is it is one less thing to worry about really, I mean the first thing I thought was that he had probably done a bad conversion on the doubles somewhere, so I was lookng for some fairly obscure data conversion there when the obscure data conversion was on the the integer division. Nothing to do with run time or saving space really it just makes your program easier to check because you remove one potential source of error.

    And then the formatting is lf, long float, but a long applies to an integer so you can get really confused, anyway I though you used %g to print doubles but then what do I know, I just think it is a rather confusing area which is best avoided.
    Even looking on the web it seemed to come up with different interpretations, and I could not find %lf at all!!

    Actually was the other problem %lf?
    Last edited by esbo; 01-27-2009 at 10:06 PM.

  10. #10
    Registered User
    Join Date
    Jan 2009
    Posts
    14
    From what I've been taught so far, %d is for integer values and %lf is for floating values.

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by Devolution View Post
    From what I've been taught so far, %d is for integer values and %lf is for floating values.
    %lf is scanf format for doubles, for floats scanf uses %f

    printf for both doubles and floats should use %f
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    printf for both doubles and floats should use %f
    Because all variable argument functions (or other functions that hasn't got a prototype with float arguments), flaots are automagically converted to doubles.

    --
    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: 15
    Last Post: 11-04-2006, 03:06 AM
  2. Help! Right Math, Wrong Output
    By verd in forum C Programming
    Replies: 12
    Last Post: 03-15-2005, 06:49 PM
  3. IDEA: Equation solver
    By Magos in forum Contests Board
    Replies: 2
    Last Post: 01-07-2003, 10:46 AM
  4. Quadratic Equation Program
    By Ambizzy in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 08:21 PM
  5. equation solving
    By ajlott_coder in forum C Programming
    Replies: 6
    Last Post: 01-26-2002, 01:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21