![]() |
|
#1
|
|||
|
|||
A problem with recursionI've written a program which get's a matrix 10*10 with values of 1 or 0.0 means we can travel to that point and 1 means it's an opsticale.we can move forward,backward,down or up assuming the values of those places are 0's.
the function checks if there's a path between the starting point and the finish point. this is the function: CPP / C++ / C Code:
and the main function: CPP / C++ / C Code:
here's the input Code:
how can I fix it and what causes this error? |
|||
|
#2
|
|||
|
|||
Re: a problem with recurrsionFor starters, I would put some logging/debug messages in your function so that you can see what it is doing and what step it crashes on. Also, something to think about is that you could be in an endless loop. Maybe you should pass some sort of path (collection...could be a dynamic array and a count) so that you do not go to the same location more than once. Here are my thoughts:
CPP / C++ / C Code:
|
|
#3
|
|||
|
|||
Re: a problem with recurrsionQuote:
You can instrument the function to see what it is seeing and to see how deep the recursion gets before the program aborts (due to overflow of stack): CPP / C++ / C Code:
Regards, Dave |
|
#4
|
|||
|
|||
Re: a problem with recurrsionthanks to your advice I've figuared whats the problem.it appears that the function makes an endless loop because it moves between 89 to 99 over and over.The solution must be to mark the places I've already been to but the problem is that I may need to return back.
So I have no idea what to do.any help? |
|
#5
|
|||
|
|||
Re: A problem with recursionWhy would you ever need to return back to a place that you have already been?
|
|
#6
|
|||
|
|||
Re: A problem with recursionbecause if I am found in a point with several availiable directions and I choose one with no end I need to return back and choose a different path.
|
|
#7
|
|||
|
|||
Re: A problem with recursionDid you read the first post that I made on here? Let me explain it a little bit:
When you go to a point, the function FindPath() branches in ALL FOUR DIRECTIONS assuming that a path has not been found yet. This is what makes recursion so powerful. You can simply tell it to find the path and then you know that it will try every combination of moves to get there until it finds it because you know that it will go in every direction. You also know that it does not need to go back to a previous point because, if it has already been there, it will have gone in all directions from that point. Here is the process: 1) Enter FindPath() with the current point 2) If we are at the end point, then return the path. 3) Try FindPath() up if it is not already on our current path. If it returns a valid path, return it. 4) Try FindPath() down if it is not already on our current path. If it returns a valid path, return it. 5) Try FindPath() right if it is not already on our current path. If it returns a valid path, return it. 6) Try FindPath() left if it is not already on our current path. If it returns a valid path, return it. 7) Since we never found a valid path in any of our directions, return NULL. Does this make sense to you? |
|
#8
|
|||
|
|||
Re: A problem with recursionso what changes should I make in the code which I've written?
|
|
#9
|
|||
|
|||
Re: A problem with recursionTry to do what I have shown you. Then, when you think you have something that needs a little tweaking, post your code. It might be easier for you to comment out your code and start from scratch. Look at my first post. I don't think I can make it any more clear without writing the entire program for you.
|
Recent GIDBlog
Problems with the Navy (Officers) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Torrents Download Problem | chandeep | Computer Software Forum - Linux | 7 | 09-Oct-2006 22:37 |
| Graphic problem in Unreal Tournament 2004 | zerox | Computer Software Forum - Games | 10 | 09-Oct-2005 12:31 |
| Runtime Problem involving "printf" in C Program | supamakia | C Programming Language | 2 | 09-Oct-2005 10:09 |
| a significant problem after installing Xp | mohammad | Computer Software Forum - Windows | 10 | 09-Aug-2005 07:03 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The