I am using an algorithm to find the biggest square from a text file given as input to my program. I have tested; my program is receiving the correct string of characters with newlines after each row, and it is receiving the correct character that marks a "full" spot. It is receiving the correct column and row count.
My function, displayed here, is supposed to make a two dimensional integer array that fills in a minesweeper-like algorithm. The goal being to be able to select any spot in the array and know how many "full" spots are in the area above and to the left of it.
Here is my function:
int **makearray(char *str, char full, int rowcount, int colcount){
int i;
int j;
int **intarray;
int *column;
int spot;
intarray = (int **) malloc(sizeof(int *) * rowcount);
i = 0;
while (i < rowcount) {
j = 0;
column = (int *) malloc(sizeof(int) * (colcount));
intarray[i] = column;
while (j < (colcount + 1)) {
spot = i + (j * (colcount + 1));
intarray[i][j] = 0;
if (str[spot] == full)
intarray[i][j] = 1;
if (i == 0 && (j != 0))
intarray[i][j] += intarray[i][j - 1];
else if (i != 0 && (j != 0))
intarray[i][j] += (intarray[i][j - 1] + intarray[i - 1][j]
- intarray[i - 1][j - 1]);
else if (i != 0 && (j == 0))
intarray[i][j] += intarray[i - 1][j];
j++;
}
i++;
}
return (intarray);
}
Here is the input that I give it:
.o...o....................o.o..............o....o..
...o..o..................o.........o..o....o.......
...o...........o...................o........o......
.........................oo...........oo.......o...
..o...........................o.......o............
o....o.....o....o....o.........o.........o.........
....................................o.....oo.o.....
.....o..o..........o..........o....................
..............o.o.......o..o......o................
.................o..o...........o...o..............
o.................oo............oo.................
...o...oo....................oo..o......o....o....o
...o......o.........o..o.....................oo....
.........oo......o............................o....
......o....o................o.....................o
......................................o..oo.......o
....................o..o........o.....o.o..........
...o.o..o..o.......o.....................o......o..
...o..........o.......o................o........o..
...........o......................o.oo....o....o.o.
.o......o...o......................o.......o.......
........................o......oo............oo....
..o.....o..........................................
This should turn it into an array that has 116 in the last position; however, when I test it, the last spot is holding a 48. The logic was obtained from this stack-overflow post: Most efficient algorithm to find the biggest square in a two dimension map
Could you help me find the bug?