Thread: Really hard recursion with boxes, help?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    29

    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

    Code:
    int find_boxes(int a, int& t, int pre);
    void count_boxes(int n);
    int boxes;
    int main()
    {
        int a;
        char c;
        
        cout << "How many unnumbered boxes do you see? - " ;
        cin >> a;
        boxes = a; 
        count_boxes(a);
        cout<< "There are: " << boxes << " boxes in the room.\n";
        cin >> c;
               
    }
    
    int find_boxes (int ans, int& total, int pre){
         int x;
         if(ans == 0)
                return total;
         while (ans != 0){   
               cout << "How many do you see in box " << pre << "." << ans << ": ";
               cin >> x; 
               total += x;
               return find_boxes(x, total, pre+1);
               ans --;
               }
         return total;
    }
    
    void count_boxes(int n){
         for ( int i = 1; i <= n; i++){
             find_boxes(n-i, boxes, i);
             }
    }
    Last edited by aciarlillo; 10-12-2005 at 02:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting data from a laptop hard drive
    By DavidP in forum Tech Board
    Replies: 6
    Last Post: 06-13-2009, 07:02 AM
  2. Template Recursion Pickle
    By SevenThunders in forum C++ Programming
    Replies: 20
    Last Post: 02-05-2009, 09:45 PM
  3. Detect SCSI Hard Drive Serial Number
    By mercury529 in forum Windows Programming
    Replies: 3
    Last Post: 10-17-2006, 06:23 PM
  4. Replies: 2
    Last Post: 07-06-2005, 07:11 PM
  5. a simple recursion question
    By tetra in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2002, 10:56 AM