Problems with calculating volume of a sphere.

Discussion in 'C Programming' started by rafa, Aug 9, 2003.

  1. rafa

    rafa Guest

    Hi I'm just learning C and I was trying to do an exercise in a book
    which asks me to write a program to calculate the volume of a
    sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3

    and I've been trying to write that but have been unsuccesfull so far.
    I tryed:
    volume = 4/3 * PI * radius;
    volume = volume * volume * volume;

    or
    volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *
    radius));

    And a few other variations on the algorithm. The rest of program works
    fine I just don't know the proper syntax for creating this algorithm.
    If someone would be kind enough to answer my question I'd also
    appreciate if they could explain why the syntax needs to be the way it
    is. Since right now I'm quite confused.

    -rafa
    rafa, Aug 9, 2003
    #1
    1. Advertising

  2. rafa

    Eric Bernard Guest

    "rafa" <> wrote in message
    news:p...
    > Hi I'm just learning C and I was trying to do an exercise in a book
    > which asks me to write a program to calculate the volume of a
    > sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3
    >
    > and I've been trying to write that but have been unsuccesfull so far.
    > I tryed:
    > volume = 4/3 * PI * radius;
    > volume = volume * volume * volume;
    >
    > or
    > volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *
    > radius));
    >
    > And a few other variations on the algorithm. The rest of program works
    > fine I just don't know the proper syntax for creating this algorithm.
    > If someone would be kind enough to answer my question I'd also
    > appreciate if they could explain why the syntax needs to be the way it
    > is. Since right now I'm quite confused.
    >
    > -rafa


    4/3 is an integer expression, consider volume = 4.0/3.0 * PI * radius *
    radius * radius;

    Eric Bernard
    Eric Bernard, Aug 9, 2003
    #2
    1. Advertising

  3. rafa

    Al Bowers Guest

    rafa wrote:
    > Hi I'm just learning C and I was trying to do an exercise in a book
    > which asks me to write a program to calculate the volume of a
    > sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3
    >
    > and I've been trying to write that but have been unsuccesfull so far.
    > I tryed:
    > volume = 4/3 * PI * radius;
    > volume = volume * volume * volume;
    >
    > or
    > volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *
    > radius));
    >


    You need to force a float type division. Change 4/3 to 4.0/3.0.
    The correct formula should be:
    4.0/3.0*PI*radius*radius*radius

    #include <stdio.h>

    #define PI 3.14

    int main(void)
    {
    double volume,radius;

    radius = 3.0;
    volume = 4.0/3.0*PI*radius*radius*radius;
    printf("The volume of a sphere of radius 3cm is %.2f cu cm\n",
    volume);
    return 0;
    }

    --
    Al Bowers
    Tampa, Fl USA
    mailto: (remove the x)
    http://www.geocities.com/abowers822/
    Al Bowers, Aug 9, 2003
    #3
  4. rafa wrote:

    > Hi I'm just learning C and I was trying to do an exercise in a book
    > which asks me to write a program to calculate the volume of a
    > sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3
    >
    > and I've been trying to write that but have been unsuccesfull so far.
    > I tryed:
    > volume = 4/3 * PI * radius;
    > volume = volume * volume * volume;
    >
    > or
    > volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *
    > radius));


    It is the radius that is cubed, not (4. * PI * radius)/3.

    Also note that (4/3) is 1, but (4./3), (4/3.), (4./3.) are all floating
    point values (actually doubles) with value 1.3333....
    Since PI is a floating point value, (4 * PI)/3 will also have the correct
    value, up to the required precision of a double or of PI, whichever is
    less. The FAQ explains these things.


    const double PI=3.14159265358979323846;
    double cube(double x) {return x*x*x;}
    double volume(double r) {return 4*PI*cube(r)/3; }




    --
    Martin Ambuhl
    Martin Ambuhl, Aug 9, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Denebola

    Rotating sphere

    Denebola, Jan 19, 2006, in forum: HTML
    Replies:
    8
    Views:
    6,818
    dorayme
    Jan 21, 2006
  2. Ron Natalie

    Random numbers within a sphere?

    Ron Natalie, Feb 8, 2005, in forum: C++
    Replies:
    25
    Views:
    7,737
    Richard.Ayling
    Oct 23, 2007
  3. Marzena
    Replies:
    1
    Views:
    934
    Andrew Thompson
    May 7, 2007
  4. mlt
    Replies:
    7
    Views:
    1,172
    Jerry Coffin
    Sep 5, 2008
  5. Daigo Tomono
    Replies:
    0
    Views:
    87
    Daigo Tomono
    Dec 9, 2004
Loading...

Share This Page