Thread: how to fill an array with data from data file? / count unique cells visited in a maze

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    how to fill an array with data from data file? / count unique cells visited in a maze

    These questions refer to a maze program (16x16 block)

    first question:
    - here is a function I am trying to replace
    void lvl0_get_walls(int y, int x, int *North, int *East, int *West, int *South);
    /* This function indirectly returns the walls (1 for present, 0 of absent)
    in the cell y, x from the internal maze. Avaliable only at Level 0
    or 1. */
    I am assuming to use a 2D array so I can create my own maze, I made a few different data files with the respective x,y coordinates and 1's/0's to represent if walls are there or not. How do I include this in my driver? I got this from my text but don't really understand how it works/ what I have to change so it'll read in my data file:

    Code:
    int i,j;
         FILE *fp;
    
         if((fp = fopen("file","r")) == NULL)
         {     fprintf(stderr,"Unable to open file\n");
               exit(1);
         }
         for(i = 0; i < MAX_ROWS; i++)
              for(j = 0; j < MAX_COLS; j++)
                   fscanf(fp, "%d", &a[i][j]);
         fclose(fp);
    Here's a sample of my data file:
    /* x y n e w s */
    0 0 0 0 1 1
    0 1 0 0 1 0
    0 2 0 0 1 0
    0 3 1 0 1 0

    another version I made that doesnt include the x,y:
    /* south walls */
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
    1 0 1 0 1 0 1 0 1 0 0 0 0 0 0 1
    ...did the same for north,east and west walls as well



    2nd question:
    I have to count the number of unique cells visited in the maze, we already have a basic cell counter which increments on every move. How do I go about setting up a unique counter? I'm thinking something along the lines of when a new cell is visited it will set a variable to TRUE and increment the count. how would I set all cells to unvisited?

    Thanks!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... What you need to do here is turn off your computer and get a few sheets of paper and a pencil...
    Sit down and PLAN your program ... become the process... What do you need to know? What do you need to do? at each step along the way.
    Now break that down to a series of steps that must be completed ... become the solution...
    Now break that down into a list of the functions you need --IsVisited(), GetWalls(), etc--
    Now you're ready to start writing code.

    There is no programmer anywhere, no matter how experienced or clever, who can code a solution to a problem he does not understand.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    How do I include this in my driver?
    Don't know what you mean. Your code should read the data file. The data file doesn't need to be "included".

    2nd question:...I have to count the number of unique cells visited in the maze
    You need an array (for all the cells) that stores essentially a flag for the cell having been visited or not. Initialize the array's elements to FALSE and set a particular element to TRUE when visited. No need to test if it's been visited already, just set to TRUE when the cell is "entered" by the user.

    Advice: don't use x, y, i, or j. Use row/col so you can keep track of which direction you are going in your code.

    Take it step by step.In your 'c', open and read the file and print out the contents. When that works move on.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding data to existing data in an input file?
    By matthayzon89 in forum C Programming
    Replies: 4
    Last Post: 11-20-2010, 11:23 PM
  2. Column count from data file
    By spiit231 in forum C Programming
    Replies: 6
    Last Post: 02-27-2008, 12:59 PM
  3. unique data types
    By simpleid in forum C++ Programming
    Replies: 20
    Last Post: 11-05-2007, 03:28 AM
  4. Replies: 21
    Last Post: 11-03-2007, 02:56 PM
  5. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM