`for` ordinary syntax to debug endless loop-Collection of common programming errors
“I think the problem is that (a,b) need to be interchanged depending on which is greater. (The second for loop). The Euclidean algorithm does that. This one does not seem to be doing that.
GCD of an array of numbers: I would use the standard Euclidean algorithm.
If you are iterating from the end : GCD (a,b) =K
Now call GCD(K,next_element_in_array) . That should work.
enter code here
Written in Pseudo-code What he is trying to do is more understandable:
For(b =array[end]; )
{
For(a= array[end-1]; end >=0 ;end --)
{
(i)Divide a/b
(ii)Put the new 'reduced' divisor in 'b'.
(iii)Put the reminder back in c
}
end --
}
}
}
So, at the end of the loop, anything What he is trying to simulate is the hand-division computation of gcd of series of numbers – the high-school trick!
The problem, as I mentioned above,is in swapping (a,b) which is greater.