473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Problems with calculating volume of a sphere.

Hi I'm just learning C and I was trying to do an exercise in a book
which asks me to write a program to calculate the volume of a
sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3

and I've been trying to write that but have been unsuccesfull so far.
I tryed:
volume = 4/3 * PI * radius;
volume = volume * volume * volume;

or
volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *
radius));

And a few other variations on the algorithm. The rest of program works
fine I just don't know the proper syntax for creating this algorithm.
If someone would be kind enough to answer my question I'd also
appreciate if they could explain why the syntax needs to be the way it
is. Since right now I'm quite confused.

-rafa
Nov 13 '05 #1
3 20155

"rafa" <pa*****@phayze.com> wrote in message
news:pa****************************@phayze.com...
Hi I'm just learning C and I was trying to do an exercise in a book
which asks me to write a program to calculate the volume of a
sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3

and I've been trying to write that but have been unsuccesfull so far.
I tryed:
volume = 4/3 * PI * radius;
volume = volume * volume * volume;

or
volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *
radius));

And a few other variations on the algorithm. The rest of program works
fine I just don't know the proper syntax for creating this algorithm.
If someone would be kind enough to answer my question I'd also
appreciate if they could explain why the syntax needs to be the way it
is. Since right now I'm quite confused.

-rafa


4/3 is an integer expression, consider volume = 4.0/3.0 * PI * radius *
radius * radius;

Eric Bernard
Nov 13 '05 #2


rafa wrote:
Hi I'm just learning C and I was trying to do an exercise in a book
which asks me to write a program to calculate the volume of a
sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3

and I've been trying to write that but have been unsuccesfull so far.
I tryed:
volume = 4/3 * PI * radius;
volume = volume * volume * volume;

or
volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *
radius));


You need to force a float type division. Change 4/3 to 4.0/3.0.
The correct formula should be:
4.0/3.0*PI*radius*radius*radius

#include <stdio.h>

#define PI 3.14

int main(void)
{
double volume,radius;

radius = 3.0;
volume = 4.0/3.0*PI*radius*radius*radius;
printf("The volume of a sphere of radius 3cm is %.2f cu cm\n",
volume);
return 0;
}

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #3
rafa wrote:
Hi I'm just learning C and I was trying to do an exercise in a book
which asks me to write a program to calculate the volume of a
sphere. I know the formula is Volume = (4/3)*(Pi)*(Radius)^3

and I've been trying to write that but have been unsuccesfull so far.
I tryed:
volume = 4/3 * PI * radius;
volume = volume * volume * volume;

or
volume = (4/3 * (PI * radius)) * (4/3 * (PI * radius)) * (4/3 * (PI *
radius));


It is the radius that is cubed, not (4. * PI * radius)/3.

Also note that (4/3) is 1, but (4./3), (4/3.), (4./3.) are all floating
point values (actually doubles) with value 1.3333....
Since PI is a floating point value, (4 * PI)/3 will also have the correct
value, up to the required precision of a double or of PI, whichever is
less. The FAQ explains these things.
const double PI=3.14159265358979323846;
double cube(double x) {return x*x*x;}
double volume(double r) {return 4*PI*cube(r)/3; }


--
Martin Ambuhl

Nov 13 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: jcnews | last post by:
I am a beginner programmer just starting to learn Java. Can someone tell me what is wrong with this applet? The output is: Circumference is 47.1225 Area is 176.70938 Volume is 1767.0938 I...
1
by: DJTB | last post by:
zodb-dev@zope.org] Hi, I'm having problems storing large amounts of objects in a ZODB. After committing changes to the database, elements are not cleared from memory. Since the number of...
24
by: Stavros Christoforou | last post by:
Hello everyone, I was wondering if someone could help me with an issue I have in C++. I want to select random points within the volume of a sphere. I know how to get random numbers using srand()...
6
by: Zee | last post by:
I need help using a query to calculate or subtract values in a row tblCalRows RowID RowValue 1 A 2 B 3 C 4.... D
17
by: Lloyd Sheen | last post by:
This IDE is driving me nuts. I needed another button so I copied an existing one, changed the Text and the id and position by drag and drop. Well then I run and get the following: Control...
9
by: DarkProtoman | last post by:
How would I write a function to calculate RSA Decryption key. I'm trying to write an RSA encryption program, and I'm stuck on calculating the decryption key. How do you check for coprimality?...
10
by: Joseph Geretz | last post by:
I need to calculate miles per degree longitude, which obviously depends on latitude since lines of longitude converge at the poles. Via Google, I've come up with the following calculation: ...
1
by: Mark B | last post by:
I am trying to programmatically create some icons for an Office 2007 add-in (VB/C#) that will appear on the navigation Ribbon. The particular icon is simply a colored round circle representing a...
6
by: mlt | last post by:
Anyone has an idea to generate points for a solid sphere in C++ that can be plotted with gnuplot? I have tried to plot simple functions based on output from a function written in C++ which worked...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.