Thread: C Filling and Printing a 2-d array

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    7

    C Filling and Printing a 2-d array

    ok, I have a data file. inside the data file is an ascii picture that is 20 rows by 50 columns.

    I need to open that file and read it into, another array 22x52 (added one line each for the top, bottom, right and left borders). The side borders of the new array are pipes |, the top and bottom borders are all dashes -, except for the 4 corners which are also pipes.

    So what I did is opened the file, filled the entire file with a pattern. then filled in the picture from the file, overwriting the unneeded sections of the pattern. then I attempt to print the file and it comes out all screwy. The alignment is all off, and I can't figure out where it went wrong.

    Code:
    #include <stdio.h>
    
    
    #define ROWS 20           /* a 2-d array, 20x50 */
    #define COLUMNS 50
    #define ARRAY_ROWS 22     /* add two rows and two columns for the border */
    #define ARRAY_COLUMNS 52
    
    
    
    int main()
    {
       int i, j;
       char array[ARRAY_ROWS][ARRAY_COLUMNS];  
    
       int nRows = ARRAY_ROWS;
       FILE *ifp;
    
       ifp = fopen("snoopy.dat", "r");
    
       /* fills the outter rim of the array with a border 
          "|"'s are run down the sides, "-"'s are run along the 
           top and bottom border, except for the four corners, 
          where a | is printed. */
       /* I've tested it, and know this part is working correctly */
       for (i = 0; i < nRows; i++)
       {
            for (j = 0; j < ARRAY_COLUMNS; j++)
            {
    	    if (j == 0 || j == ARRAY_COLUMNS - 1)
    	    {
    	        array [i][j] = '|';
    	    }
                       else
    	    {
                            array [i][j] = '-';
    	    }
            }
    
        }
    
    	/* fill the inner part of the array with what's in the file.
    	   The problem seems to be with this part of the code.*/
        for (i = 1; i < nRows -1; i++)
        {
    
               for (j = 1; j < ARRAY_COLUMNS - 1; j++)
               {
                    fscanf (ifp, "%c", &array[i][j]);   
               }
       
         } 
    
    
    
    
    
            /* Print the array.  If there is a 0, replace it with a blank   
                 space */
           /* This part also appears to be working correctly. */
        for (i = 0; i < nRows; i++)
        {
    
              for (j = 0; j < ARRAY_COLUMNS; j++)
              {
                   if (array [i][j] == '0')
                   {
                        printf (" ");
                   }
                   else
                   {
                        printf ("%c", array [i][j]);
                   }
             }
    
            printf ("\n"); 
        }
    
    
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > fscanf (ifp, "%c", &array[i][j]);
    The problem with %c is that it reads newlines (most every other conversion ignores them). So you end up with skewed data

    Try this approach
    Code:
               char buff[BUFSIZ];
               fgets( buff, sizeof buff, ifp );
               for (j = 1; j < ARRAY_COLUMNS - 1; j++)
               {
                    array[i][j] = buff[j-1];   
               }
    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
    Registered User
    Join Date
    Apr 2003
    Posts
    7
    thanks a lot man, that works.

    can you explain what it is doing though?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If I may...
    Code:
    char buff[BUFSIZ];
    fgets( buff, sizeof buff, ifp );
    This reads the whole line at once into buff.
    Code:
    for (j = 1; j < ARRAY_COLUMNS - 1; j++)
    {
       array[i][j] = buff[j-1];   
    }
    This copies the characters from buff to array, offset by 1 character.

    The index j traverses the characters in the line from buff[0] to buff[ARRAY_COLUMNS - 2]. The "row" of array is i, the "column" is j -- which, for the assignment to array, is from 1 to ARRAY_COLUMNS - 2 (inclusive), skipping the border positions at array[i][0] and array[i][ARRAY_COLUMNS - 1].
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help required in image filling program.
    By co123ol in forum C Programming
    Replies: 1
    Last Post: 03-04-2008, 02:42 PM
  2. List Reading - Printing
    By ch4 in forum C Programming
    Replies: 3
    Last Post: 06-13-2007, 10:50 AM
  3. Initialize an array in FORTRAN
    By panfilero in forum Tech Board
    Replies: 0
    Last Post: 12-01-2005, 01:51 AM