4
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by bgbnbigben
0

> if (f == 1||3)
This doesn't do what you think it does.
It's actually if (f == 1 || 3 != 0 ) The latter being trivially true all the time.

Second, a DFS search may be simplified by using a recursive function. I notice that you just have a sprawling mass in main().
Each loop which performs a specific task (read your comments) could easily be made into a separate function.

Half a dozen functions, each at most 20 lines long would allow you to focus on specific details of what works and what doesn't.

Third, if you're learning C++ then drop all the 'C' methods for reading files - fopen, fscanf etc.

0

yeah, i know what you mean about dropping the c habits
just a bad habit that WILL be fixed.....

It's actually
if (f == 1 || 3 != 0 )
The latter being trivially true all the time.

actually, the f will always start at 1, making it true.
it then runs through all the input untill it finds a 0, at which point it ends.
the
if == 1 || 3 means, in plain english, if f is equal to one or three, which it will be, twice. then it should run until it comes across a zero, at which point its supposed to backtrack.
But forgive me if that was/sounded condescending/ungrateful/i've missed the point entirely.

Now, about a recursive version...i dont actually know what the difference between the iterated and recursive versions are. in fact, i dont even know how to implement the iterated version, let alone the recursive one.
True, the sprawling mass can be recreated into functions.
but this isnt me trying to be a genius for a programming class, its just throwaway informatics. i dont want to spend too long programming this easy to read, easy to modify masterpiece so i can just shelf it once i submit it.
although my code is far from easy to read and a masterpiece......

Thanks for the help, though.

0

I think you might be missing the point 'The latter being trivially true all the time.' i.e. you have to write the if statement like: if (f == 1 || f == 3) If you write it like:

if (f == 1||3)
// or
if (f == 1 || 3 != 0 )

then the condition will always be true, regardless of the f's value, because 3 simply is not equal to 0.

0

the
if == 1 || 3 means, in plain english, if f is equal to one or three, which it will be, twice. then it should run until it comes across a zero ...

Always true as Salem already stated. Because 3 != false is true, || 3 always evaluates true! You don' t believe that? try if(false||3) cout "???" and if(true||3) cout "???". What do you see? Possibly you want this: if (f==1 || f == 3) ...

0

but either way, that wont solve the recursive/iterated dfs. i can now admit that it may help with the forward half of the search, but a DFS is supposed to backtrack. I can not get it to backtrack. I can easily manage a BFS, because thats always a LIFO queue that you put vertices into. But i cant manage a working FIFO list.
Im not worried about the syntax atm, because i can always fix that if i stuff up later. But i cant fix the DFS because i can not create it.

This article has been dead for over six months. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.