Like Tree1Likes
  • 1 Post By Adak

DESPERATELY NEED HELP!!!! PRINT A Box using arrays...

This is a discussion on DESPERATELY NEED HELP!!!! PRINT A Box using arrays... within the C Programming forums, part of the General Programming Boards category; can anyone teach me how to display the other half of the box's border? where should i put the printf? ...

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    59

    DESPERATELY NEED HELP!!!! PRINT A Box using arrays...

    can anyone teach me how to display the other half of the box's border? where should i put the printf? or is there any other method that you can show me using arrays?



    Code:
     #include <stdio.h>
    #define ROW 15
    #define COLUMN 15
    
    
    void disp_box (char b[ROW][COLUMN])
    {    
         int r,c;
    
       for(r=0; r<ROW-1;r++)
        
            printf("*%c",b[r][0]);
            
        
            for(c=0; c<COLUMN;c++)
            {
                printf("*%c",b[0][c]);
                printf("\n");
                  
            }
    }
    int main(void)
    {
        char box[ROW][COLUMN];
        char name[20] ="AAA" ;
        int x,y;
    
        printf("*************Tetris Game***********\n");
        printf("Please Enter Player Name: ");
        scanf("%s", &name);
        printf("Player Name:%s\n", name);
    
    
    
        for(x=0; x<15; x++)
        {
            for(y=0; y<15; y++)
            {
                box[x][y] = '_';
            }
        }
    
        disp_box(box);
    
        return 0;
    }

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I have no idea how that box is supposed to look like.
    But from how you initialize the 2dim array it could be
    Code:
    void disp_box (char b[ROW][COLUMN])
    {    
       int r,c;
    
       for(r=0; r<ROW;r++) {
            for(c=0; c<COLUMN;c++)
            {
                printf("*%c",b[r][c]);
            }
            printf("\n");
        }
    }
    Kurt

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    what is you're wanting to print exactly? A border separate from the contents of the array?

    Code:
    void disp_box(char b[ROW][COLUMN])
    {
        int r, c;
    
        // add border around contents of 2d array
    
        for(r = -1; r <= ROW; ++r)
        {
            for(c = -1; c  <= COLUMN; ++c)
            {
                if(r == -1 || r == ROW || c == -1 || c == COLUMN)
                    putchar('*');
                else
                    putchar(b[r][c]);
            }
    
            putchar('\n');
        }
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Printing a box is not difficult Printing a box from an array is not difficult.

    It IS difficult to help you, when you don't show what you want in an example, AND you don't explain exactly what you want (for a border, etc.).

    Making another thread on the subject won't help us understand what you want. Be specific about what you want, and post a picture of it - using Advanced editor >> manage attachments >> browse >> upload >> then the paper clip icon to place it where you want.


    Code:
    for(i=0;i<15;i++) {
       printf("|               |\n");
    }
    Put that in your print box function and you're off to a boxy start, are you not?
    AndiPersti likes this.

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    59
    its something like this... and i am making a tetris game...

    * * * * * * * * * * * * * * *
    * X X X *
    * X X *
    * X *
    * *
    * *
    * *
    * *
    * *
    * *
    * *
    * *
    * *
    * *
    * *
    * *
    * *
    * *
    * *
    * * * * * * * * * * * * * * *



    cant seem to upload the picture...
    Last edited by Alexius Lim; 05-09-2013 at 10:02 PM.

  6. #6
    Registered User
    Join Date
    May 2013
    Posts
    59
    hey ronin... thanks u got me what i wanted... could u explain to me about the if part? how why the if(r == -1 || r == ROW || c == -1 || c == COLUMN)


    Code:
    for(c = -1; c  <= COLUMN; ++c)
            {
                if(r == -1 || r == ROW || c == -1 || c == COLUMN)
                    putchar('*');
                else
                    putchar(b[r][c]);
            }
    

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,193
    In post #2, Zuk showed you how to print each element of the array in a way that correctly displays the rows and columns.

    I'm a bit perplexed why you're printing an extra (*) character along with the character in the array. It seems this would throw off the spacing of the grid. You should only be printing the array (i.e. the game board).

    Perhaps an example, using code similar to yours (with Zuk's advice) will help you visualize it.

    The game board can be said to consist of "tiles". Each tile is an element in the two-dimensional array. When you print the array, you print each tile so it forms a grid - the game board.

    This code is not as elegant as it could be, but hopefully the initialization of the "gameBoard" array illustrates how each "tile" is laid out.

    Code:
    #include <stdio.h>
    
    #define MAX_Y 10
    #define MAX_X 10
    
    void printGameBoard(char board[MAX_Y][MAX_X]);
    
    int main(void)
    {
        char gameBoard[MAX_Y][MAX_X] = {
            {'+','-','-','-','-','-','-','-','-','+'},
            {'|',' ',' ',' ',' ',' ',' ',' ',' ','|'},
            {'|',' ',' ',' ',' ',' ',' ',' ',' ','|'},
            {'|',' ',' ',' ',' ',' ',' ',' ',' ','|'},
            {'|',' ',' ',' ',' ',' ',' ',' ',' ','|'},
            {'|',' ',' ',' ',' ',' ',' ',' ',' ','|'},
            {'|',' ',' ',' ',' ',' ',' ',' ',' ','|'},
            {'|',' ',' ',' ',' ',' ',' ',' ',' ','|'},
            {'|',' ',' ',' ',' ',' ',' ',' ',' ','|'},
            {'+','-','-','-','-','-','-','-','-','+'}
        };
    
        /* print blank game board */
        printGameBoard(gameBoard);
    
        /* update the board with a piece */
        gameBoard[2][2] = '*';
        gameBoard[3][2] = '*';
        gameBoard[4][2] = '*';
        gameBoard[4][3] = '*';
    
        /* print updated game board */
        printGameBoard(gameBoard);
    
        return 0;
    }
    
    void printGameBoard(char board[MAX_Y][MAX_X])
    {
        int row,col;
    
        for(row=0; row<MAX_Y; row++)
        {
            for(col=0; col<MAX_X; col++)
            {
                printf("%c",board[row][col]);
            }
            printf("\n");
        }
    
        printf("\n\n");
    }

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I know it's a tetris game - of course. I haven't tried to compile or run this snippet of code, but it should get you thinking along the right path.

    Code:
    int r,c;
    char a[15][15];
    
    for(r=0;r<15;r++) {
    
       for(c=0;c<15;c++) {
          if(r==0 || r==14)       //handles the top and bottom rows
            a[r][c]='*';
    
          else if(c==0 || c==14)   //handles all the central rows
             a[r][c]='*';
          else
             a[r][c]=' ';
          printf("%c",a[r][c]);
       }
       printf("\n");
    }
    OK, just ran it, and it works fine.
    Last edited by Adak; 05-09-2013 at 10:19 PM.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    2,193
    Yeck, didn't realize this was a duplicate post.

  10. #10
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Quote Originally Posted by Alexius Lim View Post
    hey ronin... thanks u got me what i wanted... could u explain to me about the if part? how why the if(r == -1 || r == ROW || c == -1 || c == COLUMN)


    Code:
    for(c = -1; c  <= COLUMN; ++c)
            {
                if(r == -1 || r == ROW || c == -1 || c == COLUMN)
                    putchar('*');
                else
                    putchar(b[r][c]);
            }
    

    Glad you were able to get what you needed. I approached it along the lines of

    Code:
    for(r = -1; r <= ROW; ++r)
    {
        for(c = -1; c <= COLUMN; ++c)
        {
            if(r == -1 || r == ROW || c == -1 || c == COLUMN)
                putchar('*');
            else
                putchar(arr[r][c]);
        }
    
        putchar('\n');
    }
    array[15][15] is 15 characters wide by 15 characters high, so the border needed to be 17 characters wide by 17 characters high.

    Code:
    start   valid index     end
     -1  <->  [0-14]   <->  ROW
    (1)   +    (15)     +   (1) = 17 characters
    when r == -1 or r == ROW the if branch is taken for 17 iterations of c (-1 through COLUMN), which prints outs the top or bottom border. Once r is between 0 and ROW-1, the if path is taken only when c == -1 or c == COLUMN, printing a single '*' on either the left or right edge for 15 iterations of r (0 through ROW-1). Otherwise, r is between 0 and ROW-1 and c is between 0 and COLUMN-1, which becomes valid index positions for the array.
    Code:
         -1  [0-14] COLUMN
     -1   **************
    [ 0]  *   array    *
    [14]  *            *
    ROW   **************
    Personally, I find it easier to embed the border in the array; it really doesn't matter though, it's just another way to reach an end.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to set two arrays which print out on one line
    By Interista in forum C Programming
    Replies: 8
    Last Post: 10-26-2011, 10:18 AM
  2. Diffuculty with 2D arrays and print,fprintf
    By Melinnd in forum C Programming
    Replies: 5
    Last Post: 03-10-2010, 01:45 PM
  3. Desperately Need Help
    By Astra in forum C Programming
    Replies: 9
    Last Post: 10-21-2006, 07:59 AM
  4. I desperately need a project..........
    By frenchfry164 in forum Game Programming
    Replies: 13
    Last Post: 07-03-2003, 11:45 AM
  5. Need Some Help desperately
    By hydrianna in forum C Programming
    Replies: 1
    Last Post: 10-25-2001, 11:05 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21