Thread: Problem with traversing border of 2D array

  1. #1
    Certified Newbie dncarter's Avatar
    Join Date
    Sep 2015
    Location
    USA
    Posts
    19

    Problem with traversing border of 2D array

    Just a heads up this is my first time using C so I may be totally wrong on syntax or something, just bear with my newbie-ness.

    So this is for a programming assignment that has us mapping a path through a maze. The maze is made up of 1s and 0s and is read in from a .txt file and stored into a 2D array. The 1s are walls and 0s are open paths through the maze. The code used to read in the maze from the file is provided and so it's not something we should be changing. It stores the numbers as chars in the array. We are supposed to first trace the border to find the entrance and exit to the maze. I am using a while loop for this. It is guaranteed that any maze we are given will have exactly two 0s on the border. The first one will be the entrance, the second the exit. He wants us to start at the upper left corner (coordinates [0][0]) and go counterclockwise around the maze.

    My problem is that my code reads out the first border (as in, the first column) but then when it goes to read the bottom border (the last row) it isn't outputting anything. I don't know why.

    Code:
    #include<stdio.h>
    
    
    
    
    int main() {
        int field[200][200] = {1};
        FILE *fptr;
        char c;
        char file_name[20];
        int i, j, m, n;
        
        printf("Enter the dimensions of the array: \n");
        scanf("%d", &m);
        scanf("%d", &n);
        
        printf("Type in the name of the file containing the field:\n");
        scanf("%s",file_name);
        fptr = fopen(file_name,"r");
        for (i=0; i < m; i++)
         for (j=0; j < n; j++){
           c = fgetc(fptr); 
           while ( !((c == '1')||(c == '0')) ) c = fgetc(fptr);
           field[i][j] = c;
        }
        fclose(fptr);
        
        for (i=0; i < m; i++)
        for (j=0; j < n; j++)  {
          if (j == 0) printf("\n");                
          printf("%c  ", field[i][j]);
        }
        printf("\n");
        
      int count = 0;
      int row = 0;
      int col = 0;
      int entRow, entCol, exRow, exCol;
    
    
      while (count != 2) {
        printf("array: %c \n", field[row][col]);
          if(field[row][col] == '0') {
              count++;
              if(count == 1){
                  entRow = row;
                entCol = col;
              }else if(count == 2){
                exRow = row;
                exCol = col;
                break;
              }
          }
    
    
          if(row < m && col == 0){
              row++;
          }else if(row == m && col < n){
              col++;
          }else if(row <= m && col == n){
              row--;        
          }else if(row == 0 && col <= n){
                col--;
          }
          
          if (row > m || row < 0) { break; }
          if (col > n || col < 0) { break; }
          
          printf("row: %d ", row);
          printf("col: %d \n", col);
      }
    
    
      printf("Entrance found at %d, %d\n", entRow, entCol);
      printf("Exit found at %d, %d\n", exRow, exCol);
    
    
    }
    The output looks like this:

    Code:
    Enter the dimensions of the array: 
    12
    10
    Type in the name of the file containing the field:
    field.txt
     
    1  1  1  1  1  1  1  1  1  1  
    1  0  1  1  0  1  1  0  0  1  
    0  1  1  1  1  0  0  0  1  1  
    1  1  1  1  1  0  0  1  1  1  
    1  0  0  1  0  0  0  0  1  1  
    1  0  0  0  1  0  0  1  0  0  
    1  0  1  0  0  1  1  0  0  1  
    1  1  1  0  0  1  1  0  0  1  
    1  1  0  1  1  1  0  0  0  1  
    1  0  0  0  0  0  0  0  1  1  
    1  1  1  1  1  0  0  1  0  1  
    1  1  1  1  1  1  1  1  1  1  
    array: 1 
    row: 1 col: 0 
    array: 1 
    row: 2 col: 0 
    array: 0 
    row: 3 col: 0 
    array: 1 
    row: 4 col: 0 
    array: 1 
    row: 5 col: 0 
    array: 1 
    row: 6 col: 0 
    array: 1 
    row: 7 col: 0 
    array: 1 
    row: 8 col: 0 
    array: 1 
    row: 9 col: 0 
    array: 1 
    row: 10 col: 0 
    array: 1 
    row: 11 col: 0 
    array: 1 
    row: 12 col: 0 
    array:  
    row: 12 col: 1 
    array:  
    row: 12 col: 2 
    array:  
    row: 12 col: 3 
    array:  
    row: 12 col: 4 
    array:  
    row: 12 col: 5 
    array:  
    row: 12 col: 6 
    array:  
    row: 12 col: 7 
    array:  
    row: 12 col: 8 
    array:  
    row: 12 col: 9 
    array:  
    row: 12 col: 10 
    array:  
    row: 11 col: 10 
    array:  
    row: 10 col: 10 
    array:  
    row: 9 col: 10 
    array:  
    row: 8 col: 10 
    array:  
    row: 7 col: 10 
    array:  
    row: 6 col: 10 
    array:  
    row: 5 col: 10 
    array:  
    row: 4 col: 10 
    array:  
    row: 3 col: 10 
    array:  
    row: 2 col: 10 
    array:  
    row: 1 col: 10 
    array:  
    row: 0 col: 10 
    array:  
    Entrance found at 2, 0
    Exit found at 0, 0
     
    array:
    row: 12 col: 6
    array:
    row: 12 col: 7
    array:
    row: 12 col: 8
    array:
    row: 12 col: 9
    array:
    row: 12 col: 10
    array:
    row: 11 col: 10
    array:
    row: 10 col: 10
    array:
    row: 9 col: 10
    array:
    row: 8 col: 10
    array:
    row: 7 col: 10
    array:
    row: 6 col: 10
    array:
    row: 5 col: 10
    array:
    row: 4 col: 10
    array:
    row: 3 col: 10
    array:
    row: 2 col: 10
    array:
    row: 1 col: 10
    array:
    row: 0 col: 10
    array:
    Entrance found at 2, 0
    Exit found at 0, 0
    It's obviously not correct. It stops printing anything from array, and the traversal gets all messed up. I'm not even sure what happens one it prints a space and then starts over in a random location. Can anyone spot my error(s)? Thanks!

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    So this is for a programming assignment that has us mapping a path through a maze. The maze is made up of 1s and 0s and is read in from a .txt file and stored into a 2D array.
    Have you gotten this working correctly?

    The 1s are walls and 0s are open paths through the maze. The code used to read in the maze from the file is provided and so it's not something we should be changing. It stores the numbers as chars in the array.
    Ha! Answered my own question.

    We are supposed to first trace the border to find the entrance and exit to the maze. I am using a while loop for this. It is guaranteed that any maze we are given will have exactly two 0s on the border. The first one will be the entrance, the second the exit. He wants us to start at the upper left corner (coordinates [0][0]) and go counterclockwise around the maze.
    Why use a while-loop when for-loops would suffice?

    Do you have the number of rows? Do you have the number of columns?

    Looks like you do. Excellent.

    There are 4 borders or edges of the maze. These are indices [0][ 0 : cols - 1], [ 0 : rows - 1][ cols - 1 ], [ rows - 1 ][ 0 : cols - 1], [ 0 : rows - 1 ][ 0 ].

    Here I'm using the colon to mean, indices 0 through cols - 1 or 0 through rows - 1.

    So, to check the first border,

    Code:
    // indices [0][ 0 : cols - 1]
    
    for ( size_t i = 0; i < cols; ++i )
    {
         if ( maze[ 0 ][ i ] == '0' )
              // do something here
    }
    Does this help?

    While-loops sound like a good way of getting stuck infinitely. I'd just use 4 for-loops, tbh.
    Last edited by MutantJohn; 09-04-2015 at 01:11 PM.

  3. #3
    Certified Newbie dncarter's Avatar
    Join Date
    Sep 2015
    Location
    USA
    Posts
    19
    I was considering four for-loops initially. Maybe I'd have an easier time with that. I decided to try a while-loop so that as soon as I found both 0s I could just stop. I also might have secretly been trying to be a fancy-pants about it when really we are only being graded on IF it works, not HOW it works.

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by dncarter View Post
    I was considering four for-loops initially. Maybe I'd have an easier time with that. I decided to try a while-loop so that as soon as I found both 0s I could just stop. I also might have secretly been trying to be a fancy-pants about it when really we are only being graded on IF it works, not HOW it works.
    You can put break statements in both for- and while-loops.

    However, it'd technically be a better, more secure practice to NOT break upon the first encounter. If you wanted to be super anally retentive, you'd traverse all 4 edges of the array and keep a counter of the number of 0's. If the number was not exactly equal to 2, you have an error. This is assuming you're coding in the case of someone trying to pass "troll" input and give you a maze that's no good.

    The problem with the while-loop is the potential for infinite walking. It's best to use a for-loop which has a guaranteed break condition.

  5. #5
    Certified Newbie dncarter's Avatar
    Join Date
    Sep 2015
    Location
    USA
    Posts
    19
    Alright, I'll try re-doing my code with for-loops. And since this is for a class, I doubt I'll be getting "troll" input. The instructor gave us the code he'll be using to randomly generate the mazes so we can test with it ourselves and I haven't seen any errors yet.

  6. #6
    Certified Newbie dncarter's Avatar
    Join Date
    Sep 2015
    Location
    USA
    Posts
    19
    So I changed it to four for-loops, but I am still having the same problem with the array not printing out anything after a little while:

    Code:
    Code:
    #include<stdio.h>
    
    
    
    
    int main() {
        char field[200][200] = {'1'};
        FILE *fptr;
        char c;
        char file_name[20];
        int i, j, m, n;
        
        printf("Enter the dimensions of the array: \n");
        scanf("%d", &m);
        scanf("%d", &n);
        
        printf("Type in the name of the file containing the field:\n");
        scanf("%s",file_name);
        fptr = fopen(file_name,"r");
        for (i=0; i < m; i++)
         for (j=0; j < n; j++){
           c = fgetc(fptr); 
           while ( !((c == '1')||(c == '0')) ) c = fgetc(fptr);
           field[i][j] = c;
        }
        fclose(fptr);
        
        for (i=0; i < m; i++)
        for (j=0; j < n; j++)  {
          if (j == 0) printf("\n");                
          printf("%c  ", field[i][j]);
        }
        printf("\n");
        
     int count = 0;
    int row, col;
    int entRow, entCol, exRow, exCol;
    
    
    for(row = 0; row < m; row++) {
        printf("Array: %c \n", field[row][0]);
        printf("row: %d col: 0\n", row);
        if(field[row][0] == '0') {
            count++;
            if(count == 1){
                entRow = row;
                entCol = 0;
            }else if (count == 2) {
                exRow = row;
                exCol = 0;
                break;
            }
        }
    }
    
    
    if (count != 2) {
        for(col = 0; col < n; col++) {
            printf("Array: %c \n", field[m][col]);
            printf("row: %d col: %d\n", m, col);
            if(field[m][col] == '0') {
                count++;
                if(count == 1){
                    entRow = m;
                    entCol = col;
                }else if (count == 2) {
                    exRow = m;
                    exCol = col;
                    break;
                }
            }
        }
    }
    
    
    if (count != 2) {
        for(row = m; row >= 0; row--) {
            printf("Array: %c \n", field[row][n]);
            printf("row: %d col: %d\n", row, n);
            if(field[row][n] == '0') {
                count++;
                if(count == 1){
                    entRow = row;
                    entCol = n;
                }else if (count == 2) {
                    exRow = row;
                    exCol = n;
                    break;
                }
            }
        }
    }
    
    
    if (count != 2) {
        for(col = n; col >= 0; col--) {
            printf("Array: %c \n", field[0][col]);
            printf("row: 0 col: %d\n", col);
            if(field[0][col] == '0') {
                count++;
                if(count == 1){
                    entRow = 0;
                    entCol = col;
                }else if (count == 2) {
                    exRow = 0;
                    exCol = col;
                    break;
                }
            }
        }
    }
    
    
      printf("Entrance found at %d, %d\n", entRow, entCol);
      printf("Exit found at %d, %d\n", exRow, exCol);
    
    
    }
    Output:
    Code:
    Enter the dimensions of the array: 
    12
    10
    Type in the name of the file containing the field:
    field.txt
     
    1  1  1  1  1  1  1  1  1  1  
    1  0  1  1  0  1  1  0  0  1  
    0  1  1  1  1  0  0  0  1  1  
    1  1  1  1  1  0  0  1  1  1  
    1  0  0  1  0  0  0  0  1  1  
    1  0  0  0  1  0  0  1  0  0  
    1  0  1  0  0  1  1  0  0  1  
    1  1  1  0  0  1  1  0  0  1  
    1  1  0  1  1  1  0  0  0  1  
    1  0  0  0  0  0  0  0  1  1  
    1  1  1  1  1  0  0  1  0  1  
    1  1  1  1  1  1  1  1  1  1  
    Array: 1 
    row: 0 col: 0
    Array: 1 
    row: 1 col: 0
    Array: 0 
    row: 2 col: 0
    Array: 1 
    row: 3 col: 0
    Array: 1 
    row: 4 col: 0
    Array: 1 
    row: 5 col: 0
    Array: 1 
    row: 6 col: 0
    Array: 1 
    row: 7 col: 0
    Array: 1 
    row: 8 col: 0
    Array: 1 
    row: 9 col: 0
    Array: 1 
    row: 10 col: 0
    Array: 1 
    row: 11 col: 0
    Array:  
    row: 12 col: 0
    Array:  
    row: 12 col: 1
    Array:  
    row: 12 col: 2
    Array:  
    row: 12 col: 3
    Array:  
    row: 12 col: 4
    Array:  
    row: 12 col: 5
    Array:  
    row: 12 col: 6
    Array:  
    row: 12 col: 7
    Array:  
    row: 12 col: 8
    Array:  
    row: 12 col: 9
    Array:  
    row: 12 col: 10
    Array:  
    row: 11 col: 10
    Array:  
    row: 10 col: 10
    Array:  
    row: 9 col: 10
    Array:  
    row: 8 col: 10
    Array:  
    row: 7 col: 10
    Array:  
    row: 6 col: 10
    Array:  
    row: 5 col: 10
    Array:  
    row: 4 col: 10
    Array:  
    row: 3 col: 10
    Array:  
    row: 2 col: 10
    Array:  
    row: 1 col: 10
    Array:  
    row: 0 col: 10
    Array:  
    row: 0 col: 10
    Array: 1 
    row: 0 col: 9
    Array: 1 
    row: 0 col: 8
    Array: 1 
    row: 0 col: 7
    Array: 1 
    row: 0 col: 6
    Array: 1 
    row: 0 col: 5
    Array: 1 
    row: 0 col: 4
    Array: 1 
    row: 0 col: 3
    Array: 1 
    row: 0 col: 2
    Array: 1 
    row: 0 col: 1
    Array: 1 
    row: 0 col: 0
    Entrance found at 2, 0
    Exit found at 0, 0
    It finds the entrance correctly but then something happens and it stops printing what is in the array after the first border segment and so it doesn't find the exit.

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Are your indices correct? I notice you using m instead of m - 1. Is that the cause of the errors?

  8. #8
    Certified Newbie dncarter's Avatar
    Join Date
    Sep 2015
    Location
    USA
    Posts
    19
    Well, I am saying r < m, not r <= m, so it'd stop at m - 1 wouldn't it?

    EDIT: And even if it went out of bounds, the instructor has us nesting this in the upper left corner of a 200 x 200 array populated entirely of 1s, so it'd still have something to print, right?

  9. #9
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Code:
    if(field[m][col] == '0') {
    I was referring to this.

    Should you use m or m - 1?

  10. #10
    Certified Newbie dncarter's Avatar
    Join Date
    Sep 2015
    Location
    USA
    Posts
    19
    Ahh right. Those should be m - 1 and n - 1. Fixed all of that and here's the output now:

    Code:
    Enter the dimensions of the array: 
    12
    10
    Type in the name of the file containing the field:
    field.txt
     
    1  1  1  1  1  1  1  1  1  1  
    1  0  1  1  0  1  1  0  0  1  
    0  1  1  1  1  0  0  0  1  1  
    1  1  1  1  1  0  0  1  1  1  
    1  0  0  1  0  0  0  0  1  1  
    1  0  0  0  1  0  0  1  0  0  
    1  0  1  0  0  1  1  0  0  1  
    1  1  1  0  0  1  1  0  0  1  
    1  1  0  1  1  1  0  0  0  1  
    1  0  0  0  0  0  0  0  1  1  
    1  1  1  1  1  0  0  1  0  1  
    1  1  1  1  1  1  1  1  1  1  
    Array: 1 
    row: 0 col: 0
    Array: 1 
    row: 1 col: 0
    Array: 0 
    row: 2 col: 0
    Array: 1 
    row: 3 col: 0
    Array: 1 
    row: 4 col: 0
    Array: 1 
    row: 5 col: 0
    Array: 1 
    row: 6 col: 0
    Array: 1 
    row: 7 col: 0
    Array: 1 
    row: 8 col: 0
    Array: 1 
    row: 9 col: 0
    Array: 1 
    row: 10 col: 0
    Array: 1 
    row: 11 col: 0
    Array: 1 
    row: 11 col: 0
    Array: 1 
    row: 11 col: 1
    Array: 1 
    row: 11 col: 2
    Array: 1 
    row: 11 col: 3
    Array: 1 
    row: 11 col: 4
    Array: 1 
    row: 11 col: 5
    Array: 1 
    row: 11 col: 6
    Array: 1 
    Array: 1
    row: 11 col: 7
    Array: 1
    row: 11 col: 8
    Array: 1
    row: 11 col: 9
    Array: 1
    row: 11 col: 9
    Array: 1
    row: 10 col: 9
    Array: 1
    row: 9 col: 9
    Array: 1
    row: 8 col: 9
    Array: 1
    row: 7 col: 9
    Array: 1
    row: 6 col: 9
    Array: 0
    row: 5 col: 9
    Entrance found at 2, 0
    Exit found at 5, 9
    All correct. Thanks a bunch!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Traversing an array of lists of objects
    By Big Lee in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2013, 06:38 PM
  2. Traversing n-dimensional array
    By yorki in forum C Programming
    Replies: 15
    Last Post: 01-10-2011, 09:14 AM
  3. Replies: 5
    Last Post: 10-17-2010, 01:30 PM
  4. Traversing Link List Problem
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 05-15-2007, 02:13 AM
  5. Traversing an array
    By DigitalKhaos in forum C Programming
    Replies: 3
    Last Post: 05-17-2004, 12:37 PM