need help, Volume of cone is always 0??

This is a discussion on need help, Volume of cone is always 0?? within the C Programming forums, part of the General Programming Boards category; hello there am trying to compute the volume of a cone using a function but it always gives 0, why??? ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    Katy, Texas
    Posts
    2,309
    Try
    Code:
    (1.0/3.0)
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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-17-2009, 11:21 PM
  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, 02:13 AM
  4. Help! Right Math, Wrong Output
    By verd in forum C Programming
    Replies: 12
    Last Post: 03-15-2005, 06: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

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