realhigh69
realhigh69

Reputation: 19

How to assign value in 2 Dimensional array

Creating a tile pattern

char t1[11][11];
int i=0;
int j=0;

int size = 10;
//Putting "|" for tile1 (0,0) to (11,0)
for(i=0;i<11;i++)
{
    while(j=0)
    {
        t1[i][j] = '|';
    }
}

//Putting "-" for tile1 (0,0) to (0,11)
for(j=1;j<10;j++)
{
    while(i=0)
    {
        t1[i][j] = '-';
    }
}

//PRINTING  "|" for tile1 (0,0) to (11,0)
for(i=0;i<11;i++){
    while(j=0){
        printf("%c \n",t1[i][j]);
    }
    printf("\n");
}

//PRINTING "-" for tile1 (0,0) to (0,11)
for(j=1;j<10;j++)
{
    while(i=0)
    {
        printf("%c "), t1[i][j];
    }
}

So basically I want the output to be like the image given. I declared an array with 11,11 size and I am trying to use loops to assign characters in the array. But for some reason it is not working.

I know I have to use loops so I drew the pattern on a paper and write down the array location and tried to assign using loops.

Upvotes: 1

Views: 9953

Answers (3)

if2
if2

Reputation: 1

With a bigger 2D array you can make the code shorter:

#define VSIZE 13
#define HSIZE 25

char t1[VSIZE][HSIZE];

for(int i = 0; i < VSIZE ; ++i) {
    for(int j = 1; j < HSIZE - 2; ++j) {
        // default value for odd columns:
        char c = ' ';
        // but change it to something else in an even column:
        if(!(j%2)) {
            if(i == 0 || i == VSIZE - 1)
                c = '-';
            if(i == 1 || i == VSIZE - 2 || 2 * i == j)
                c = '@';
        }
        t1[i][j] = c;
    }
    t1[i][0] = t1[i][HSIZE - 2] = '|';
    t1[i][HSIZE - 1] = '\0';   // turn the row to a string closing it with '\0'

    printf("%s\n", t1[i]);
}

Upvotes: 0

Ahmad Khan
Ahmad Khan

Reputation: 2693

You have a number of errors in your program. Let me list down some:

  • j = 0 assigns 0 to j, and j == 0 compares if j is equal to 0.

  • You haven't updated the i or j in inner while loops.

  • If you want to stick to the exact pattern shown in the picture, you have to eliminate \n from printf("%c \n", t1[i][j]);.

  • printf("%c "), t1[i][j]; doesn't print t1[i][j]. But printf("%c ", t1[i][j]); does.

  • You're not assigning @ character in the array anywhere.

Let's re-write your code:

Initialize the array:

char t1[11][11];

Now, in a loop, assign each element in this 11 x 11 array a character according to the pattern.

for (int i = 0; i < 11; i++){
    for (int j = 0; j < 11; j++){

        // If its first or last column, then place a | at t1[i][j]
        if (j == 0 || j == 10){
            t1[i][j] = '|';
        }
        // Else If its the first or last row, then place a - at t1[i][j]
        else if (i == 0 || i == 10){
            t1[i][j] = '-';
        }
        // Else if its the second or second to last row/column
        // then place @ at t1[i][j]
        else if (i == 1 || i == 9 || j == 1 || j == 9){
            t1[i][j] = '@';
        }
        // Else if its the diagonal, then place a @ at t1[i][j]
        else if (i == j){
            t1[i][j] = '@';
        }
        // All remaining places would have a space.
        else{
            t1[i][j] = ' ';
        }
    }
}

After this, every element in the array would have been initialized to some appropriate value. Now, print the whole array.

for (int i = 0; i < 11; i++){
    for (int j = 0; j < 11; j++){
        printf("%c\t", t1[i][j]);
    }
    printf("\n");
}

See this code running live here.

Upvotes: 3

Cl&#233;ment P&#233;au
Cl&#233;ment P&#233;au

Reputation: 323

When you are using

printf("%c \n", t1[i][j]);

You are writing a character from the array and appending it with a newline which will break your pattern.
Try changing your printing loops for this one

 for (j = 0; j <= 10; ++j)  
 {  
   printf("%s\n" ,t1[j]);  
 }

As for writting the '@' characters I would do a loop like that

for (i = 1; i < 10; ++i)  
{  
  for (j = 1; j < 10; ++j)    
  {  
    if ((j == 1 || i == 1 || j == 9 || i == 9) || j == i)
      t1[i][j] = '@';
  }
}

Upvotes: 1

Related Questions