Thread: Problem with printing a maze...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Problem with printing a maze...

    Hi,

    THe printing part of my maze program doesn't work. So i picked out the part that doens't work ( below ). It should print 12 characters per column and per row, however it doesn't. Pls help. THnx in advance!

    Code:
    /* Prints a maze */
    
    #include <stdio.h>
    
    void printMaze( char maze[][ 12 ] );
    
    int main()
    {
       int maze[ 12 ][ 12 ] = { '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1',  /* 0 */
                                '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1',  /* 1 */
                                '0', '0', '1', '0', '1', '0', '1', '1', '1', '1', '0', '1',  /* 2 */
                                '1', '1', '1', '0', '1', '0', '0', '0', '0', '1', '0', '1',  /* 3 */
                                '1', '0', '0', '0', '0', '1', '1', '1', '0', '1', '0', '0',  /* 4 */
                                '1', '1', '1', '1', '0', '1', '0', '1', '0', '1', '0', '1',  /* 5 */
                                '1', '0', '0', '1', '0', '1', '0', '1', '0', '1', '0', '1',  /* 6 */
                                '1', '1', '0', '1', '0', '1', '0', '1', '0', '1', '0', '1',  /* 7 */
                                '1', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '1',  /* 8 */
                                '1', '1', '1', '1', '1', '1', '0', '1', '1', '1', '0', '1',  /* 9 */
                                '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1',  /* 10 */
                                '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', '1' };/* 11 */
    
       printMaze( maze );
    
       getch();
       return 0;
    }
    
    void printMaze( char maze[][ 12 ] )
    {
       int i, j;
    
       printf( "\n\n" );
    
       for ( i = 0; i < 12; i++ )
          for ( j = 0; j < 12; j++ ) {
             if ( j == 11 )
                printf( "\n" );
             printf( "%c", maze[ i ][ j ] );
          }
    }

  2. #2
    Unregistered
    Guest
    Make sure "maze" is defined as either an integer or a char array (you have it as int where you define it and char in the function you pass it to). I would say char is the way to go here.

    Also, put your statement to print a newline AFTER you print the array character... otherwise the last character of the line gets shifted to the next line and the whole maze gets thrown off.

    Hope that helps.

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > printf( "%c", maze[ i ][ j ] );

    You're printing a character when maze is declared as an integer array.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    void printMaze( int maze[][ 12 ] );
    void printMaze( int maze[][ 12 ] )
    {
       int i, j;
       printf( "\n\n" );
       for ( i = 0; i < 12; i++ ) {
          for ( j = 0; j < 12; j++ ) {
             printf( "%c", maze[ i ][ j ] );
          }
          printf( "\n" );
       }
    }
    Your compiler must have generated a warning about incompatible types - surely

  5. #5
    Unregistered
    Guest
    If he makes it "int" and leaves the single quotes in the array declaration/definition, won't he end up with the integer equivalent of the CHARACTER 1 and 0 versus the number itself?

    Please let me know if I'm way off on that. Thanks.

  6. #6
    Unregistered
    Guest
    Sorry... that was very presumptuous (sp?) of me. He OR she...

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > won't he end up with the integer equivalent of the CHARACTER 1 and 0 versus the number itself?
    Which is exactly what you want, when your're printing them using %c in a printf statement

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 12:57 PM
  2. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  3. Problem in message handling VC++
    By 02mca31 in forum Windows Programming
    Replies: 5
    Last Post: 01-16-2009, 09:22 PM
  4. Homework HELP - Problem Printing to file
    By brentshreve in forum C Programming
    Replies: 5
    Last Post: 04-23-2005, 02:11 AM
  5. problem with printing a structed array using for loop
    By Prezo in forum C++ Programming
    Replies: 2
    Last Post: 09-15-2002, 09:00 AM