New to programming, need help understanding what's wrong.

This is a discussion on New to programming, need help understanding what's wrong. within the C Programming forums, part of the General Programming Boards category; This is my code, basically i'm trying to write a program which find s the volume of spheres with certain ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    New to programming, need help understanding what's wrong.

    This is my code, basically i'm trying to write a program which find s the volume of spheres with certain radius's. However it doesn't seem to be working, and is returning values of 0. Any clue as to why? Any help would be much appreciated

    Code:
    #include <stdio.h>
    #define Pi 3.14159265
    double VolumeForRadius(double Radius)
    {
      double radius;
      double volume = (4.0 / 3.0) * Pi * radius * radius * radius;
        return volume;
    }
    int main(int argc, char *argv[])
    {
        printf("The volume of a sphere with radius %d is %f\n", 1, VolumeForRadius(1.0));
        printf("The volume of a sphere with radius %f is %f\n", 4.657, VolumeForRadius(4.657));
        printf("The volume of a sphere with radius %d is %f\n", 10, VolumeForRadius(10));
        printf("The volume of a sphere with radius %d is %f\n", 42, VolumeForRadius(42));        
    }

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    double VolumeForRadius(double Radius)
    {
      double radius;
      double volume = (4.0 / 3.0) * Pi * radius * radius * radius;
        return volume;
    }
    You define your function accepting a parameter "Radius" but never use it. Instead you declare a new variable "radius" which will have a garbage value because you don't initialize it and calculate "volume" from it.
    You want to use the parameter in your calculation.

    Bye, Andreas
    Last edited by AndiPersti; 10-12-2012 at 01:49 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C Programming- Need help understanding !! operator
    By shellwoo3 in forum C Programming
    Replies: 4
    Last Post: 07-17-2012, 07:18 PM
  2. Replies: 3
    Last Post: 09-11-2008, 12:29 AM
  3. Not understanding CPU C programming benchmarks...
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-22-2005, 03:49 AM
  4. Understanding Programming Industry
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 06:26 PM

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