LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-04-2004, 12:08 PM   #1
nostrum
Member
 
Registered: Jun 2004
Location: New York
Distribution: Red Hat
Posts: 32

Rep: Reputation: 15
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
 
Old 07-04-2004, 12:12 PM   #2
dfownz
Member
 
Registered: Jan 2004
Posts: 35

Rep: Reputation: 15
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
 
Old 07-04-2004, 12:20 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
#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.
 
Old 07-04-2004, 12:28 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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).
 
Old 07-04-2004, 01:10 PM   #5
needforspeed
Member
 
Registered: Aug 2003
Distribution: Gentoo
Posts: 73

Rep: Reputation: 15
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.
 
Old 07-04-2004, 01:29 PM   #6
dfownz
Member
 
Registered: Jan 2004
Posts: 35

Rep: Reputation: 15
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.
 
Old 07-04-2004, 02:16 PM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
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
 
Old 07-05-2004, 07:34 PM   #8
nostrum
Member
 
Registered: Jun 2004
Location: New York
Distribution: Red Hat
Posts: 32

Original Poster
Rep: Reputation: 15
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);
}
 
Old 07-05-2004, 07:37 PM   #9
nostrum
Member
 
Registered: Jun 2004
Location: New York
Distribution: Red Hat
Posts: 32

Original Poster
Rep: Reputation: 15
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);
}
 
Old 07-06-2004, 12:32 AM   #10
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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);
 
Old 07-07-2004, 04:01 AM   #11
fpmc
LQ Newbie
 
Registered: Jun 2004
Posts: 10

Rep: Reputation: 0
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner needs various help. meeep Linux - Software 44 08-21-2005 12:10 AM
A few newb question's Abbaddon Slackware 13 09-22-2004 04:58 PM
Help this beginner weng Linux - Newbie 5 12-20-2003 08:55 AM
RH 9.0 question's related to Dell Inspiron 5150 shankariyer Linux - Laptop and Netbook 1 12-18-2003 05:05 AM
I am THE beginner please help me :( statuszero Linux - General 10 05-26-2002 03:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration