How to write recursive functions in iterative way


Let's begin with an example from CLRS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 int gcdex(int a,int b,int &x,int &y)
{
    int d,dp,xp,yp;
    if(b==0)
    {
        d = a;
        x = 1;
        y = 0;
    }
    else
    {
        dp = gcdex(b,a%b,xp,yp);
        d = dp;
        x = yp;
        y = xp - (a/b)*yp; 
    }
    return d;
} 

Well, are you going to show us how to write it?
Topic archived. No new replies allowed.