CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2002
    Posts
    96

    Lightbulb How to change recursive loop to non recursive loop

    I want to find area of contiguous 5 (5-connected area) in two dimension array like this

    00000000000000000
    00000055555500000
    00000055055000000
    00000000005550000
    00000000000000000


    Contiguous area of number 5 = 13
    I write FindAreaof5 function below and m_tempData(x,y) is data in this array (0 or 5)
    I call FindAreaof5 function by this loop

    code
    ...............................................................
    for (y=0; y< height; y++){
    for (x=0; x< width; x++){
    if (m_tempData(x,y) == 5){
    InterestArea = FindAreaof5(x,y);
    }
    }
    ...............................................................

    ////Below this is FindAreaof5 function//////

    code
    ...............................................................
    long FindAreaof5(int x, int y)
    {
    long count ;
    if (m_tempData(x,y)==0) return 0;
    m_tempData(x,y) = 0;

    count = 1 + FindAreaof5(x, y-1) + FindAreaof5(x+1, y-1)
    + FindAreaof5(x+1, y) + FindAreaof5(x+1, y+1)
    + FindAreaof5(x, y+1) +FindAreaof5(x-1, y+1)
    + FindAreaof5(x-1, y) + FindAreaof5(x-1, y-1);
    return count;
    ...............................................................

    In the above example I can find FindAreaof5 = 13
    If FindAreaof5 has small value ,it can find FindAreaof5.
    But if FindAreaof5 has large value (more than 12000) , it show message box and tell me there is Stack Overflow error.
    I want to know how to solve this problem. Someone suggest me to change recursive loop to non recursive or use blob coloring but I do not know how to do that. If you know how to solve my problem, please tell me. Thank you very much.

  2. #2
    Join Date
    May 2002
    Location
    Islamabad, Pakistan
    Posts
    5
    Hi

    Either I am missing something out of your problem, or you didn't describe your problem correctly.

    Atleast I can not understand the given example.

    I do have some algorithm to find the connected similar values in two dimensional space. This algorithm is intended for multiple values (i.e. clusters of multiple values), but may well work for single value also.

    Please elaborate your problem so that I try to find that piece of code from my untidy mess of computer

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured