how to print an array like in this picture..

This is a discussion on how to print an array like in this picture.. within the C Programming forums, part of the General Programming Boards category; my code only prints the chars of the 2d array. how to add this "frame" to each char like in ...

  1. #1
    Banned
    Join Date
    Oct 2008
    Posts
    1,535

    how to print an array like in this picture..

    my code only prints the chars of the 2d array.
    how to add this "frame" to each char
    like in this picture:
    http://img184.imageshack.us/img184/74/37908549bp5.gif

    Code:
    void printBoard(char board[N][N],int size){
     
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("%c ",board[index][kndex]);
         }
         printf("\n");
     }
    
    }//end printBoard
    Last edited by transgalactic2; 12-31-2008 at 04:48 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Think about how to print the
    +-+-+-+-+
    rows first, then see what develops
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried this
    i dont know how to put on each char +-+
    ??
    Code:
    void printBoard(char board[N][N],int size){
     
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
             printf("|%c|",board[index][kndex]);
         }
         printf("\n");
     }
    
    }//end printBoard

  4. #4
    Registered User
    Join Date
    Aug 2008
    Location
    Croatia
    Posts
    36
    The code is a bit different but the principle is the same:
    Code:
    #include <stdio.h>
    
    int main() {
    
    
            int  i, c, r;
            for(c=0; c < 5; c++){
                    printf("+");
                    for(r=0; r < 5; r++)
                            printf("--+");
                    printf("\n|");
                    for( i=0; i < 5; i++)
                            printf("? |");
                            printf("\n");
            }
    
    
    return 0;
    }

  5. #5
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    its not like the picture
    above every char i need to have +-+ not +--+
    and there needs to be a bottom line

    i tried to change --+ to -+

    its not working
    ??

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    Create two functions
    printPlusMinus(int n);
    Which prints "+-" n times, and a single + at the end

    printRow(/*your turn*/);
    Which prints "|" and a char from your map, followed by a single "|" at the end.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Location
    Croatia
    Posts
    36
    Quote Originally Posted by transgalactic2 View Post
    its not like the picture
    above every char i need to have +-+ not +--+
    and there needs to be a bottom line

    i tried to change --+ to -+

    its not working
    ??

    Code:
    #include <stdio.h>
    
    int main() {
    
    
            int  i, c, r, a;
            for(c=0; c < 5; c++){
                    printf("+");
                    for(r=0; r < 5; r++)
                            printf("-+");
                    printf("\n|");
                    for( i=0; i < 5; i++)
                            printf("?|");
                    printf("\n");
            }
    
            printf("+");
            for(r=0; r < 5; r++)
                   printf("-+");
            printf("\n");
    
    
    return 0;
    }
    Here, problem fixed by adding another loop at the end. This can be done different, a bit shorter...but I'm currently in the rush.
    Last edited by dotunix; 12-31-2008 at 06:17 AM.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Learn to use flowcharts and pseudo code!
    You keep asking questions all about logic! However are you going to learn anything if you just keep asking questions without trying to solve it on your own?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I tried to program like this.
    I put in every cell char '0'.
    its not working i get errors
    |9|warning: missing braces around initializer|
    |9|warning: (near initialization for `board[0]')|
    |28|error: syntax error at end of input|
    It points to the last col of the function.

    Where is the mistake in this code??
    Code:
    
    #include <stdio.h>
    
    
    #define N 9
    
    void printBoard(char board[N][N],int size);
    int main(){
    char board[N][N]={'0'};
    
    printBoard(board,5);
    return 0;
    }
    
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
    
     }

  10. #10
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I wrote it in another way.
    but still i have this error pointing at the last col of the function.
    |28|error: syntax error at end of input|

    Code:
    #include <stdio.h>
    
    
    #define N 9
    
    void printBoard(char board[N][N],int size);
    
    int main(){
        int index,kndex;
    char board[N][N];
    
    for(index=0;index<N;index++){
         for(kndex=0;kndex<N;kndex++){
             board[index][kndex]='0';
         }
    }
    
    printBoard(board,5);
    
    }
    
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
    
     }

  11. #11
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I solved the errors but it shows different picture then it should to.

    it should look like
    http://img184.imageshack.us/img184/74/37908549bp5.gif

    where is the mistake?
    Code:
    #include <stdio.h>
    
    
    #define N 9
    
    void printBoard(char board[N][N],int size);
    
    int main(){
        int index,kndex;
    char board[N][N];
    
    for(index=0;index<N;index++){
         for(kndex=0;kndex<N;kndex++){
             board[index][kndex]='0';
         }
    }
    
    printBoard(board,5);
    
    }
    
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
    
     }
    }

  12. #12
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i solved it

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,650
    Frankly, you are making it harder than it should be:
    Code:
    #include <stdio.h>
    
    int main()
    {
        printf("%s",
            "+-+-+-+-+-+\n"
            "|?|?|?|?|?|\n"
            "+-+-+-+-+-+\n"
            "|?|?|?|?|?|\n"
            "+-+-+-+-+-+\n"
            "|?|?|?|?|?|\n"
            "+-+-+-+-+-+\n"
            "|?|0|?|?|?|\n"
            "+-+-+-+-+-+\n"
            "|?|?|?|?|?|\n"
            "+-+-+-+-+-+\n");
        return 0;
    }
    I expect that the above program does not actually do what you want, but that would be because you never precisely stated what you were really trying to do.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i solved it
    its working
    is there an error?
    Code:
    void printBoard(char board[N][N],int size){
    int index,kndex;
     for(index=0;index<size;index++){
         for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
         printf("\n");
         for(kndex=0;kndex<size;kndex++){
    
             printf("|%c|",board[index][kndex]);
         }
         printf("\n");
    
     }
      for(kndex=0;kndex<size;kndex++){
             printf("+-+");
    
         }
    }

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    Frankly, you are making it harder than it should be:
    ...
    Good one :-)

    and it also replies to:
    its working
    is there an error?

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Print Array in reverse order
    By swgh in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2007, 12:41 PM
  3. i want to print a array
    By timhxf in forum C Programming
    Replies: 2
    Last Post: 01-07-2007, 03:25 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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