I am trying to create a (10x10) 2 dimensional character array to store either ' ' and 'T' and display it like this
+-+-+-+-+-+-+-+-+-+-+
| |T| |T| |T| |T| |T|
+-+-+-+-+-+-+-+-+-+-+
|T| |T| |T| |T| |T| |
+-+-+-+-+-+-+-+-+-+-+
| |T| |T| |T| |T| |T|
+-+-+-+-+-+-+-+-+-+-+
|T| |T| |T| |T| |T| |
+-+-+-+-+-+-+-+-+-+-+
| |T| |T| |T| |T| |T|
+-+-+-+-+-+-+-+-+-+-+
|T| |T| |T| |T| |T| |
+-+-+-+-+-+-+-+-+-+-+
| |T| |T| |T| |T| |T|
+-+-+-+-+-+-+-+-+-+-+
|T| |T| |T| |T| |T| |
+-+-+-+-+-+-+-+-+-+-+
| |T| |T| |T| |T| |T|
+-+-+-+-+-+-+-+-+-+-+
|T| |T| |T| |T| |T| |
+-+-+-+-+-+-+-+-+-+-+
The function I wrote:
int plant_forest(char forest[][SIZE])
{
int i,j;
forest[0][0] = ' ';
for(i = 0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
if(forest[i][j]!= forest[0][0])
{
if(forest[i][j-1]!='T' && forest[i-1][j]!= 'T')
{
forest[i][j] = 'T';
}
else
{
forest[i][j] = ' ';
}
}
}
}
return 0;
}
The result I got was slightly different.
+-+-+-+-+-+-+-+-+-+-+
| |T| |T| |T| |T| |T|
+-+-+-+-+-+-+-+-+-+-+
| | |T| |T| |T| |T| |
+-+-+-+-+-+-+-+-+-+-+
|T| | |T| |T| |T| |T|
+-+-+-+-+-+-+-+-+-+-+
| |T| | |T| |T| |T| |
+-+-+-+-+-+-+-+-+-+-+
|T| |T| | |T| |T| |T|
+-+-+-+-+-+-+-+-+-+-+
| |T| |T| | |T| |T| |
+-+-+-+-+-+-+-+-+-+-+
|T| |T| |T| | |T| |T|
+-+-+-+-+-+-+-+-+-+-+
| |T| |T| |T| | |T| |
+-+-+-+-+-+-+-+-+-+-+
|T| |T| |T| |T| | |T|
+-+-+-+-+-+-+-+-+-+-+
| |T| |T| |T| |T| | |
+-+-+-+-+-+-+-+-+-+-+
I checked the logic and couldn't find anything wrong. Except from i-1 and j-1 could be negative number. But how would this effecting the execution ?
Just for reference, I will include the printing function here. But I have already checked and sure that there was no error in this function
void printBoard(char forest[][SIZE])
{
int i,j;
printf("+-+-+-+-+-+-+-+-+-+-+\n");
for(i = 0; i<SIZE;i++)
{
for(j = 0;j<SIZE;j++)
{
printf("|%c",forest[i][j]);
}
printf("|\n");
printf("+-+-+-+-+-+-+-+-+-+-+\n");
}
}