Really hard recursion with boxes, help?

This is a discussion on Really hard recursion with boxes, help? within the C++ Programming forums, part of the General Programming Boards category; The problem is to use recursion to count how many boxes are in a room. The trick is each box ...

Really hard recursion with boxes, help?

The problem is to use recursion to count how many boxes are in a room. The trick is each box can have boxes inside of it and each one of those can have even more inside of it. It is supposed to begin by asking "How many unnumbered boxes do you see? " you input the number. Then for each of these it asks how many you see inside i.e. (How many do you see in box #1?" Then for each of the ones inside it will ask "How many do you see inside 1.1? .... 1.2? .... 1.3" and so on. I have tried my best at this but I think it is just outside my understanding of recursion so far, especially keeping track of 1.1 1.2 1.1.1 1.2.1 etc etc. I have some code posted below that can so far count the number of boxes within 1 box but thats as far as I can go. We are allowed to use functions but I do not see where this will help. Any suggestions or pushes in the right direction would be appreciated.
Thanks
-a

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell

So basically before 'a' can increment from zero to one it has to loop through 'b' from zero to five first. It is important to understand the way this operates since this is how your recursive function will work- in much the same way.

Ok, so how does that help with your problem?
Well the best way to approach this would be to draw a sample diagram. (A picture is worth a thousand words) A tree diagram serves as the best example.

In the diagram below, let each folder be representative of a box.
The question is how would we recursively search all the boxes?

Well, think back to the nested for loop and remember this rule:-

"As soon as you encounter another level of nesting you immediately, search that level. Once you have finished searching that entire level you go back to the preceding level."

Ok so given that rule, how might we search the boxes in the diagram below?

Code:

how many boxes in the room>>2
how many in box 1>> 2
how many in box 1.1>> 0
how many in box 1.2>>1
how many in box 1.2.1>>0
how many in box 2>> 0
End program

That would be how. Notice that before box 2 is searched all the sub-folders of box 1 has to be searched first.

I have no intention of doing your homework for you. The program is not complete. You have to think of a way to make it display the numbers for the boxes properly. For example, box 1.2.1. Other than that the code behaves as expected.

One other thing, notice that you don't need to 'return' anything to main. You simply pass the variable back into the recursive function. I hope this helps.

Ps. An example of a tree diagram can be found if you go to the 'command prompt' and type tree.