Thread: 2D Array Board

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    7

    2D Array Board

    Hi all,

    I'm making a board using a 2D array in which there are 8 rows and 7 columns. The bottom four rows are filled with '1's and the top four are filled with '0's. I'm probably being a complete idiot but I can't get my compiler to print them off in rows and columns, it just prints them out in one long line. Here is said section of my code. Any help regarding this would be amazing. Thanks!

    Code:
    #include <stdio.h>
    
    #define ROW 8
    #define COL 7
    
    
    
    int main(void)
    
    {
    
       int board[ROW][COL];
       int i, j;
    
       for (i=0; i<ROW; i++) {
          for (j=0; j<COL; j++) {
             board[i][j] = 0;
          }
       }
    
       for (i=4; i<ROW; i++) {
          for (j=0; j<COL; j++) {
             board[i][j] = 1;
          }
       }
    
       for (i=0; i<ROW; i++) {
          for (j=0; j<COL; j++) {
             printf("%d", board[i][j]);
          }
       }
    
       printf("\n");
    
       return 0;
    
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You just have to print a newline character at the end of each row:
    Code:
       for (i=0; i<ROW; i++) {
          for (j=0; j<COL; j++) {
             printf("%d", board[i][j]);
          }
          putchar('\n');
      }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    for (i=0; i<ROW; i++) {
          for (j=0; j<COL; j++) {
             printf("%d", board[i][j]);
          }
       printf("\n"); /* <-- new and improved */
       }

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    7
    oh cool, thanks so much! I guess I was being an idiot! haha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Treating a 2D array as a 1D array....
    By John_L in forum C Programming
    Replies: 6
    Last Post: 10-18-2007, 02:38 PM
  2. 2D array of threads?
    By shorinhio in forum C Programming
    Replies: 2
    Last Post: 01-04-2007, 04:02 PM
  3. passing/pointing to a 2d array
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 10-28-2005, 10:16 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Pick a number....
    By Salem in forum A Brief History of Cprogramming.com
    Replies: 39
    Last Post: 01-19-2003, 07:27 AM