LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 11-17-2006, 11:53 PM   #1
jabka
Member
 
Registered: Sep 2005
Distribution: Debian unstable && Kazit (reformated knoppix)
Posts: 194

Rep: Reputation: 31
how to work with fractions in c ? (help neded);


Howdy..

i got hw and ine asinment is like that :
the user enter two numbers (a and b) and the prog will show the fraqtion in his smallest form :
Exm : 4 6
the ouptut will be 2/3
so what i tried was :
//a,b,c are int the if(b<a) is requeired
if (b>a){
c= b / a;
a=a/c;
printf("%d/%d ",a,c);
}
it fine for 2/4 and 5/25 but for more complex form :
4 6
i don't know what to do
 
Old 11-18-2006, 12:58 AM   #2
pda_h4x0r
Member
 
Registered: Feb 2006
Location: somewhere in cyberspace
Distribution: Debian, Familiar
Posts: 380

Rep: Reputation: 31
Code:
#define Min(x,y) ((x)<=(y)?(x):(y))
#define Abs(x) ((x)<0?-(x):(x))

void main(int argc, char** argv) {

// assume a and b have been entered
int min = Min(Abs(a), Abs(b));
while(min--) {
if(a%min == 0 && b%min == 0) {
a /= min; b /= min;
}
}
printf("%d/%d", a, b);

}
At least, that's how I'd do it.

Last edited by pda_h4x0r; 11-18-2006 at 12:59 AM.
 
Old 11-18-2006, 09:46 AM   #3
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
Quote:
Originally Posted by pda_h4x0r
Code:
#define Min(x,y) ((x)<=(y)?(x):(y))
#define Abs(x) ((x)<0?-(x):(x))

void main(int argc, char** argv) {

// assume a and b have been entered
int min = Min(Abs(a), Abs(b));
while(min--) {
if(a%min == 0 && b%min == 0) {
a /= min; b /= min;
}
}
printf("%d/%d", a, b);

}
At least, that's how I'd do it.
void main() ? bahh.. sorry i just threw up a little. main always should return int.
 
Old 11-18-2006, 11:10 AM   #4
jabka
Member
 
Registered: Sep 2005
Distribution: Debian unstable && Kazit (reformated knoppix)
Posts: 194

Original Poster
Rep: Reputation: 31
np .. thnx for the help ..
 
Old 11-19-2006, 06:59 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
oh good grief, how does this kid learn anything if he's spoon fed his own coursework answers??? members should *NOT* ask homework questions, and other members should *NOT* provde full answers as and when they do get posted.
 
Old 11-19-2006, 10:56 PM   #6
pda_h4x0r
Member
 
Registered: Feb 2006
Location: somewhere in cyberspace
Distribution: Debian, Familiar
Posts: 380

Rep: Reputation: 31
LOL, but that's how WE learn to code

Joking, of course.

Last edited by pda_h4x0r; 11-19-2006 at 10:57 PM.
 
Old 11-20-2006, 12:55 PM   #7
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
pda_h4x0r: Inefficient for big numbers.

Acid_kewpie is right, it's Jabka's homework. But maybe a tiny clue would be OK? From middle school you remember that you divide numerator and denominator by their greatest common denominator. A few thousand years ago a Greek proposed a nifty GCD algorithm; I think the guy's name was Euclid.
 
Old 11-20-2006, 03:06 PM   #8
pda_h4x0r
Member
 
Registered: Feb 2006
Location: somewhere in cyberspace
Distribution: Debian, Familiar
Posts: 380

Rep: Reputation: 31
Yes, I am aware that it is HORRIBLY inefficient for large numbers (in fact, horribly inefficient in general--my algorithm is O(n) and Euclid's is O(log n)). However, the algorithm demonstrates a simplified, conceptual implementation, intended to show Jabka the concepts of the solution. This code snippet is analogous to the Bubble Sort algorithm: easy to understand for newbies but without a doubt one of the WORST algorithms of its kind, in terms of performance.

Last edited by pda_h4x0r; 11-20-2006 at 03:17 PM.
 
  


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
Help neded shell script arungs Linux - Newbie 3 10-27-2005 08:34 AM
sleep() looking for a way to pause execution for fractions of a second qwijibow Programming 3 02-20-2005 02:38 AM
got ALSA to work in root, need help getting it to work in my user profile ic3 Slackware 2 11-28-2003 10:32 PM
FALCONS EYE (nethack) FAILS ON STARTUP, use to work, wont work even after reinstall roorings Linux - Software 0 10-08-2003 10:39 PM
Newbe help neded opo Linux - Software 20 01-17-2003 03:19 AM

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

All times are GMT -5. The time now is 06:41 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