ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi ya'll as the subject says I'm very new to C and I had a two questions when doing some programming excerises with the book I'm using, O'Reilly's Practical C Programming 3rd Ed..
By the way thanks to all those who have helped me in the past on here.
1) When using fractions in C would just having 4/3 * 5 actually tell C four third's times 5? And if I use 1.25 * 5 will that give me the same result?
2) I've been trying to write a program to calculate the volume of a sphere and the program is fine I'm just having trouble with the formula. The formula I'm using is (4/3) * PI * Radius ^3.
So four thirds times Pi time the radius of the sphere to the third power. Now I'm almost positive my problem lies with the ^3. For one is there a shorthand version of using exponents in C? like 5e2 or something... And second in regards to that formula what do I multiply to the third power? Is it (4/3 * Pi * Radius) ^3? or (4/3 * Pi) * (radius * radius * radius)? Or something else? I'm lost basically... so any help would be much appreciated.
From what I know, dividing in C++ is kind of difficult. Doing something that DOES NOT have a remainder is simple and done like 4/2 = 2. But the problem comes in with the remainder. Doing 4/3 would equal 1, not 1.whatever because the / operator does not give the remainder. I believe the % (modulos or something) operator gives only the remainder. So you code would need to be like ((4/3)+(4%3))*5.
I could be completely wrong as I am a newb with C++, but that is how I believe it needs to be done.
#1 Nope. The compiler will create integer division unless you force it to create floating point division. You force floating point by using a decimal somewhere in your formula. Using integer division, 4/3 will equal 1. Changing the expression to 4.0/3, 4/3.0, or even 4./3 forces the program to do floating point division. In that case, 4.0/3 equals 1.33333333...
#2 To my knowledge, there is no math operator (like +,-, /, *) to raise a number to a power. You'll have to use a function in the math library ( pow() for instance ) or write a function yourself to calulate it. The formula to calculate a volume of a sphere relies on the radius being cubed. So the exponent only applies to the radius.
Last edited by Dark_Helmet; 07-04-2004 at 12:21 PM.
Dark_Helmet is correct on both counts. pow() is the way to go for exponential math. Make sure that when you compile your program (if you use the pow() function) that you link in the math library (with gcc just add -lm to the end of the compile command, eg. gcc myfile.c -o myfile -lm).
or you could just go (rad * rad * rad). It's the same thing, you wouldn't need to include any additional files or the pow(rad, 3). If the program does a lot of other math things, like calculates surface area of the sphere or other calculations that use powers a lot, then it would probably be easier to #include<math.h> and use the pow() method.
Hey everyone thank you for the help, Darkhelmet needforspeed and everyone else it was just as simple as changing 4/3 to 4.0/3.0 the funny thing is I declared the variable as float too, I just totaly spaced out on that. By the way i'm including the code so if anyone needs to calculate the volume of sphere this program will do just dandy.
Thanks again,
Spiros
P.S Yes I know the program is almost completely useless, but it does it's meager job well and thats all you can ask for.
/* Volume.C - Calculates the volume of a sphere */
#include <stdio.h>
char line[100]; /* line of input data */
float volume; /* volume of the sphere */
float radius; /* radius of the sphere */
const float PI = 3.1415927; /* PI */
float partial; /* the partial calculation */
int main()
{
printf("Please enter the radius of the circle you wish to know the volume of: ");
I just realized after posting this relpy that partial was a totally useless variable so I got rid of it here's volume.c part du.
/* Volume.C - Calculates the volume of a sphere */
#include <stdio.h>
char line[100]; /* line of input data */
float volume; /* volume of the sphere */
float radius; /* radius of the sphere */
const float PI = 3.1415927; /* PI */
int main()
{
printf("Please enter the radius of the circle you wish to know the volume of: ");
By the way, a few things I found when I first learned C/C++ was:
1) Use double instead of floats. It is much better in its precision. Besides, typing decimals like 4.0 or 3.14159... in C automatically translates it into a double, not a float (recent versions of gcc). Use %lf instead of %f when using doubles
2) Use PI = acos(0.0) * 2.0 for pi. That way you don't ever have to worry about precision with your PI value.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.