How to print 2 tables on top of each other in C language?

Viewed 232

I'm learning C in university and we got our first project to create the scrabble game.

I'm having trouble with the tables, as I'm trying to print a table on top of another one to put multiple/different characters in 1 "cell". I know this isn't possible with a regular table but I could create 2 different tables and just print them on top of each other, so it looks like there are 2 different characters in one cell.

This is my first table:

first table

and this is what I want it to look like:

desired appearance

I'm trying to make it look like the second one with the number 1 to 15 on the left and letters in the same space as the special characters &, %, ...

The code I'm using to print the first table is:

//Affichage du tableau
    for (i=0; i<16; i++)
    {
        for(j=0; j<16; j++)
        {
            printf(" %c ", tab[i][j]);
        }
        printf("\n");
    }
1 Answers

A simple approach would be something like this:

  • Assuming 2 char tables with '\0' as the char when you do not want to print anything.

    #define MAX 3

  char tab1[MAX][MAX] = {{'a', 'b', 0}, {'&', 0, '#'}, {0, '8', 'V'}};
  char tab2[MAX][MAX] = {{'&', 0, '#'}, {0, '8', 'V'}, {'a', 'b', 0}};
  // or any table you have  

  for (int i = 0; i < MAX; i++)
  {
    printf("%2d|", i+1); // if you want the line number
    for (int j = 0; j < MAX; j++)
    {
      if (tab1[i][j] != '\0') 
        printf("%c", tab1[i][j]);
      else
        printf(" ");
      
      if (tab2[i][j] != '\0')
        printf("%c", tab2[i][j]);
      else
        printf(" ");
    }
    printf("\n");
  }

And you can easily  adjust the spacing in the print statements.
This will print:


 1|a&b  #
 2|&  8#V
 3| a8bV 
Related