Thread: Reading data into a 2D array

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    Reading data into a 2D array

    Hi I thought id challenge myself and make a small and very very basic
    graphics turtle logo program to plot graph points then prints them out.

    What I am having trouble the most with is reading input from the keyboard
    into the 2D array I passed from the main function. The below program is
    not finished but it was what I have managed to do so far... it does compile but obviously so far doesnt quite work.

    I have looked through my book and nothing tells me how to do this, I now
    how to print out a declared 2D array but Im not sure how to read the coordinates into it from the keyboard. I guess what I have attempted is totally
    wrong, and I gather using scanf() is wrong too.

    heres what I have so far:

    Code:
    #include <stdio.h>
    
    #define MAX_HEIGHT 20
    #define MAX_LENGTH 20
    #define TRUE 1
    #define FALSE 0
    
    /*function prototype*/
    int mainMenu ( char[][] );
    
    /*main function - begins program execution -----------------------------------*/
    int main ( void )
    {
       char board[ MAX_HEIGHT ][ MAX_LENGTH ] =
       {
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' },
          { '0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0','0' }
       };
    
       mainMenu ( board );
    
       getchar(); /*freeze console output window*/
    
       return 0; /*return value from int main*/
    }
    
    /*function to display the main menu*/
    int mainMenu ( char bd[][ 20 ] )
    {
       int choice = TRUE;
       int penUp = TRUE;
       int i, j;
    
       do
       {
          printf("\tTURTLE LOGO GRAPHICS\n\n"
                 "1 Place Pen Up\n"
                 "2 Place Pen Down\n"
                 "3 Draw\n"
                 "4 Show Grid\n"
                 "5 Quit\n"
                 "> ");
          scanf("%d", &choice);
    
          switch ( choice )
          {
          case 1:
             if ( penUp == TRUE )
             {
                puts("\nThe pen is already up\n");
             }
    
             if ( penUp == FALSE )
             {
                penUp = TRUE;
    
                puts("\nThe pen has been raised up\n");
             }
             break;
    
          case 2:
             if ( penUp == FALSE )
             {
                puts("\nThe pen is already down\n");
             }
    
             if ( penUp == TRUE )
             {
                penUp = FALSE;
    
                puts("\nThe pen has been placed down\n");
             }
             break;
    
          case 3:
             if ( penUp == TRUE )
             {
                puts("\nYou cannot draw with the pen raised up\n");
             }
    
             else
             {
                // This is my trouble part:
    
                printf("Enter the xy coordinates to draw: ");
                scanf("%d", &bd[ MAX_HEIGHT ][ MAX_LENGTH ]);
             }
             break;
    
          case 4:
             puts("\n");
    
             for ( i = 0; i < MAX_HEIGHT; i++ )
             {
                for ( j = 0; j < MAX_LENGTH; j++ )
                   printf("%c", bd[ i ][ j ]);
                   printf("\n");
             }
             puts("\n");
             break;
    
          case 5:
             return 0;
             break;
    
          default:
             printf("\n\n");
             break;
          }
       } while ( choice );
    }
    Double Helix STL

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    There is an easier way to initialize the array to '0', why not fill it with memset()?
    Code:
    memset(board, '0', MAX_HEIGHT + MAX_WIDTH);
    (I think )

    You have to map the coords to the array, for example have a starting point and an ending point.

    say, (x1, y1) = start and (x2, y2) = end, for drawing lines - requires a bit of maths.

    Or simply to draw a dot, ask for x and y (each will be an offset from the start of the array), ie the upper left hand 'cell' is (0, 0). I'd use fgets() to read the line (after firstly flushing stdin - see the FAQ) then sscanf() to read whatever format on that line, checking if the coords are in-bounds then set the point to whatever character.

    Unless I've missed the point of your problem?
    Last edited by zacs7; 08-17-2007 at 02:55 AM. Reason: Moronic idea

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    No thats perfect thanks zacs7 I appreicate that.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array becoming "deallocaded"
    By Aaron M in forum C Programming
    Replies: 2
    Last Post: 09-23-2006, 07:53 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07: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