Need Help With Calculation
-
The program works, but the calculation is wrong. The is written in C. Could someone tell me what I am doing wrong? Thank You #include "stdafx.h" #include #using 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); } Joseph L. Gelsomino
-
The program works, but the calculation is wrong. The is written in C. Could someone tell me what I am doing wrong? Thank You #include "stdafx.h" #include #using 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); } Joseph L. Gelsomino
Try changing the 4/3 to 4.0/3.0 so the calulation is done as a double not as an integer, which is currently losing you the 0.3333333 If you vote me down, my score will only get lower
-
The program works, but the calculation is wrong. The is written in C. Could someone tell me what I am doing wrong? Thank You #include "stdafx.h" #include #using 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); } Joseph L. Gelsomino
In addition to Roger's suggestion, I suggest the following to help 'polish' your application:
WackoWolf wrote:
// Integer local to Main to store initial radius value double r;
Comment (int) does not match code (double).
WackoWolf wrote:
if (r >= 0)
Use a
double
constant (0.0) here.WackoWolf wrote:
printf("You Must Enter A Positive Integer!!!! Not A Negative Integer!!!\n");
Again referencing integers.
WackoWolf wrote:
double exponent=3;
Use a
double
constant (3.0) here.WackoWolf wrote:
void sphere_volume (double radius)
Another suggestion I might make, although it has no impact on the outcome of your program, is to make this function's parameter
const
.void sphere_volume( const double radius );
This ensures that your code does not accidently change the value of the parameter.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb