Thread: Maze program question

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Maze program question

    Hi everyone, I have a question about a maze program i have to write. All i want to accomplish at the moment is to just locate the start and end coordinates of a maze the user would be inputting on stdin.

    For some reason the program gives me the correct y coordinate but not the x.

    For exp: if i were to input: 6 4
    o#####
    .#####
    .#####
    *#####

    my program would tell me the start("o") is at 1 0(supposed to be 0 0) and that end(*) is at 4 0(supposed to be 3 0).

    Thank you for your help!

  2. #2
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Code:
    
    #include<stdio.h>
    #include<stdlib.h>
    int main(void)
    {
            int x;
            int y;
            int startx;
            int starty;
            int finishx;
            int finishy;
            char a;
            
            int tempx=0;
            int tempy=0;
    
    
            scanf("%d %d", &y, &x);
    
    
    
    
    
    
            char maze[x][y];        
            
            for(;tempx<=x;tempx++)
            {
            
                    fgets(maze[tempx],y+2,stdin);
            }
    
    
    
    
    
    
     for(tempx=1;tempx<x+1;tempx++){
                    for(tempy=0;tempy<y;tempy++){
    
    
                            a=maze[tempx][tempy];
                            if(a=='o')
                            {
                                    printf("start position: %d %d\n", tempx, tempy);
                                    startx=tempx;
                                    starty=tempy;
                            }
    
    
                            if(a=='*')
                            {
                                    printf("end position: %d %d", tempx, tempy);
                                    finishx=tempx;
                                    finishy=tempy;
    
    
                            
                             
                            }
                            
                    } 
    
    
            }

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    You need to post your code if you want help.

    According to your post, just subtract 1 from x before printing its value.

  4. #4
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    code is posted

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Arrays, in C, start at index 0.
    Your loops should be from 0 to just before the length of the array
    for (index = 0; index < length; index++)

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    I did that at the beginning the reason why i started at 1 was because before i started to find the start and end points i tried to print out the maze just as a test. I used the normal way but for some reason it would never print the last line of the array. And some weird numbers and chars would appear in the first line.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    What i think is that fgets is scanning the coordinates i put for scanf and putting that into the array as well as the maze chars i entered. Thats why im getting those jumbled up numbers and chars. But im not sure how to fix that

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by stpeng View Post
    Code:
           char maze[x][y];        
            
            for(;tempx<=x;tempx++)
            {
            
                    fgets(maze[tempx],y+2,stdin);
            }
    You have two different causes of buffer overrun there.
    1. tempx goes up to x, but the first dimension of x is only valid from 0 to x-1
    2. You're telling it that each "string" is y+2 characters long, when in fact it is only y characters long.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Note that I meant "the first dimension of maze", not of x, in 1 above.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating a maze program structure in c, question
    By DaveAlex in forum C Programming
    Replies: 5
    Last Post: 04-10-2012, 09:22 PM
  2. maze program.
    By choiik in forum C Programming
    Replies: 1
    Last Post: 03-13-2011, 01:38 AM
  3. Replies: 11
    Last Post: 01-08-2011, 01:10 PM
  4. Maze program
    By supaben34 in forum C++ Programming
    Replies: 5
    Last Post: 12-08-2002, 07:36 PM
  5. Maze game, complete with maze editor and an example maze!
    By Brian in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-20-2002, 03:27 AM