Thread: Printing an 4x4 grid and storing it in an array?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    22

    Printing an 4x4 grid and storing it in an array?

    Hi guys, I'm new here and I'm fairly new to C.

    This maybe simple to most of you guys but I'm extremely frustrated because I don't know what to do with it at all.

    The problem is, I want to print out a pattern of this type:

    Code:
    +---+
    |   |
    |   |
    +---+
    and then storing this in an array so that when a user types in 4,3 it should point to board[4][3]. The grid size is supposed to be 8x8 but in the interest of simplicity, I'm going to start with a 4x4 grid.

    The problem is, I don't know how to print out that 4x4 grid with that pattern and I don't know how to store it in the array. Can someone please point me in the right direction?

    If you want to see some coding, all I have for now is this:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    char board[4][4];
    
    int main() {
    
        for (int i = 0; i < 4; i++) {
            for (j = 0; j < 4; j++) {
                board[i][j] = 
            }
        }
    I'm really stuck as to what to input for board[i][j] and I'm unsure if that would even solve that problem...

    Thanks in advance!

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Personally, I would handle the first and last rows separately:
    Code:
    for(i = 0;i < width;++i)
      board[0][i] = (i == 0 || i == width - 1) ? '+' : '-';
    for(i = 1;i < height - 1;++i)
      for(j = 0;j < width;++j)
        board[i][j] = j == 0 || j == width - 1) ? '|' : ' ';
    for(i = 0;i < width;++i)
      board[height - 1][i] = (i == 0 || i == width - 1) ? '+' : '-';
    Something like that should work anyway. I didn't compile or test it.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    22
    Hmm I don't really understand that...

    Also what are the ? and : for?

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Okay, how about this then?
    Code:
    for(i = 0;i < height;++i)
    {
      if(i == 0 || i == height - 1)
      {
        // Print the first / last rows
      }
      else
      {
        // Print the other rows
      }
    }
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    22
    So by first/ last rows you mean

    Code:
    +---+
    
    
    +---+
    that?

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Yup.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    22
    Okay but I don't get how I would store it in the array.

    I mean, I get that if row is 0 or 3 in this case, it would output the first and last rows and the else statement is for the middle. But how would I go about storing that one "Box" in the board array, say for example board[1][1]?

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You'd determine what character goes in that position based on the values of i and j. if the current column is 0 or width - 1, then the character is '+' if the current row is 0 or height - 1, otherwise it's a '|', etc.

    Code:
    for each i in rows
    {
      if i equals 0 or i equals height - 1
      {
        for each j in columns
        {
          if j equals 0 or j equals width - 1
            board[i][j] = '+'
          else
            board[i][j] = '-'
        }
      }
      else
      {
        for each j in columns
        {
          if j equals 0 or j equals width - 1
            board[i][j] = '|'
          else
            board[i][j] = ' '
        }
      }
    }
    If you understand what you're doing, you're not learning anything.

  9. #9
    Novice
    Join Date
    Jul 2009
    Posts
    568
    First, I would declare an array of appropriate size, accounting for edges. Like so.
    Code:
    #define GRIDH (8 + 2)
    #define GRIDW (8 + 2)
    
    char grid[GRIDH][GRIDW];
    Then I would initialize the array, using a function like this.
    Code:
    #define CN '+'
    #define HL '-'
    #define VL '|'
    #define BL ' '
    
    void init_grid(int h, int w, char new_grid[GRIDH][GRIDW])
    {
      int i, t;
      for (i = 0; i < h; ++i)
      {
        for (t = 0; t < w; ++t)
        {
          if ((i == 0 || i == (h - 1)) && (t == 0 || t == (w - 1))) /* corners */
            new_grid[i][t] = CN;
          else if (i == 0 || i == (h - 1)) /* top and bottom edges */
            new_grid[i][t] = HL;
          else if (t == 0 || t == (w - 1)) /* side edges */
            new_grid[i][t] = VL;
          else                             /* we're in the clear */
            new_grid[i][t] = BL;
        }
      }
    }
    Then I would use this initialized grid to place a cursor based on user input, which would probably be defined as a structure holding XY coordinates. Before placing the cursor on the board, I would check if it is within bounds of the board.

    It's up to you to implement this in a complete program.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    22
    Quote Originally Posted by itsme86 View Post
    You'd determine what character goes in that position based on the values of i and j. if the current column is 0 or width - 1, then the character is '+' if the current row is 0 or height - 1, otherwise it's a '|', etc.

    Code:
    for each i in rows
    {
      if i equals 0 or i equals height - 1
      {
        for each j in columns
        {
          if j equals 0 or j equals width - 1
            board[i][j] = '+'
          else
            board[i][j] = '-'
        }
      }
      else
      {
        for each j in columns
        {
          if j equals 0 or j equals width - 1
            board[i][j] = '|'
          else
            board[i][j] = ' '
        }
      }
    }
    I tried that but it doesn't seem to work. Maybe I'm using a wrong printf conversion?

    If I wanted to test that out, would I use

    printf("%s", board);

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You need to either make it into a string, or you need to print each character one at a time.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying content of file into a 2D array
    By trueman1991 in forum C Programming
    Replies: 10
    Last Post: 12-16-2009, 12:42 AM