Thread: need a help in calcaulating a -bb.

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    130

    need a help in calcaulating a -bb.

    Hello, at first It's a pleasure for me as a new C programmer to start participating here and helping others n collecting ideas about c programming simultaneously, as I've seen it sounds a very helpful and wonderful website.

    well, lets get to the question, I have been struggling two days ago until I succeeded in coding a code that's finding the correct path to get out from a binary maze(included just numbers 0 and 1) like inputing a matrix:
    "(1111000,
    0111110,
    0101100,
    1101100,
    0111010,
    010101G)"
    , 0 defined as a wall(block way) and 1 defined as a 'move on' until u arrive to the G(the exit), the indoor is mat[1][1], the code is compiling finely and it finds leading path correctly, but I'm stuck in two things :
    1) I want the code besides to finding the way(as it works in my code) to calculate the same way that leads you to get out from the maze, and I tried to calculate but the output isn't correct, it prints the length of the path is sum=0, and that's wrong.
    can anyone aim me how to write a function/code to calculate the path properly?
    I mean with calculating a path, it's the sum of "1" n the path that you get out by, from the maze.

    2) lets say there's another way in the maze leads to the same exit, what editing should I do in my code for printing the other path besides to the first path?, and how could I calculate the other path's length So?
    can anyone please aim me on how could I edit my code for calculating the other path length and find the other path if possible?

    my code:
    Code:
    #include <stdio.h>
    #define FALSE 0
    #define TRUE 1
    #define NROWS 7
    #define MCOLS 7
    // Symbols:
    // '1' = open
    // '0' = blocked
    // 'S' = start
    // 'G' = goal
    // '+' = path
    // 'x' = bad path
    char maze[NROWS][MCOLS] = {
        "1111000",
        "0111110",
        "0101100",
        "1101100",
        "0111010",
        "010101G"
    };
    
    
    
    
    void display_maze(void);
    int find_path(int x,  int y);
    int main(void)
    {
        int count=0;
        display_maze();
        if ( find_path(0, 0) == TRUE )
            printf("Success!\n");
        else
            printf("Failed\n");
        display_maze();
        printf("You path lenght is: %d\n", count);// used the "count" to sum all the "1" in the path and print the result(path's length)at the end//
        return 0;
    }
    // main()
    void
    display_maze(void)
    {
        int i;
        printf("MAZE:\n");
        for ( i = 0; i < NROWS; i++ )
            printf("%.*s\n", MCOLS, maze[i]);
        printf("\n");
        return;
    }
    int
    find_path(int x, int y)
    {
        int count=0;
        // If x,y is outside maze, return false.
        if ( x < 0 || x > MCOLS - 1 || y < 0 || y > NROWS - 1 ) return FALSE;
        // If x,y is the goal, return true.
        if ( maze[y][x] == 'G' ) return TRUE;
        // If x,y is not open, return false.
        if ( maze[y][x] != '1') return FALSE;
        // Mark x,y part of solution path.
        maze[y][x] = '+';
        // If find_path North of x,y is true, return true.
        if ( find_path(x, y - 1) == TRUE ) 
            {
                count+=1;
                    return TRUE;
        }
        // If find_path East of x,y is true, return true.
        if ( find_path(x + 1, y) == TRUE )
        {
                count+=1;
                    return TRUE;
        }
        // If find_path South of x,y is true, return true.
        if ( find_path(x, y + 1) == TRUE ) 
        {
            count+=1;
                    return TRUE;
        }
        // If find_path West of x,y is true, return true.
        if ( find_path(x - 1, y) == TRUE ) 
        {
                count+=1;
                    return TRUE;
        }
        // Unmark x,y as part of solution path.
        maze[y][x] = 'x';
        return FALSE;
    }
    // find_path()
    I really need a help, I'm not saying to let you write a code for me because the code already done and compiles, just aim me and give me a hint what should I do for what I've required above, thanks in advance and appreciate your cooperative.

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    130
    Well, I'm trying to write/add an algorithm inside my prime code(the code that I have posted in the post) to sum up the "1" along the finding path-as 1 means move on-that my code is already scripted successfully in finding it.
    I put a "count=0" that adds 1 to itself to every "true" attempt which means that he moves on, but it's not working, in the output's sum(length) appears 0 and that's wrong.
    e.g : it should sum up like if the path is (1,1,1,1,1,1), the sum's result : 1+1+1+1+1+1=6.
    Anyone can help me why its not working? any hint? idk really why even the sum's result appears as 0, thanks

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Romyo2
    I put a "count=0" that adds 1 to itself to every "true" attempt which means that he moves on, but it's not working, in the output's sum(length) appears 0 and that's wrong.
    e.g : it should sum up like if the path is (1,1,1,1,1,1), the sum's result : 1+1+1+1+1+1=6.
    Anyone can help me why its not working? any hint? idk really why even the sum's result appears as 0, thanks
    count is a local variable: its lifetime ends when control returns from the find_path function. So, each recursive call to find_path has its own count variable. One way out is to use a pointer parameter:
    Code:
    int
    find_path(int x, int y, int *count)
    {
        /* ... */
        ++*count;
        /* ... */
    }
    Then in the initial call to find_path, in the main function:
    Code:
    find_path(0, 0, &count)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by laserlight View Post
    count is a local variable: its lifetime ends when control returns from the find_path function. So, each recursive call to find_path has its own count variable. One way out is to use a pointer parameter:
    Code:
    int
    find_path(int x, int y, int *count)
    {
        /* ... */
        ++*count;
        /* ... */
    }
    Then in the initial call to find_path, in the main function:
    Code:
    find_path(0, 0, &count)
    I doubt in this case,if all the paths are blocked, the count will be something isn't 0, and that's contrary to what I wanted, if there's a path then sum..by a count, if there's no path then should the sum be 0 or even shouldn't sum, am I right?

  5. #5
    Registered User
    Join Date
    May 2015
    Posts
    130
    And after editing the code as following to what you told me, I got a holly error which is:
    error C2082: redefinition of formal parameter 'count'
    error C2198: 'find_path' : too few arguments for call

    Code:
    #include <stdio.h>
    #define FALSE 0
    #define TRUE 1
    #define NROWS 7
    #define MCOLS 7
    // Symbols:
    // '1' = open
    // '0' = blocked
    // 'S' = start
    // 'G' = goal
    // '+' = path
    // 'x' = bad path
    char maze[NROWS][MCOLS] = {
        "1111000",
        "0111110",
        "0101100",
        "1101100",
        "0111010",
        "010101G"
    };
     
     
     
     
    void display_maze(void);
    int find_path(int x,  int y,int *count);
    int main(void)
    {
        int count=0;
        display_maze();
        if ( find_path(0, 0,&count) == TRUE )
            printf("Success!\n");
        else
            printf("Failed\n");
        display_maze();
        printf("You path lenght is: %d\n", count);// used the "count" to sum all the "1" in the path and print the result(path's length)at the end//
        return 0;
    }
    // main()
    void
    display_maze(void)
    {
        int i;
        printf("MAZE:\n");
        for ( i = 0; i < NROWS; i++ )
            printf("%.*s\n", MCOLS, maze[i]);
        printf("\n");
        return;
    }
    int
    find_path(int x, int y,int *count)
    {
        int count=0;
        // If x,y is outside maze, return false.
        if ( x < 0 || x > MCOLS - 1 || y < 0 || y > NROWS - 1 ) return FALSE;
        // If x,y is the goal, return true.
        if ( maze[y][x] == 'G' ) return TRUE;
        // If x,y is not open, return false.
        if ( maze[y][x] != '1') return FALSE;
        // Mark x,y part of solution path.
        maze[y][x] = '+';
        ++*count;
        // If find_path North of x,y is true, return true.
        if ( find_path(x, y - 1) == TRUE ) return TRUE;
        // If find_path East of x,y is true, return true.
        if ( find_path(x + 1, y) == TRUE )return TRUE;
        // If find_path South of x,y is true, return true.
        if ( find_path(x, y + 1) == TRUE ) return TRUE;
        // If find_path West of x,y is true, return true.
        if ( find_path(x - 1, y) == TRUE ) return TRUE;
        // Unmark x,y as part of solution path.
        maze[y][x] = 'x';
        return FALSE;
    }
    // find_path()
    Any help for please??

    And for another specific required- as I already have made a code for finding a path, how could I develop to show me all the leading path to the exit if possible? (Supposing the input matrix-maze has three paths way leading to its exit, not only one), I'm not making the things go hardly, just trying to get the code more efficient and in the same time as a new programmer to be known of what development steps should be taken to develop your prime code properly.

    thanks
    Last edited by Romyo2; 05-03-2015 at 04:52 PM.

  6. #6
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Quote Originally Posted by Romyo2 View Post
    And after editing the code as following to what you told me, I got a holly error which is:
    error C2082: redefinition of formal parameter 'count'
    error C2198: 'find_path' : too few arguments for call
    Your errors are pretty self explanatory.

    You're still declaring a local count variable at line 53.

    And you haven't updated your calls to find_path() at lines 64,66,68 and 70 to reflect the addition of another parameter.

  7. #7
    Registered User
    Join Date
    May 2015
    Posts
    130
    I have tried before the same thing u're telling but no offence..the same results.

    if u can elaborate by showing me in the code itself..would be appreciated.

  8. #8
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    This must be a windup.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Basically, the OP started with this code -> Simple recursive maze algorithm counting steps - C++ Programming - KnowCoding.com
    And has been getting everyone else to suggest per-line changes to make it work.
    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.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Romyo2
    I doubt in this case,if all the paths are blocked, the count will be something isn't 0, and that's contrary to what I wanted, if there's a path then sum..by a count, if there's no path then should the sum be 0 or even shouldn't sum, am I right?
    I would expect the count to remain 0 if all
    Actually, come to think of it, you probably cannot just have one count variable that is passed around by a pointer since you are branching along different possible paths until the goal is found. You might need to create a count variable for each possible path, then update the count variable from the caller for a path that reaches the goal, or set it to 0 if there is no such path.

    Quote Originally Posted by Romyo2
    And for another specific required- as I already have made a code for finding a path, how could I develop to show me all the leading path to the exit if possible? (Supposing the input matrix-maze has three paths way leading to its exit, not only one), I'm not making the things go hardly, just trying to get the code more efficient and in the same time as a new programmer to be known of what development steps should be taken to develop your prime code properly.
    You might want to check out a currently active thread on a similiar problem: labyrinth(it's dubbed as numeric maze matrix-backtracking).

    At this point, you need to focus on the algorithm: getting a solution and making it efficient starts from there, not from minor tweaks in the code that you have not yet written, at least not until much later when you already have working code.

    Quote Originally Posted by Romyo2
    I have tried before the same thing u're telling but no offence..the same results.
    Like gemera and Salem, I find it hard to imagine that you managed to get the recursion to work without being able to fix the problems even with the feedback from post #6.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by laserlight View Post
    Actually, come to think of it, you probably cannot just have one count variable that is passed around by a pointer since you are branching along different possible paths until the goal is found. You might need to create a count variable for each possible path, then update the count variable from the caller for a path that reaches the goal, or set it to 0 if there is no such path.


    You might want to check out a currently active thread on a similiar problem: labyrinth(it's dubbed as numeric maze matrix-backtracking).

    At this point, you need to focus on the algorithm: getting a solution and making it efficient starts from there, not from minor tweaks in the code that you have not yet written, at least not until much later when you already have working code.


    Like gemera and Salem, I find it hard to imagine that you managed to get the recursion to work without being able to fix the problems even with the feedback from post #6.
    Quote Originally Posted by laserlight View Post
    Actually, come to think of it, you probably cannot just have one count variable that is passed around by a pointer since you are branching along different possible paths until the goal is found. You might need to create a count variable for each possible path, then update the count variable from the caller for a path that reaches the goal, or set it to 0 if there is no such path.


    You might want to check out a currently active thread on a similiar problem: labyrinth(it's dubbed as numeric maze matrix-backtracking).

    At this point, you need to focus on the algorithm: getting a solution and making it efficient starts from there, not from minor tweaks in the code that you have not yet written, at least not until much later when you already have working code.


    Like gemera and Salem, I find it hard to imagine that you managed to get the recursion to work without being able to fix the problems even with the feedback from post #6.
    Well, for the semi and really silly errors for me it's sometimes actually hard to figure as far as if you're a new programmer, and thats why fixing a lil problem might be sometimes a serious problems if the noob programmers didn't notice on his mistakes.
    I've re-edit the code once again, and still getting the same error!! and re-reviewed the code again but got the same result.
    here's the code after edition:
    I tried to write in lines 64 66 68 70 instead of "find_path (x,y)", a "find_path(x,y,*count" but its getting the same error result.
    Code:
    #include <stdio.h>
    #define FALSE 0
    #define TRUE 1
    #define NROWS 7
    #define MCOLS 7
    // Symbols:
    // '1' = open
    // '0' = blocked
    // 'S' = start
    // 'G' = goal
    // '+' = path
    // 'x' = bad path
    char maze[NROWS][MCOLS] = {
        "1111000",
        "0111110",
        "0101100",
        "1101100",
        "0111010",
        "010101G"
    };
     
     
     
     
    void display_maze(void);
    int find_path(int x,  int y,int *count);
    int main(void)
    {
        int count=0;
        display_maze();
        if ( find_path(0, 0,&count) == TRUE )
            printf("Success!\n");
        else
            printf("Failed\n");
        display_maze();
        printf("You path lenght is: %d\n", count);// used the "count" to sum all the "1" in the path and print the result(path's length)at the end//
        return 0;
    }
    // main()
    void
    display_maze(void)
    {
        int i;
        printf("MAZE:\n");
        for ( i = 0; i < NROWS; i++ )
            printf("%.*s\n", MCOLS, maze[i]);
        printf("\n");
        return;
    }
    int
    find_path(int x, int y,int *count)
    {
        // If x,y is outside maze, return false.
        if ( x < 0 || x > MCOLS - 1 || y < 0 || y > NROWS - 1 ) return FALSE;
        // If x,y is the goal, return true.
        if ( maze[y][x] == 'G' ) return TRUE;
        // If x,y is not open, return false.
        if ( maze[y][x] != '1') return FALSE;
        // Mark x,y part of solution path.
        maze[y][x] = '+';
    	++*count;
        // If find_path North of x,y is true, return true.
        if ( find_path(x, y - 1) == TRUE ) return TRUE;
        // If find_path East of x,y is true, return true.
        if ( find_path(x + 1, y) == TRUE )return TRUE;
        // If find_path South of x,y is true, return true.
        if ( find_path(x, y + 1) == TRUE ) return TRUE;
        // If find_path West of x,y is true, return true.
        if ( find_path(x - 1, y) == TRUE ) return TRUE;
        // Unmark x,y as part of solution path.
        maze[y][x] = 'x';
        return FALSE;
    }
    // find_path()
    What should I do exactly for fixing and let it comiples? I'm really stucked in and a push hint would be useful aswell, thanks.

  12. #12
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by gemera View Post
    This must be a windup.
    Lol, really it sounds weird, I tried what you've told even before you informing me, also writing find_path(x,y,count) leads to a more complex error!

  13. #13
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by laserlight View Post
    count is a local variable: its lifetime ends when control returns from the find_path function. So, each recursive call to find_path has its own count variable. One way out is to use a pointer parameter:
    Code:
    int
    find_path(int x, int y, int *count)
    {
        /* ... */
        ++*count;
        /* ... */
    }
    Then in the initial call to find_path, in the main function:
    Code:
    find_path(0, 0, &count)
    I put the count for every true step's iteration, but it gives either a wrong sum's output or just throw 0, can you please instruct me where exactly should I put the count itself? I know if for e.g the path is blocked my put *count=0 etc... but already tried and it gives wrong results, the itself code works generally and the codes that u gave I added them and they are working completely.

Popular pages Recent additions subscribe to a feed