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.
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
#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);
}
#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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.