07-02-2003, 01:30 AM
Well, you seem to be against recursion. It's slow and a stack hog but in problems involving a limited number of calls it can be the simplest solution. Obviously it's no good to calculate factorials but it's the best solution when a tree must be explored, as Moneo said.
Loose Caboose: Your floodfill example is only an exampe of a poor algorithm. It just generates calls without knowing if there is a former call for the same point. With an iterative implementation you should create a huge queue array or you would have a lot of gaps. There are much better ways to floodfill. The one you posted is good too teach what's floodfill, you can see it at a first glance.
To keep the thread open (and again not a good example of what recursion should be used on). A mcd calculation
Loose Caboose: Your floodfill example is only an exampe of a poor algorithm. It just generates calls without knowing if there is a former call for the same point. With an iterative implementation you should create a huge queue array or you would have a lot of gaps. There are much better ways to floodfill. The one you posted is good too teach what's floodfill, you can see it at a first glance.
To keep the thread open (and again not a good example of what recursion should be used on). A mcd calculation
Code:
DECLARE FUNCTION mcd& (a&, b&)
PRINT mcd&(320, 240)
FUNCTION mcd& (a&, b&)
c& = a& MOD b&
IF c& THEN
mcd& = mcd&(b&, c&)
ELSE
mcd& = b&
END IF
END FUNCTION
Antoni