#### Archived

This topic is now archived and is closed to further replies.

# Help with Recursion

This topic is 6297 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I need to implement a recursive function to compute the number of 0 that are next to another 0. A data file is read into an array, and looks something like this: 111111111111111 100110011010001 110010000011101 111011110001101 111000111011111 111001100000111 111111111111111 This is the section where i''m think the recursion need to go? void AreaCal() { if ((Grid[Row][Col] == ''1'') || (Grid[Row][Col] == ''²'')) Area = 0; else { Grid[Row][Col] = ''²''; Area ++; } } This section takes the coordinates entered in by user and check to see if the coordinates contains a 0 or 1. If it contains a 0 then it replace the 0 with ASCII value 178. I have an ideal on how to implement the recursion, just not sure how to code it out to make it work. Any suggestion?

##### Share on other sites
Try something similar to this:
  int AreaCal(int row, int col){ // make sure we are in bounds of array if(row >= MAX_ROW || row < 0) return 0; if(col >= MAX_COL || col < 0) return 0; if(grid[row][col] == ''1'' || grid[row][col] == ''2'') return 0; // else, we have a ''0'' grid[row][col] = ''2''; // mark off the spot as counted // return this space (1) plus the number of 0''s in all // directions return 1 + AreaCal(row-1, col) + AreaCal(row+1, col) + AreaCal(row, col-1) + AreaCal(row, col+1);}

This should hopefully, work, but might have a bug or two. You need to remember with recursion to always have a terminating case (in this situation, it is if you are out of bounds of the array or on a non-''0'' space).

Be sure to let me know what grade I get on this assignment.

##### Share on other sites
that looks too much like a homework problem to me.
b/c i remember doing something similar when i was in my first semester of c++

"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T

##### Share on other sites
quote:
Original post by jaxson

Try something similar to this:

  int AreaCal(int row, int col){ // make sure we are in bounds of array if(row >= MAX_ROW || row < 0) return 0; if(col >= MAX_COL || col < 0) return 0; if(grid[row][col] == ''1'' || grid[row][col] == ''2'') return 0; // else, we have a ''0'' grid[row][col] = ''2''; // mark off the spot as counted // return this space (1) plus the number of 0''s in all // directions return 1 + AreaCal(row-1, col) + AreaCal(row+1, col) + AreaCal(row, col-1) + AreaCal(row, col+1);}

This should hopefully, work, but might have a bug or two. You need to remember with recursion to always have a terminating case (in this situation, it is if you are out of bounds of the array or on a non-''0'' space).

Be sure to let me know what grade I get on this assignment.

Thanks Jaxson for you input, got me through coder''s block. lol

  unsigned AreaCal(unsigned X,unsigned Y){ if ((Grid[X][Y] == ''1'') || (Grid[X][Y] == ''²'')) Area = 0; else { Grid[X][Y] = ''²''; Area ++; return AreaCal(X-1,Y)+AreaCal(X+1,Y)+ AreaCal(X,Y-1)+AreaCal(X,Y+1); }}

Now I just have to debug one little bug. Thanks for your help

##### Share on other sites
quote:
Original post by jaxson

Try something similar to this:

  int AreaCal(int row, int col){ // make sure we are in bounds of array if(row >= MAX_ROW || row < 0) return 0; if(col >= MAX_COL || col < 0) return 0; if(grid[row][col] == ''1'' || grid[row][col] == ''2'') return 0; // else, we have a ''0'' grid[row][col] = ''2''; // mark off the spot as counted // return this space (1) plus the number of 0''s in all // directions return 1 + AreaCal(row-1, col) + AreaCal(row+1, col) + AreaCal(row, col-1) + AreaCal(row, col+1);}

This should hopefully, work, but might have a bug or two. You need to remember with recursion to always have a terminating case (in this situation, it is if you are out of bounds of the array or on a non-''0'' space).

Be sure to let me know what grade I get on this assignment.

Thanks Jaxson for you input, got me through coder''s block. lol

  unsigned AreaCal(unsigned X,unsigned Y){ if ((Grid[X][Y] == ''1'') || (Grid[X][Y] == ''²'')) Area = 0; else { Grid[X][Y] = ''²''; Area ++; } return AreaCal(X-1,Y)+AreaCal(X+1,Y)+ AreaCal(X,Y-1)+AreaCal(X,Y+1);}

Now I just have to debug one little bug. Thanks for your help

##### Share on other sites
Sorry for the double posting.. just messed up on writing the first msg.

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002286
×