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;
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.
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 !