LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Beginner with C question's... (https://www.linuxquestions.org/questions/programming-9/beginner-with-c-question%27s-201034/)

nostrum 07-04-2004 12:08 PM

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

dfownz 07-04-2004 12:12 PM

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

Dark_Helmet 07-04-2004 12:20 PM

#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.

itsme86 07-04-2004 12:28 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).

needforspeed 07-04-2004 01:10 PM

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.

dfownz 07-04-2004 01:29 PM

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.

Dark_Helmet 07-04-2004 02:16 PM

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 :D

nostrum 07-05-2004 07:34 PM

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);
}

nostrum 07-05-2004 07:37 PM

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);
}

itsme86 07-06-2004 12:32 AM

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);

fpmc 07-07-2004 04:01 AM

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 03:36 PM.