GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Nov-2009, 17:19
ReynaldoEloy ReynaldoEloy is offline
New Member
 
Join Date: Nov 2009
Posts: 1
ReynaldoEloy is on a distinguished road

Calculate volume/surface area of a sphere


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:

CPP / C++ / C Code:
//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

how would the code look?
Last edited by admin : 04-Nov-2009 at 19:26. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 05-Nov-2009, 09:21
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Calculate volume/surface area of a sphere


Quote:
Originally Posted by ReynaldoEloy
...i was using this code as a reference
I wouldn't want to hurt anyone's feelings, but as a reference for learning programming, this is the pits. (And I mean really pitiful.)

Why, oh why, would anyone use a #defined macro to implement a function like this? (That's a rhetorical question; I don't really want to see a response.)

So the first suggestion is something like this: Delete the #define stuff and just make the function do the calculations directly:
CPP / C++ / C Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

float distance(float x1, float y1, float x2, float y2)
{
    return sqrt(pow((x2-x1),2.0)+ pow((y2-y1),2.0));
}


int main()
{
.
.
.
}

Secondly: This is the C++ board. Now, the code that you showed might compile OK as a C++ program, but it's C code. (See Footnote.)

If you are supposed to be learning C++, then I respectfully suggest that you look for C++ examples in your text or other course material, or find a tutorial on the web. Maybe http://www.cplusplus.com/doc/tutorial/files/. Poke around and you might find an explanation and some examples: http://www.cplusplus.com/doc/tutorial/functions/

Since you indicated that you don't know where to start, I suggest that you start at the beginning and work your way through.

If you are supposed to be writing C and not C++, then it might be more appropriate to submit your question to the C programming section of this forum.
Quote:
Originally Posted by ReynaldoEloy
*note* VOLUME of a SPHERE = ( 4 / 3 ) * * radius 3

The volume of a sphere is 4/3 * pi * (radius to the power 3). See, for example, http://www.1728.com/diamform.htm
So, you would make a function that takes a single argument, the radius, and returns the value of that expression.

I'll use double precision, but if you really, really, want single precision instead of double, you could use floats instead of doubles:

CPP / C++ / C Code:

const double Pi = 3.14159265358979323846;

double sphere_volume(double radius)
{
    return 4.0/3.0 * Pi * radius * radius * radius;
}

Note that I could have used the pow() function to raise the value of radius to the third power, but I just chose to multiply it by itself the appropriate number of times. Chacun à son goût

Quote:
Originally Posted by ReynaldoEloy
SURFACE AREA of a SPHERE = 4 * * radius 2

The surface area of a sphere is 4.0 times pi times (radius to the power 2).

Make a function that takes a single argument, the radius, and returns the value of this expression.

As for the other functions that you mention: Look at function examples in whatever reference material you are using.

Then:
  1. Define exactly what the function is supposed to do. Write it down. This should be part of the comments that accompany the code that implements the function. It also allows us to understand something about what you have in mind (in case you want to make a meaningful request for help).

  2. Does the function have any parameters? If so then describe each one. Exactly. What is the data type, and what does each parameter represent?

  3. Does the function have a (non-void) return data type? If so, then explain what it means. What is the data type, and what does the value represent?
Regards,

Dave

Footnote: The header <conio.h> is not a standard header for C or C++ (is not now, has never been part of any language standard), so your program may or may not work with your compiler. If your instructor requires you to use the non-standard header <conio.h> and the non-standard function getch(), I can't help you, since my compiler of choice has neither one. There have been many discussions on this forum and others as to why sticking to standards is important for people's growth as a programmer, so I won't repeat the arguments here.
Last edited by davekw7x : 05-Nov-2009 at 10:41.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:01.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.