0

I am looking for a way to transform this recursive function to an iterative one. I have tried thinking about how to actually do it but it hurts me to think about it because I am not really familiar with how to convert from recursive to iterative. If anyone can help me out how to deal with this I would be very grateful.

int function(int row, int col)
{
    int* current = &A[row][col];

    if (*current == "f") {
        return 1;
    }

    if (*current == ' ') {
        *current = '.';
        if (function(row, col - 1)){
            *current = '.';
            return 1;
        }
        if (function(row + 1, col)){
            *current = '.';
            return 1;
        }
        if (function(row, col + 1)){
            *current = '.';
            return 1;
        }
        if (function(row - 1, col)){
            *current = '.';
            return 1;
        }
    }

    return 0;
}
  • Is this function supposed to do something? – anotherOne Jan 05 '21 at 23:24
  • It is supposed to search for the letter 'f' in the matrix. Whenever the function finds a space, it adds a dot to mark the current position he's at as visited. If it reaches "f", it should stop. – narcester Jan 05 '21 at 23:28
  • Where is `A` defined, and what is it initialized to? Also, if you do this once `*current = '.';` then there is no point in doing it 4 more times afterwards. – dxiv Jan 05 '21 at 23:41
  • A is a global variable and it is initialized in another function. It is a matrix with walls along with a starting point and a finishing point. – narcester Jan 05 '21 at 23:48
  • The only way to approach something like this is to put down the keyboard. Take out an 8.5x11 sheet of paper and pencil and then write out the steps that take place for the first few iterations. Look at how `row` and `col` are incremented and decremented (the fact that both are taking place likely rules out a `for` loop, a `while` loop will likely be needed). Pay attention to your exit conditions and track how those occurs. Remember each recursive call is a separate function call, so a `return` does not end the function, it just returns control to the previous recursive call. – David C. Rankin Jan 05 '21 at 23:55
  • So no other way I see. Well then, I'll try my best and think on how to do it. Thank you very much for your response. – narcester Jan 06 '21 at 00:00
  • The challenge is understandings how recursing "Winds In" and then "Winds Out" when it begins to return. Meaning, you wind-in `call1 -> call2 -> call3 -> ... -> call_last` and then when the exit condition is reached, you wind-out `call_last-> ... -> call3 -> call2 -> call1`. Program control picks up in the previous call at the point of the recursive call. Your logic will be complicated by the fact that you have 4 possible points within each call where the recursive call can occur -- and where control can pack up on the unwind. You have to track that as well. – David C. Rankin Jan 06 '21 at 00:33
  • See additional discussion at [Writing a recursive function that takes an array...](https://stackoverflow.com/a/64604029/3422102) and I have one more better link if I can find it. – David C. Rankin Jan 06 '21 at 00:36
  • Similar post touching on multiple paths in recursion [Recursion flow in c language...](https://stackoverflow.com/a/54362604/3422102) – David C. Rankin Jan 06 '21 at 00:51
  • @narcester It matters what type of variable `A` is, and what values it is initialized with. For one thing, the recursion as posted has no clear stopping condition so it could never end, depending on the initial values. This is why you need to post enough code for others to understand what happens and where it may fail. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – dxiv Jan 06 '21 at 05:02

0 Answers0