Thread: how to print an array like in this picture..

  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 05:48 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  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 07:17 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    396
    Frankly, you are making it harder than it should be:
    ...
    Good one :-)

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

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, 01:41 PM
  3. i want to print a array
    By timhxf in forum C Programming
    Replies: 2
    Last Post: 01-07-2007, 04: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