Naija Ryders

Go Back   Naija Ryders > Main > Techie Forum
Connect with Facebook
Register Blogs FAQ Members List Guitar Hero Calendar Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old 07-17-2007, 10:57 PM   #1 (permalink)
Registered User
 
PRIMO's Avatar
 
Join Date: Apr 2004
Location: NYC
Posts: 1,262
Points: 20,246.20
Bank: 0.00
Total Points: 20,246.20
Donate
Rep Power: 1186954 PRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legend
Default java problem, please help

I need to create a method that can simplify a fraction, that is, represent it as a fraction where the numerator and the denominator have no common divisors. it should read in the numerator and the denominator and then print out the fraction in its simplified form.

since dividing the numerator and denominator by their gcd will simplify the fraction.
here is a sample run (user input)
enter a numerator:
6
enter a denominator:
8

your fraction is 6/8
the simplified form is 3/4

I wrote this code and it compiles and makes sense to me at least. However, it doesn't work properly for more difficult integers like 20, 30. Can anyone spot the problem. nothing is wrong with the compiling, just doesn't work logically. I can't use the Euclidean Algorithm, so I basically had to write the simplest program to find gcd.


public static int gcd(int a, int b)

{

int gcd;

if (a > b && a % b == 0)

{

gcd = b;

return gcd;

}

else if (b > a && b % a == 0)

{

gcd = a;

return gcd;

}

else if (a > b && a % b != 0)

{

int count = b;

do

{

count = count - 1;

}

while (b % count != 0 && a % count != 0);

gcd = count;

return gcd;

}

else if (b > a && b % a != 0)

{

int count = a;

do

{

count = count - 1;

}

while (b % count != 0 && a % count != 0);

gcd = count;

return gcd;

}

else

{

gcd = a;

return gcd;

}

}
__________________
Hoes on my dick, like Prim' you the best baby...
PRIMO is offline   Reply With Quote
Old 07-17-2007, 11:15 PM   #2 (permalink)
Registered User
 
omonija's Avatar
 
Join Date: Jun 2005
Location: somewhere shaaa...
Posts: 2,551
Points: 7,200.00
Bank: 8,952,368.10
Total Points: 8,959,568.10
Donate
Rep Power: 0 omonija omonija omonija omonija omonija omonija omonija omonija omonija omonija omonija
Default

i think your problem is your structure. too many if-elses and whiles in the same block of code is usually a sign of trouble.

why don't you consider doing it like this: create an array of factors for each number, then go through and compare both arrays. select the largest common factor, and divide both by the number, then present your answer? that should take like only 3 while/for loops. let me know if you want further explanations.

also, it may be better to use matlab for this, unless you absolutely must use java.
omonija is offline   Reply With Quote
Old 07-17-2007, 11:24 PM   #3 (permalink)
goat herder
 
COMMONCENTS's Avatar
 
Join Date: Apr 2002
Location: in love
Posts: 26,891
Points: 154,741.95
Bank: 25,706,094,021.84
Total Points: 25,706,248,763.80
Donate
Rep Power: 21474910 COMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legend
Default

yeah thats way too messy....i dont know much about java, but basic data structures is definitely missing in that code.
__________________
COMMONCENTS is offline   Reply With Quote
Old 07-17-2007, 11:32 PM   #4 (permalink)
goat herder
 
COMMONCENTS's Avatar
 
Join Date: Apr 2002
Location: in love
Posts: 26,891
Points: 154,741.95
Bank: 25,706,094,021.84
Total Points: 25,706,248,763.80
Donate
Rep Power: 21474910 COMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legend
Default

ok if u have n/d,
write a while i > 0,
}
if n%a = 0 && d%a = 0
then display n/a '/' d/a
else
i-1
}
basic data structure.
thats more for c/c++ sha.
__________________

Last edited by COMMONCENTS; 07-17-2007 at 11:36 PM..
COMMONCENTS is offline   Reply With Quote
Old 07-17-2007, 11:38 PM   #5 (permalink)
Mami Wata
 
bettybogus's Avatar
 
Join Date: May 2003
Location: Under the sea
Posts: 17,591
Points: 77,006.55
Bank: 1,902,669.48
Total Points: 1,979,676.03
Donate
Rep Power: 3212561 bettybogus is a legendbettybogus is a legendbettybogus is a legendbettybogus is a legendbettybogus is a legendbettybogus is a legendbettybogus is a legendbettybogus is a legendbettybogus is a legendbettybogus is a legendbettybogus is a legend
Default

clean up your code, dude.. you only need one return statement.. after all the if else statements.. I'd help.. but, I'm a little busy right now..
__________________
Bonita Applebum..y'know..like a hip hop song..
Like a perfect verse, over a dope beat..

~~~~~~~

Jesus take the wheel..


bettybogus is offline   Reply With Quote
Old 07-17-2007, 11:46 PM   #6 (permalink)
Registered User
 
PRIMO's Avatar
 
Join Date: Apr 2004
Location: NYC
Posts: 1,262
Points: 20,246.20
Bank: 0.00
Total Points: 20,246.20
Donate
Rep Power: 1186954 PRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legend
Default

Quote:
Originally Posted by COMMONCENTS
ok if u have n/d,
write a while i > 0,
}
if n%a = 0 && d%a = 0
then display n/a '/' d/a
else
i-1
}
basic data structure.
thats more for c/c++ sha.
did you compile it with that and it runs?
__________________
Hoes on my dick, like Prim' you the best baby...
PRIMO is offline   Reply With Quote
Old 07-17-2007, 11:52 PM   #7 (permalink)
Registered User
 
aba made's Avatar
 
Join Date: Jun 2003
Location: waterside
Posts: 5,897
Points: 1,246,715.09
Bank: 1.15
Total Points: 1,246,716.24
Donate
Rep Power: 21474866 aba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legendaba made is a legend
Default

Quote:
Originally Posted by PRIMO
did you compile it with that and it runs?
lol. is this a joke?
__________________
It is the mark of an educated mind to be able to entertain a thought without accepting it. - Aristotle
aba made is offline   Reply With Quote
Old 07-17-2007, 11:53 PM   #8 (permalink)
goat herder
 
COMMONCENTS's Avatar
 
Join Date: Apr 2002
Location: in love
Posts: 26,891
Points: 154,741.95
Bank: 25,706,094,021.84
Total Points: 25,706,248,763.80
Donate
Rep Power: 21474910 COMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legend
Default

Quote:
Originally Posted by PRIMO
did you compile it with that and it runs?
thats not in any language...just giving u an idea of how u should go about it in any language u decide to use it with.

if i was to write i'd use c++, and i's only need one while loop and one if statement.
__________________
COMMONCENTS is offline   Reply With Quote
Old 07-17-2007, 11:55 PM   #9 (permalink)
Registered User
 
PRIMO's Avatar
 
Join Date: Apr 2004
Location: NYC
Posts: 1,262
Points: 20,246.20
Bank: 0.00
Total Points: 20,246.20
Donate
Rep Power: 1186954 PRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legend
Default

Quote:
Originally Posted by aba made
lol. is this a joke?
thats not a joke aba boy
__________________
Hoes on my dick, like Prim' you the best baby...
PRIMO is offline   Reply With Quote
Old 07-17-2007, 11:56 PM   #10 (permalink)
Registered User
 
omonija's Avatar
 
Join Date: Jun 2005
Location: somewhere shaaa...
Posts: 2,551
Points: 7,200.00
Bank: 8,952,368.10
Total Points: 8,959,568.10
Donate
Rep Power: 0 omonija omonija omonija omonija omonija omonija omonija omonija omonija omonija omonija
Default

Quote:
Originally Posted by PRIMO
did you compile it with that and it runs?
see dis kopykata

even if that works, that would only be the first part. maybe you should say what your logic is. your code isn't clear at all.
omonija is offline   Reply With Quote
Old 07-17-2007, 11:56 PM   #11 (permalink)
goat herder
 
COMMONCENTS's Avatar
 
Join Date: Apr 2002
Location: in love
Posts: 26,891
Points: 154,741.95
Bank: 25,706,094,021.84
Total Points: 25,706,248,763.80
Donate
Rep Power: 21474910 COMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legendCOMMONCENTS is a legend
Default

Quote:
Originally Posted by aba made
lol. is this a joke?
lol....
__________________
COMMONCENTS is offline   Reply With Quote
Old 07-18-2007, 12:01 AM   #12 (permalink)
Registered User
 
PRIMO's Avatar
 
Join Date: Apr 2004
Location: NYC
Posts: 1,262
Points: 20,246.20
Bank: 0.00
Total Points: 20,246.20
Donate
Rep Power: 1186954 PRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legend
Default

i seriously need help, make una help and stop making fun of my condition. I don loose sleep for 2 night becos of this nonsense
__________________
Hoes on my dick, like Prim' you the best baby...
PRIMO is offline   Reply With Quote
Old 07-18-2007, 12:07 AM   #13 (permalink)
Registered User
 
PRIMO's Avatar
 
Join Date: Apr 2004
Location: NYC
Posts: 1,262
Points: 20,246.20
Bank: 0.00
Total Points: 20,246.20
Donate
Rep Power: 1186954 PRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legend
Default

why una dey act like say no be one nigeria wey we be
__________________
Hoes on my dick, like Prim' you the best baby...
PRIMO is offline   Reply With Quote
Old 07-18-2007, 12:14 AM   #14 (permalink)
Registered User
 
ebunoluwa's Avatar
 
Join Date: Apr 2002
Location: Japan
Posts: 4,137
Points: 2,487.55
Bank: 36,434.77
Total Points: 38,922.32
Donate
Rep Power: 87149 ebunoluwa is a legendebunoluwa is a legendebunoluwa is a legendebunoluwa is a legendebunoluwa is a legendebunoluwa is a legendebunoluwa is a legendebunoluwa is a legendebunoluwa is a legendebunoluwa is a legendebunoluwa is a legend
Default

Did you clean it up at all based on the suggested comments? What's your code looking like now and is it still not working?
ebunoluwa is offline   Reply With Quote
Old 07-18-2007, 12:29 AM   #15 (permalink)
Registered User
 
PRIMO's Avatar
 
Join Date: Apr 2004
Location: NYC
Posts: 1,262
Points: 20,246.20
Bank: 0.00
Total Points: 20,246.20
Donate
Rep Power: 1186954 PRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legendPRIMO is a legend
Default

^it still no dey work. i don try many ways, but it still dey give me errorrs. please work with me, compile and run it and help me figure where the problem is
__________________
Hoes on my dick, like Prim' you the best baby...
PRIMO is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is On
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Points Per Thread View: 0
Points Per Thread: 0
Points Per Reply: 0
Forum Jump


All times are GMT +2. The time now is 09:18 AM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.2.0
NaijaRyders