0

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");

        }

}
Tam Lam
  • 135
  • 1
  • 3
  • 13

2 Answers2

1

I think this might have to do with how the data in the array is stored. In memory, the data is just stored sequentially. Since negative array indexes are allowed in C the functional outcome in your case is that forest[1][-1] points to the same data located in cell forest[0][9], which you will note is 'T'.

Because forest[0][9] (aka forest[1][-1]) is 'T', you fail this test

if(forest[i][j-1]!='T' && forest[i-1][j]!= 'T')

Which means that a ' ' is registered in cell forest[1][0] instead. Prevent negative index values, and you should be good.

Community
  • 1
  • 1
James Shewey
  • 260
  • 2
  • 19
0

You may be over-thinking the logic you are using to fill the forest array in plant_forest. All you need to stagger the 'T' and ' ' elements used to fill forest is a check on whether your outer loop index 'i' is odd or even and start filling with either a 'T' or ' ' based on the result. Then just toggle between 'T' and ' ' for each 'j' index. For example:

int plant_forest (char (*forest)[SIZE])
{
    if (!forest) return 1;              /* validate pointer */
    int i, j;

    for (i = 0; i < SIZE; i++) {
        int t = i & 1;
        for (j = 0; j < SIZE; j++)   /* simple toggle 'T' or ' ' */
            forest[i][j] = t ? 'T' : ' ', t ^= 1;
    }
    return 0;
}

You can test the results with a simple bit of code:

#include <stdio.h>

#define SIZE 10

int plant_forest (char (*forest)[SIZE])
{
    if (!forest) return 1;              /* validate pointer */
    int i, j;

    for (i = 0; i < SIZE; i++) {
        int t = i & 1;
        for (j = 0; j < SIZE; j++)   /* simple toggle 'T' or ' ' */
            forest[i][j] = t ? 'T' : ' ', t ^= 1;
    }
    return 0;
}

int main (void) {

    char arr[SIZE][SIZE] = {{0}};

    if (!plant_forest (arr)) {
        int i, j;
        for (i = 0; i < SIZE; i++) {
            for (j = 0; j < SIZE; j++)
                putchar (arr[i][j]);
            putchar ('\n');
        }
    }
    return 0;
}

Example Use/Output

$ ./bin/plant
 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

If you have any questions, or if I misunderstood your question, just let me know. (I'll leave it to you to fill in all the additional '|' formatting your may like).

You can even save a line of code as mentioned in the comments by anding the sum of i and j, in plant_forest to choose between assigning 'T' or ' ' e.g.

    for (i = 0; i < SIZE; i++)
        for (j = 0; j < SIZE; j++)   /* simple toggle 'T' or ' ' */
            forest[i][j] = (i + j) & 1 ? 'T' : ' ';

Either way will insure your forest array is populated correctly.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85