recursive function problem

This is a discussion on recursive function problem within the C Programming forums, part of the General Programming Boards category; this function supposed to open all empty spaces.. but it does not work properly.. Code: void openemptyspace(int x, int y) ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    32

    recursive function problem

    this function supposed to open all empty spaces..
    but it does not work properly..


    Code:
    void openemptyspace(int x, int y)
    {
        
    /* Declarations */
    	int i;
        int j;
    
    /* nested for loops and if statments*/
    	for(i = -1; i <= 1; i++)
    	{
    		for(j = -1; j <= 1; j++)
    		{
         		 if(showmines[x + i][y + j] == '-' )
    	              {
    					showmines[x + i][y + j] = mines[x + i][y + j];
     					if(showmines[x + i][y + j] == '0' )
                           {
    						openemptyspace(x + i, y + j);
                           }  /* end if */
    				    }     /* end outer if*/
    		}   /*end inner for loop */
    	}   /* end outer for loop */
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>but it does not work properly..
    and? What does it do or not do that makes you think its wrong?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    dat
    dat is offline
    Registered User
    Join Date
    Nov 2001
    Posts
    28

    Re: recursive function problem

    Originally posted by jk81
    this function supposed to open all empty spaces..
    but it does not work properly..


    Code:
    void openemptyspace(int x, int y)
    {
        
    /* Declarations */
    	int i;
        int j;
    
    /* nested for loops and if statments*/
    	for(i = -1; i <= 1; i++)
    	{
    		for(j = -1; j <= 1; j++)
    		{
         		 if(showmines[x + i][y + j] == '-' )
    	              {
    					showmines[x + i][y + j] = mines[x + i][y + j];
     					if(showmines[x + i][y + j] == '0' )
                           {
    						openemptyspace(x + i, y + j);
                           }  /* end if */
    				    }     /* end outer if*/
    		}   /*end inner for loop */
    	}   /* end outer for loop */
    }

    Usually in recursive functions the recursive part (the part where you call the function again) is not nested within a condition, and the "base case" is in an conditional statement. This style guarentees the the recursive call unless the base case is statisfied. With your style the:

    Code:
    if(showmines[x + i][y + j] == '0' )
                           {
    						openemptyspace(x + i, y + j);
                           }  /* end if */
    might not get called.


    cheers
    Dat
    http://hoteden.com <-- not an porn site damnit!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 09:28 AM
  2. Recursive function problem
    By trnd in forum C Programming
    Replies: 5
    Last Post: 01-30-2009, 12:36 AM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  5. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21