# Thread: Defines and Header file

1. ## Defines and Header file

1. Write a program the calculates the volume of a sphere. Use a define to set Pi to 3.14 and a macro for the formula for the sphere. V = 4/3PiR3.
In main ask for the radius (R). Pass it to a function where you calculate the volume and return it to main and print the volume in main. Use float values. (Save this program as you'll need it later.)

Can you please help me?

Code:

Code:
```#include<stdio.h>
void fun (float);
main()
{
float  a;
float v;
printf("Please enter the radius: ");
scanf("%.3f",&a);
fun(a);
printf("The volume is  =  %.3f", v);
}

void fun (float Radius)
{
float v,pi = 3.14;
v = ((4/3)*pi*(Radius * Radius * Radius));
printf("%.3f",v);
return(v);
}```

2. Can you please help me?
What sort of help do you need?

3. You've got the basic idea.

fun calcuates the volume of sphere, given a radius. But it shouldn't be called "fun". It should be called "spherevolume" or somethign similar.

Then it's one of those functions which returns a single value, like tan or sqrt. So it should be float fun(float r).

Finally, it depends on the mathematical constant PI. In C we define constants with the #define directive. For instance phi, the golden ratio, is a constant. So you could write
#define PHI 1.618

Another constant you use a lot is INT_MAX, the maximum value that can be held in an integer. This is defined for you in limits.h, or you can define a similar constant yourself.

4. can you please help me in the program?

5. Code:
```#include<stdio.h>
#include<limits.h>
#define pi 3.141592654
void spherevolume (float a);
main()
{
float a,v;
float answer;
printf("Please enter the radius: ");
scanf("%f",&a);
spherevolume(a);
printf("%f",v);
getch();
}
void spherevolume (float b)
{
float num,Radius,v;
num = 1.333333333;
Radius = b * b * b;
v = (num * pi * Radius);
return(v);
getch();

}```
Can you please help me how to return spherevolume to main? please.

6. Look up Preprocessor commands. That's what you need to use.

You want the volume thing to not be a function. you need to use #define for a macro as well.

7. @The Doctor

Code:
```if (codeWorks( code) && !youCanDoSomethingBetter( code) )
{
fix( code);
} else
{
beHappy();
}```
Fix your signature! You have the statements reversed!

Code:
```if (codeWorks( code) && !youCanDoSomethingBetter( code) )
{
beHappy();
} else
{
fix( code);
}```

8. > Can you please help me how to return spherevolume to main? please.
How about making it return something?

> void spherevolume (float a);
Like say
float spherevolume (float a);

Then make sure you assign the result in main rather than ignore it.

9. Code:
```#include<stdio.h>
#include<limits.h>
#define pi 3.141592654
float spherevolume (float);
main()
{
float a,v;
printf("Please enter the radius: ");
scanf("%f",&a);
v = spherevolume(a);
printf("\n\nThe volume is : %.3f",v);
getch();
}
float spherevolume (float b)
{
float num,Radius,v;
num = 1.333333333;
Radius = b * b * b;
v = (num * pi * Radius);
return(v);
getch();

}```
Thank you so much it 's working now.

Popular pages Recent additions