Programming This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
07-04-2004, 12:08 PM
|
#1
|
Member
Registered: Jun 2004
Location: New York
Distribution: Red Hat
Posts: 32
Rep:
|
Beginner with C question's...
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.
Thanks in advance,
Spiros
|
|
|
07-04-2004, 12:12 PM
|
#2
|
Member
Registered: Jan 2004
Posts: 35
Rep:
|
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.
DF
|
|
|
07-04-2004, 12:20 PM
|
#3
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
#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.
|
|
|
07-04-2004, 12:28 PM
|
#4
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
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).
|
|
|
07-04-2004, 01:10 PM
|
#5
|
Member
Registered: Aug 2003
Distribution: Gentoo
Posts: 73
Rep:
|
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.
|
|
|
07-04-2004, 01:29 PM
|
#6
|
Member
Registered: Jan 2004
Posts: 35
Rep:
|
Dark,
Thanks for saying the 4.0 where you add the 0. I never thought of doing that. Now I can do divison!
Heh, my bad on being completely wrong.
|
|
|
07-04-2004, 02:16 PM
|
#7
|
Senior Member
Registered: Jan 2003
Posts: 2,786
|
No sweat big man. It's all part of getting familiar with the language.
I fought with integer and floating point stuff before, although, this will be the only time you hear me admit it 
|
|
|
07-05-2004, 07:34 PM
|
#8
|
Member
Registered: Jun 2004
Location: New York
Distribution: Red Hat
Posts: 32
Original Poster
Rep:
|
I got it!
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: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%f", &radius);
partial = 4.0/3.0 * PI * (radius * radius * radius);
volume = partial;
printf("The volume of the sphere is %f\n", volume);
return (0);
}
|
|
|
07-05-2004, 07:37 PM
|
#9
|
Member
Registered: Jun 2004
Location: New York
Distribution: Red Hat
Posts: 32
Original Poster
Rep:
|
Wow uhh...
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: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%f", &radius);
volume = 4.0/3.0 * PI * (radius * radius * radius);
printf("The volume of the sphere is %f\n", volume);
return (0);
}
|
|
|
07-06-2004, 12:32 AM
|
#10
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
Congratulations
You can also do away with the line variable. Then you can get rid of your fgets() call and just use scanf("%f", &radius);
|
|
|
07-07-2004, 04:01 AM
|
#11
|
LQ Newbie
Registered: Jun 2004
Posts: 10
Rep:
|
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.
Frank
|
|
|
All times are GMT -5. The time now is 12:20 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|