Volume of a sphere using call-to-value function

Hi,
I am trying to write a program that uses a call-to-value function to evaluate the volume of a sphere. My program below will run but gives the wrong answer. If anyone knows where I went wrong any help would be appreciated !

#include <stdio.h>
#include <math.h>
#define PI 3.14159


double sphereArea(double radius);

int main(void)
{
double radius;
double area;

printf("Enter a radius length in cm:\n");
scanf("%lf", &radius);

area= sphereArea( radius);//function call
printf("The area of a sphere with radius %f is %d\n", radius, area);

return 0;
}//end main function
double sphereArea(double radius)
{
double area;
area = 4*PI*radius*radius;

return area;
}
The formula for the volume of a spere is area = 4/3 * PI * radius * radius * radius. The formula you used is for surface area.
sorry I meant to write area, I am tring to find the area of a sphere.
http://www.cplusplus.com/reference/clibrary/cstdio/printf/

You are using the wrong format to print out your area. %d is a signed decimal integer, you need a floating point.
Last edited on
I don't know how to use stdio.h or the printf function, but I'm almost 100% sure that you're error is with this line:

printf("The area of a sphere with radius %f is %d\n", radius, area);

Also, you should use code tags to make your code look nicer.

[Edit]: Ha, I actually made it work. %d should be actually be %f, the %f signifying floating point. You should follow the link from HooklessFastener though to understand exactly how this works.
Last edited on
Thanks so much !

The second part of the question is to use a call-to-reference function. I know you have to use pointers but I'm not too sure how.
If you could point me in the right direction that would be great !
This is the code that I tried, it runs the program but gives the worng output for the area and the volume.

#include <stdio.h>
#include <math.h>
#define PI 3.14159

void sphere( double *areaptr, double*volumeptr, double radius);

int main (void)
{
double radius;
double area, volume;
double *areaptr;
double *volumeptr ;

printf("Enter a radius length in cm:\n");
scanf("%lf", &radius);

sphere(&areaptr, &volumeptr, radius); // function call

printf("The area of a sphere with radius %f is %f\n", radius, &areaptr);
printf("The volume of a sphere with radius %f is %f\n", radius, &volumeptr);

return 0;
}//end main function

void sphere( double *areaptr, double*volumeptr, double radius)
{
*areaptr= 4*PI*radius*radius;
*volumeptr= (4/3)*PI*radius*radius*radius;
}//end function call
I'll give you a better link. This site actually has a great tutorial section to learn all the basics of c++.

http://www.cplusplus.com/doc/tutorial/functions2/

Actually, the link in my last post was really crappy compared to this one.

All you are trying to do is take a reference to area and radius ( double& area, double& radius), and it performs the exact same operation as before.
Last edited on
Topic archived. No new replies allowed.