#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0

    How to you create and print a table in C?


    I've been learning C and one of the things that I have struggled with is creating and printing tables/grids/boards. I have an idea that it will require 2d arrays, linked lists and a few functions but am unsure as to what the simplest way to do this is. From what I think I understand so far:

    //your basic libraries
    #include <stdio.h>
    #include <stdlib.h>
    #define ROWS 10
    #define COLS 10

    void board();
    void printboard();

    //
    int main(int argc, char *argv[])
    {

    return
    }

    //
    void board()
    {
    int board[ROWS][COLS]
    int i, j;
    for(i = 0; i < ROWS; i++)
    for(j = 0; j < COLS; j++)

    }

    //unsure how to do this
    void printboard()
    {
    printf("");
    }

    That's what I understand so far. Could you correct my mistakes and explain how to do this as simply as possible. Much appreciated!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    I'm not sure of what you really want to do. Do you want to fill the board with a random number? A word? Ask user for a number for each case?

    Anyway to print the board you use the exact same method as to fill it. For example:

    Code:
    for (i=0; i<ROWS; i++)
       {
       for (j=0; j<COLS; j++)
          printf("%d ", board[i][j]);
    
       printf("\n");
       }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by Toodd
    I'm not sure of what you really want to do. Do you want to fill the board with a random number? A word? Ask user for a number for each case?

    Anyway to print the board you use the exact same method as to fill it. For example:

    for (i=0; i<ROWS; i++)
    {
    for (j=0; j<COLS; j++)
    printf("%d ", board[i][j]);

    printf("\n");
    }
    Sorry, should have been clearer. I wanted to know how to

    a)print a board with letters

    b)print a board with numbers

    c)print a board with some empty spaces and some with 'O' letters. As if I was creating a checkers board

    What would be the simplest way.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    Then you have to fill it square by square.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define ROWS 3   to be easier to test the program
    #define COLS 3
    
    char board[ROWS][COLS][100];  declaring them as globals so you can use them in all your functions
    int i, j;
    
    void fill_board();
    void printboard();
    
    int main()
       {
          fill_board();
          printboard();
    
          return 1;  return value 1 so you know the program succed (not very important)
       }
    
    
    void fill_board()
       {
       for (i=0; i<ROWS; i++)
          {
          for (j=0; j<COLS; j++)
             {
             printf("\nIndicate content of row %d and column %d: ", i+1, j+1);
             scanf("%s",board[i][j]);       this will read your anwser and store it in the array
             getchar();  Just to clean the \n
             }
          }
       }
    
    void printboard()
    {
    printf("\n\n");
    
    for (i=0; i<ROWS; i++)
       {
       for (j=0; j<COLS; j++)
          printf("%s ", board[i][j]);
    
       printf("\n\n");
       }
    }
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by Toodd
    Then you have to fill it square by square.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define ROWS 3   to be easier to test the program
    #define COLS 3
    
    char board[ROWS][COLS][100];  declaring them as globals so you can use them in all your functions
    int i, j;
    
    void fill_board();
    void printboard();
    
    int main()
       {
          fill_board();
          printboard();
    
          return 1;  return value 1 so you know the program succed (not very important)
       }
    
    
    void fill_board()
       {
       for (i=0; i<ROWS; i++)
          {
          for (j=0; j<COLS; j++)
             {
             printf("\nIndicate content of row %d and column %d: ", i+1, j+1);
             scanf("%s",board[i][j]);       this will read your anwser and store it in the array
             getchar();  Just to clean the \n
             }
          }
       }
    
    void printboard()
    {
    printf("\n\n");
    
    for (i=0; i<ROWS; i++)
       {
       for (j=0; j<COLS; j++)
          printf("%s ", board[i][j]);
    
       printf("\n\n");
       }
    }
    Cheers! That seems a lot easier than I thought it would be. I has an assignment a few weeks ago where I had to create a board[5][5] for the game peg solitaire(similar to checkers where a piece can only move by jumping another). The starting board was supposed to look like

    . . O . .
    . . O . .
    OOOOO
    . . O . .
    . . O . .

    and then I had to use linked list to write a code to show all the possible moves the user could make before the game ends. The O's represent pegs and the . are empty board places. Wasn't confident with linked lists at all so didn't get it done. Any idea on how I should have done it (in the simplest way to understand)? If not, then no worries as the deadline for that has gone and thanks for the above on creating/printing boards.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2013
    Posts
    24
    Rep Power
    0
    Sorry, first time I heard about linked list. I have no ideia how it works. But I'm sure if you do some research on google you could find some useful tutorials. Or you could even ask someone in this forum.

    Anyway good luck.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    9
    Rep Power
    0
    Originally Posted by Toodd
    Sorry, first time I heard about linked list. I have no ideia how it works. But I'm sure if you do some research on google you could find some useful tutorials. Or you could even ask someone in this forum.

    Anyway good luck.
    No problem, Thanks anyway!

IMN logo majestic logo threadwatch logo seochat tools logo