• 03-23-2013
hnparmar
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.)

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); }```
• 03-23-2013
rogster001
Quote:

What sort of help do you need?
• 03-23-2013
Malcolm McLean
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.
• 03-24-2013
hnparmar
• 03-24-2013
hnparmar
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();         }```
• 03-24-2013
The Doctor
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.
• 03-24-2013
Matticus
@The Doctor

Quote:

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); }```
• 03-25-2013
Salem
```#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();         }```