![]() |
|
#1
|
|||
|
|||
Calculate volume/surface area of a sphereHey 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:
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
|
|||
|
|||
Re: Calculate volume/surface area of a sphereQuote:
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:
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:
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:
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:
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:
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 GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The