Thread: A recursion array scanner

  1. #1
    Registered User
    Join Date
    Dec 2010

    Question A recursion array scanner

    I am trying to write a recursive function
    void check_array(int array1[][M],int row,int col,int array1[row][col]);
    (it might have to be an int function in the end)

    The object of this function is check what number is in array1[row][col] and to printf how many similar neighbors it has.
    for example:
    4 3 2 3 1
    5 3 3 2 2 
    1 1 3 3 2 
    1 3 3 2 4
    5 5 3 4 4
    if function is called with 2,2 then the number is 3 and it has 9 connected 3's
    if function is called with 4,4 then the number is 4 and it has 3 connected 4's (the extra 4 in the array is not adjacent)

    Now the catch is I need to do it recursively. Do you have any advice on how to approach the problem? I could use a helper sub-function if need be, I jsut have no idea how to approach this.

    Thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    How would you, yourself, personally, do it if you had a pencil?

  3. #3
    Registered User
    Join Date
    Sep 2006
    After getting the start sqr's value, you need to recursively get all 8 (or fewer) adjacent sqr's values.

    for every value == to your start value, you need to follow them and count up all their adjacent sqr's with equal value.

    Sounds very much like recursive flood fill, except instead of filling with a color, you're just counting the sqr's that are equal in value.

    A part of this page, deals with that:
    Flood fill - Wikipedia, the free encyclopedia

  4. #4
    Registered User
    Join Date
    Dec 2010
    Thank you for that reference, adak. It is very helpful. In my specific function I'll jsut need to move in 8 directions rather than 4.. but how do I count the instances with a void funcion in which I cant return a number (and I cant use byref)is there a way I can "chain" the functions to printf("Total neighbors are %d",something); or is there no escape but to make the function return an int?
    Last edited by steelsoul; 12-12-2010 at 04:25 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Have you considered using a simple global int variable as a counter?

    I could be wrong on this, but I think you'll need a global array also, to keep track of situations where the sqr's equal to the start value, curve back around to a previous start value sqr, that was already counted.

    For this, I believe you'll need something like an array of all zeroes. Then with every sqr you find equal in your search, you give this second array (which will also be global), a value of 1 (any value you want other than zero).

    Then before counting that sqr you find that's equal, you count it only after it's corresponding sqr in this boolean array, is false (zero).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Replies: 7
    Last Post: 11-25-2008, 01:50 AM
  3. creating combinations of array values - recursion??
    By johndirect in forum C Programming
    Replies: 2
    Last Post: 11-20-2008, 12:49 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM