user
user

Reputation: 1

Rewrite a simple iterative algorithm recursively

It is known that in some cases an iteration can be transformed in a recursive algorithm. How can I rewrite an iteration as simple as the following one as a recursion?

for(i=0,i<500,i++) 
    row_multiply();

I realize that, as it has been pointed out, I have to try something like...

void recursiveSolution(int i)

    {
        raw_multiply();
        if (i< 499)
            recursiveSolution(i+ 1);
    }

..., but I am unsure about how to deal with the base case and how to structure coherent C code for the recursive rewrite.

Upvotes: 0

Views: 149

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53046

May be this way?

void recursiveSolution(int count)
{
    raw_multiply();
    if (count < 499)
        recursiveSolution(count + 1);
}

and then to start it

recursiveSolution(0);

Upvotes: 1

Related Questions