Program check

Reel

Diamond Member
Jul 14, 2001
4,484
0
76
I am working on a homework problem that asks me to prove that two different methods of finding the greatest common denominator work. One of them is clear that it works. The other it seems to me does not work. I even went so far as to write a java program to check this out. Can someone give this a glance and tell me if I am mistaken or not please?

It seems to me that no matter what this program will always end up in a divide by 0 situation. Take two easy numbers: 21 and 7. We can glance at this and say the greatest common divisor is 7. However, the program does 21 mod 7 which yields 0. Then next loop 7 mod 0 which is a divide by 0.

Pseudocode:
 

agnitrate

Diamond Member
Jul 2, 2001
3,761
1
0
Why don't you just put the condition in that if i == 0 after the mod, return whatever you divided by?
 

Reel

Diamond Member
Jul 14, 2001
4,484
0
76
Thanks. I don't have to fix it. I just wanted to make sure I wasn't crazy and that there was a problem with the pseudocode provided by the book.
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Interesting trick question -- without checks for 0 result in both the if and else I think this will always fail unless i==j. Unless they've defined "mod" differently from how it behaves in C, C++, Pascal, and BASIC.