-
November 4th, 2009, 05:45 PM
#1
Code
Hey i need help creating a code that calculates volume/surface area of a sphere, i was using this code as a reference but I'm just drawing a blank right now, any help would be appreciated:
//INCLUDE SECTION
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <math.h>
//DEFINE STATEMENT
#define d(x1,y1,x2,y2) sqrt(pow((x2-x1),2)+ pow((y2-y1),2))
//FUNCTION PROTOTYPE
float distance(float a,float b,float c,float d);
int main(void)
{
//VARIABLE DECLARATIONS
float x1,y1,x2,y2,d_tot;
//EXPLAIN TO USER
system ("cls");
puts("This program will calculate the distance between 2 points");
puts("in space. You will be asked for (x1,y1) (x2,y2) and then");
puts("it will calculate and ouput the distance between these");
puts("points.");
puts("");
//GET USER INPUT
printf("(x1 y1)==>");
fflush(stdin);
scanf("%f%f",&x1,&y1);
printf("(x2 y2)==>");
fflush(stdin);
scanf("%f%f",&x2,&y2);
//CALCULATIONS
d_tot=distance(x1,y1,x2,y2);
//OUTPUT
printf("\n\n(x1,y1) =(%.1f,%.1f)",x1,y1);
printf("\n\n(x2,y2) =(%.1f,%.1f)",x2,y2);
printf("\n\nTotal distance=%.1f",d_tot);
getch();
return 0;
}
//FUNCTION DEFINITIONS
float distance(float a,float b,float c,float d)
{
return(d(a,b,c,d));
}
I need to use these functions in there:
explain();
get_radius();
calc_display();
and i also need to use a define statement for both formulas.
*note* VOLUME of a SPHERE = ( 4 / 3 ) * * radius 3
SURFACE AREA of a SPHERE = 4 * * radius 2
-
November 4th, 2009, 05:56 PM
#2
Re: Code
In my opinion the define requirement is silly (no one would write code like that in reality, they'd use an inline function), but it's fairly straightforward. The define will look pretty much identical to the formulas as-written. Just watch out for integer division on the 4/3 bit...
Note the use of pow() in the code above----you'll use it in yours, too. (Well, not necessarily but you may as well.)
-
November 5th, 2009, 11:10 AM
#3
Re: Code
That define is just idiotic, and will only cause hurt to you if you use constructs like that.
Make a function, you have examples.
Code:
float Volume_Sphere( float radius)
{
Do something with the radius
return a meaningful value
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|