Help With Calculation

This is a discussion on Help With Calculation within the C++ Programming forums, part of the General Programming Boards category; This is written in C. The program works, but the calculation is wrong, can someone tell me what I am ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    56

    Help With Calculation

    This is written in C. The program works, but the calculation is wrong, can someone tell me what I am doing wrong?

    Thank You

    Code:
    #include "stdafx.h"
    #include <math.h>
    
    #using <mscorlib.dll>
    
    using namespace System;
    
    // Function prorotypes
    
     void sphere_volume (double);
    
    // PI is a constant
    
    const double PI=3.14159;
    
    
    int _tmain()
    
    
    {
    	// Integer local to Main to store initial radius value
    double r;
    
    	printf("Please Enter A Positive Radius Value\n");
    	scanf("%lf", &r);
    
    // If Else Function To Make Sure The User Types A Positive and Not A Negative Integer.
    
    	if (r >= 0)
    		sphere_volume (r);
    
    	else
    		printf("You Must Enter A Positive Integer!!!! Not A Negative Integer!!!\n");
    
    	return 0;
    
    }
    
    // This Is The Fnction That Does The Calculations For The Volume.
    
    void sphere_volume (double radius)
    
    {
    	double  radius_cubed;
    
    	double exponent=3;
    
    	double volume;
    
    	printf("This Is The Test To See If R Was Passed To The Function: %lf\n", radius);
    
    	radius_cubed=pow(radius, exponent);
    
    	volume=(4/3)*PI*radius_cubed;
    
    	printf("The Volume Of The Sphere is %lf\n", volume);
    }

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    There is a C programming forum for C programs.

    Your problem is this:
    Code:
    (4/3)
    make it
    Code:
    (4.0/3)
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    The reason is: 4/3 is integer division. The fractional part is discarded. So it ends up being 1, not 1.33333333333333. 4.0 is a floating point number, and the result of that expression is the expected value, 1.3333333333333333.

    Oh yes, and
    Code:
    printf("The Volume Of The Sphere is %lf\n", volume);
    Should be
    Code:
    printf("The Volume Of The Sphere is %f\n", volume);
    With printf(), %lf doesn't exist.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    56
    Thank you, I am trying it now. Were is the C programming forum for C programs.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    56
    Sorry I found it.

    Thank You

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Date calculation program
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 08:24 AM
  2. Problem with modulo calculation
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 03:37 PM
  3. C# calculation problem
    By Diablo02 in forum C# Programming
    Replies: 13
    Last Post: 10-25-2007, 09:42 PM
  4. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 06:37 AM
  5. help with pay calculation program
    By shugstone in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 09:38 AM

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