Thread: need help, Volume of cone is always 0??

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    need help, Volume of cone is always 0??

    hello there
    am trying to compute the volume of a cone using a function but it always gives 0, why???
    i still an amateur so excuse me

    Code:
    # include <stdio.h>
    # define PIE 3.14
    float ComputeVolume ( float r, float h, float ans , float r2 );
    int main () {
    float radius=0 , height=0 , ans, r2;
    printf (" Radius : ");
    scanf ("%f", & radius );
    printf (" Height : ");
    scanf ("%f", & height );
    ans = ComputeVolume (radius , height , ans , r2 );
    printf (" Volume : %f\n", ans );
    system("PAUSE");
    return 0;
    }
    float ComputeVolume ( float r, float h, float ans , float r2)
    {
    r2 = r*r;
    ans = (1/3)* PIE *r2*h;
    return ans ;
    }
    any help?!!!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > ans = (1/3)* PIE *r2*h;
    1/3 in integer maths is zero.

    Make the expression a double.

    And please look at improving the indentation of your code.
    SourceForge.net: Indentation - cpwiki
    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.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Quote Originally Posted by Salem View Post
    > ans = (1/3)* PIE *r2*h;
    1/3 in integer maths is zero.

    Make the expression a double.

    And please look at improving the indentation of your code.
    SourceForge.net: Indentation - cpwiki
    thanks for the reply, i just tried that and still zero

    maybe am not making it well but it still give me zero
    Code:
    # include <stdio.h>
    # define PIE 3.14
    double ComputeVolume ( float r, float h, double ans , float r2 );
    int main () {
    float radius=0 , height=0,r2;
    double ans;
    printf (" Radius : ");
    scanf ("%f", & radius );
    printf (" Height : ");
    scanf ("%f", & height );
    ans = ComputeVolume (radius , height , ans , r2 );
    printf (" Volume : %f\n", ans );
    system("PAUSE");
    return 0;
    }
    double ComputeVolume ( float r, float h, double ans , float r2)
    {
    r2 = r*r;
    ans = (1/3)* PIE *r2*h;
    return ans ;
    }
    is it right like that to make the expression double?
    i even tried to remove the (1/3) and still zero is appearing

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Try
    Code:
    (1.0/3.0)
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A few changes:

    Code:
    # include <stdio.h>
    # define PIE 3.14
    
    double ComputeVolume (float r, float h);
    
    int main () {
      float radius=0.0, height=0.0;
      double vol;
      printf (" Radius : ");
      scanf ("%f", &radius );
      printf (" Height : ");
      scanf ("%f", &height );
      vol = ComputeVolume (radius , height);
      printf (" Volume : %f\n", vol);
      system("PAUSE");
      return 0;
    }
    double ComputeVolume (float r, float h)
    {
      double ans, r2;
      r2 = r*r;
      ans = (1.0/3.0)* PIE *r2*h;
      return ans ;
    }

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    that was just great, thank you all for this great effort

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Detect the Audio And Microphone Level In Windows
    By Kelderic in forum C Programming
    Replies: 6
    Last Post: 11-18-2009, 12:21 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