Demonstrates the use of recursion . Implemetn a blobsize ( 5 by 5) calculator pr
ID: 3608584 • Letter: D
Question
Demonstrates the use of recursion . Implemetn a blobsize ( 5 by 5) calculator program. Here is a sample program dialog for the gird: Enter coordinates for each filled cell; e.g.2 3 Don't enter commas or parentheses. Enter -1 -1 to finish entering cells. input: 1 1 1 2 22 2 3 3 3 3 5 4 1 4 5 5 2 5 4 5 5 -1 -1 output: Grid: X X _ _ _ _ X X _ _ _ _ X _ X X _ _ _ X _ X _ X X Blob sizes: 5 5 0 0 0 0 5 5 0 0 0 0 5 0 4 2 0 0 0 4 0 2 0 4 4 Algorithm to calculate the size of a blob anchored at a givencell 1. Copy the grid. 2. Make a recursive call, using the copy of the grid you madein step 1: If the cell (x, y) is not in the array then return0 else if the cell(x, y) is empty, then return0 else Mark the cell(x, y)as empty return 1 + the count of(x, y)'s 8 neighbor cells (Make 8 recursive calls, one for each neighbor) Demonstrates the use of recursion . Implemetn a blobsize ( 5 by 5) calculator program. Here is a sample program dialog for the gird: Enter coordinates for each filled cell; e.g.2 3 Don't enter commas or parentheses. Enter -1 -1 to finish entering cells. input: 1 1 1 2 22 2 3 3 3 3 5 4 1 4 5 5 2 5 4 5 5 -1 -1 output: Grid: X X _ _ _ _ X X _ _ _ _ X _ X X _ _ _ X _ X _ X X Blob sizes: 5 5 0 0 0 0 5 5 0 0 0 0 5 0 4 2 0 0 0 4 0 2 0 4 4 Algorithm to calculate the size of a blob anchored at a givencell 1. Copy the grid. 2. Make a recursive call, using the copy of the grid you madein step 1: If the cell (x, y) is not in the array then return0 else if the cell(x, y) is empty, then return0 else Mark the cell(x, y)as empty return 1 + the count of(x, y)'s 8 neighbor cells (Make 8 recursive calls, one for each neighbor)Explanation / Answer
#include<iostream> using namespace std; void print_grid(int grid[5][5]) { int i,j;
for(i=0;i<5;i++) for(j=0,cout<<" ";j<5;j++) { if(grid[i][j]==1) cout<<"X"; else cout<<"_"; } } int rec_find_grid(int x,int y,int grid[5][5]) { if(x<0 || x>=5 || y<0 ||y>=5) return 0; else if(grid[x][y] ==0) return 0; else { grid[x][y]=0; return (1+ rec_find_grid(x-1,y-1,grid) + rec_find_grid(x,y-1,grid) + rec_find_grid(x+1,y-1,grid) + rec_find_grid(x-1,y,grid) + rec_find_grid(x+1,y,grid) + rec_find_grid(x-1,y+1,grid) + rec_find_grid(x,y+1,grid) + rec_find_grid(x+1,y+1,grid )); } } void size_grid(int grid[5][5]) { int i,j; int copy_grid[5][5]; for(i=0;i<5;i++) for(j=0,cout<<" ";j<5;j++) { memcpy(copy_grid,grid,sizeof(copy_grid)); cout<<rec_find_grid(i,j,copy_grid); } } int main() { int grid[5][5]; int x,y,i,j; for(i=0;i<5;i++) for(j=0;j<5;j++) grid[i][j]=0; cout<<"Don't enter commas or parentheses. Enter -1 -1to finish entering cells."; cout<<"input :"; do { cin>>x>>y; if(x==-1 && y == -1) break; else { grid[x-1][y-1]=1; } }while(x!=-1 && y!=-1); cout<<"Grid is : "; print_grid(grid); cout<<endl; cout<<"Size vector is: "; size_grid(grid); cout<<endl; system("pause");
}
/* Sample output Don't enter commas orparentheses. Enter -1 -1 to finish enteringcells.input :1 1 12 2 2 23 33 35 41 45 52 54 55 -1-1 Grid is :
XX___ _XX__ __X_X X___X _X_XX Size vector is :
55000 05500 00504 20004 02044 Press any key to continue . .. Don't enter commas orparentheses. Enter -1 -1 to finish enteringcells.input :1 1 12 2 2 23 33 35 41 45 52 54 55 -1-1 Grid is :
XX___ _XX__ __X_X X___X _X_XX Size vector is :
55000 05500 00504 20004 02044 Press any key to continue . ..
*/
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.