Hi All!
I am currently stuck on a c++ program I'm working on.
So the program will have the user input data into a 2-d array.
sample dialogue:
BLOB SIZE CALCULATOR-
Enter coordinates for each filled cell; e.g 2 3
Do not enter commas or parentheses.
Enter -1 -1 to finish entering cells.
So for user input :
1 1
1 2
2 2
2 3
3 3
3 5
4 1
4 5
5 2
5 5
-1 -1
the output will be:
Grid:
XX___
_XX__
__X_X
X___X
_X_XX
this is easy. the real issue is when I need to output:
55000
05500
00504
20004
02044
The Algorithm needs to calculate all the Blob sizes, and return another grid like the one above.
Algorithm to calculate the size of a blob anchored at a given cell
1. copy the grid (b/c you don't want to use the original)
2. Make a recursive call, using the copy of the grid you made in step 1:
if the cell(x,y) is not in the array then
return 0
else if the cell (x,y) is empty, then
return 0
else
mark the cell (x,y) as empty
return 1+ the count of (x,y)'s 8 neighboring cells
(make 8 recursive calls, one for each neighbor)
so all I can output is the first spot any given blob as the highest number blob- example 0,0 blob will be 5, but the rest won't be 5, since it got turned into 0!!
PLEASE HELP!
#include <iostream>
using namespace std;
void welcomeMessege();
void userInput(int[][5]);
void outputOriginal(const int[][5]);
void returnBlobSize(const int anArray[][5]);
int recursive(int copyArray[][5], int x, int y);
int main()
{
int originalGrid[5][5]= {0};
welcomeMessege();
userInput(originalGrid);
outputOriginal(originalGrid);
returnBlobSize(originalGrid);
return 0;
}
void welcomeMessege()
{
cout << "BLOB SIZE CALCULATOR\n"
<< "Enter coordinates for each filled cell; e.g 2 3\n"
<< "Do not enter commas or parentheses.\n"
<< "Enter -1 -1 to finish entering cells.\n";
}
void userInput(int anArray[][5])
{
int i = 0, j = 0;
while(i != -1 || j !=-1)
{
cin >> i >> j;
anArray[i-1][j-1] = 1;
}
}
void outputOriginal(const int anArray[][5])
{
cout << "\nOUTPUT OF PROGRAM:\n\n"
<< "Grid:\n";
int i, j;
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
if(anArray[i][j] == 1)
{
cout << "X";
else
{
cout << "_";
if(j == 4)
{
cout << endl;
}
}
}
}
void returnBlobSize(const int anArray[][5])
{
int copyArray[5][5] = {0};
int i=0, j=0, x=0, y=0;
for(i=0; i<5; i++)
{
for(j=0; j<5; j++)
{
copyArray[i][j] = anArray[i][j]; //array is copied
}
}
for(x=0; x<5; x++) //for every square do:
{
for(y=0; y<5; y++)
{
copyArray[x][y] = recursive(copyArray, x, y);
}
}
cout << endl; //print array
for(x=0; x<5; x++)
{
for(y=0; y<5; y++)
{
cout << copyArray[x][y];
if(y==4)
cout << endl;
}
}
}
int recursive(int copyArray[][5], int x, int y)
{
if(x<0 || x>4 || y<0 || y>4)
{
return 0;
}
else if(copyArray[x][y] == 0)
{
return 0;
}
else
{
copyArray[x][y] = 0;
return 1 + recursive(copyArray, x-1, y-1)
+ recursive(copyArray, x-1, y)
+ recursive(copyArray, x-1, y+1)
+ recursive(copyArray, x, y-1)
+ recursive(copyArray, x, y+1)
+ recursive(copyArray, x+1, y-1)
+ recursive(copyArray, x+1, y)
+ recursive(copyArray, x+1, y+1);
}
}
by the way, the PGM should calculate the sizes of all blobs in the grid. a blob is 1 square with a value of 1 in it, or more touching one another staight up and down or diagnoly. after the pgm goes 1 by 1 to look for value of 1, it will discover 1 eventually and make 8 recursive calls in the squares around it. the problem I have isw that it calculates the blobs, but erases the data for the rest of the current blob- so a blob of 5 will only have 5 in the final output on its FIRST square, and the rest of the squares are now 0's because they were turned into 0's by the algorithm. I need to have them turn back into 1's, whereever they were 1's so all the 5 squares that creat a blob will become 5 as well.