Question

[C++] Write an interactive program to help you count all of the boxes in a room....

[C++] Write an interactive program to help you count all of the boxes in a room. The program should begin by asking something like

How many unnumbered boxes can you see? Then the program will have you number those boxes from 1 to m, where m is your answer. But, remember that each box might have smaller boxes inside, so once the program knows you can see m boxes, it should ask you to open box number 1 and take out any box- es you find, numbering those boxes 1.1, 1.2, and so on. It will also ask you to open box number 2 and take out any boxes you find there, numbering those boxes 2.1, 2.2, and so on. This continues for box 3, 4, up to m. And, of course, each time you number a box 1.1 or 3.8 or something similar, that box might have more boxes inside. Boxes that reside inside of 3.8 would be numbered 3.8.1, 3.8.2, and so on. At the end, the program should print a single number tell- ing you the total number of boxes in the room.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

This problem can be solved using a recursive approach.The base case here will be to open a box which does not contain any other box. So the approach is to recursively iterate each box until we find an empty box. While iterating keep track of the count and return it to the previous function call. I have shared the code below -

#include <iostream>
#include <string>

using namespace std;

// child variable is the box to open
// parent variable keeps track of the previous boxes opened
int open(string child, string parent)
{
    int count;
    if (child == "")
    {
        cout << "How many un-numbered boxes do you see in the room? ";
    }
    else
    {
        cout << "Open box " << (parent + child).substr(1) << " How many do you see? " << endl;
    }
    cin >> count;
    // base case
    // if there are no more boxes then return 0
    if (count == 0)
    {
        return 0;
    }
    else
    {
        // recursive step
        // for each choice of boxes
        int sum = 0;
        for (int i = 1; i <= count; i++)
        {
            // explore the boxes inside
            sum += open(to_string(i), parent + child + ".") + 1;
        }
        // return the sum
        return sum;
    }
}

int main()
{
    int total = 0;
    total = open("", "");
    cout << "Total number of boxes: " << total << endl;
    return 0;
}


I am also sharing the screenshot of the code for readability and the screenshot of the code execution.

Add a comment
Know the answer?
Add Answer to:
[C++] Write an interactive program to help you count all of the boxes in a room....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java question! Ask the user for the number of boxes to use (N). These boxes will...

    Java question! Ask the user for the number of boxes to use (N). These boxes will be represented in our program as an array. Each box has only two states – open or closed. So make this an array of Boolean, true = closed, false = open. Start with all boxes closed. Begin with box #2 (remember this is index 1 in the list) and open it and every 2nd box. Now starting with box 3, and continuing every 3rd...

  • Due: in class Tuesday, 3/27/2018 (upload electronic copy by 9:00am) Problem: You will write a program...

    Due: in class Tuesday, 3/27/2018 (upload electronic copy by 9:00am) Problem: You will write a program to compute some statistics about boxes of a popular breakfast cereal called "Chocolate Frosted Sugar Bombs" manufactured by the General Junkfoods Corporation. Automated machinery is used at the company's factory to fill individual boxes with cereal. No machine is perfect, so the amount of cereal actually in a box will vary slightly from box to box. The data file CFSB.txt on the class website...

  • Write a program to pack boxes with blobs. Write two classes and a driver program. A...

    Write a program to pack boxes with blobs. Write two classes and a driver program. A Blob class implements the following interface. (Defines the following methods.) public interface BlobInterface { /** getWeight accessor method. @return the weight of the blob as a double */ public double getWeight(); /** toString method @return a String showing the weight of the Blob */ public String toString(); } A Blob must be between 1 and 4 pounds, with fractional weights allowed. A default constructor...

  • Write a java program that demonstrates recursion. This program can be in a java GUI frame...

    Write a java program that demonstrates recursion. This program can be in a java GUI frame or as a console application. On this one it is up to you. (It would probably work better as a console program for this particular program.) The input for this program is a number of boxes. Ask the user for this with a textbox or a prompt on the command line. Demonstrate recursion with a function called LoadTruck() that takes a number of boxes...

  • USING C LANGUAGE Write a program that computes the total weight of a cargo. Ask the...

    USING C LANGUAGE Write a program that computes the total weight of a cargo. Ask the user to enter multiple inputs. For each input, the user indicate the weight and quantity of the boxes. The program computes and prints the total cargo weight The output should match the sample below. Make sure you print the input number (Input #1, Input #2,) 3 - Input # 1. Weight of the box (lbs): 4 Enter quantity: 2 Input #2. Weight of the...

  • .Need code for my java class !! Write a simple java program that uses loops. You...

    .Need code for my java class !! Write a simple java program that uses loops. You may use ‘WHILE’ or ‘FOR’ or ‘DO-WHILE’ – you decide. The program should use a loop to compute the semester average for up to 5 students. In the loop the user will ask for a student name and their 3 test scores and add them up and divide the total by 3 giving the semester average. You will print out the student name and...

  • c++ please Assignment Write a program that calculates the quality of a home composed of three...

    c++ please Assignment Write a program that calculates the quality of a home composed of three rooms. For each room, the user will give the width and depth information as two doubles, the unusable amount of space within the room as a double, the fact that the door opens inward or outward as a boolean (1 means inward), and the number of windows as an integer. Based on this information the program should calculate the total usable size of the...

  • please write this program in C++(Linux). Also write all the explanation of codes, the comment for...

    please write this program in C++(Linux). Also write all the explanation of codes, the comment for your code. Description You are an avid reader and have decided that you would like to keep track of the books that you read. You will define a structure called book_list that will hold 1. a number associated with each book (int) 2. book title (string), 3. author (string), 4. a description of the book (string), 5. a date when the book was read...

  • C Programming For this task, you will have to write a program that will prompt the...

    C Programming For this task, you will have to write a program that will prompt the user for a number N. Then, you will have to prompt the user for N numbers, and print then print the sum of the numbers. For this task, you have to create and use a function with the following signature: int sum(int* arr, int n); Your code should look something like this (you can start with this as a template: int sum(int* arr, int...

  • How would i code this in MatLab 3. Write the program, openclasses.m, where you start by...

    How would i code this in MatLab 3. Write the program, openclasses.m, where you start by creating a structured array called class which contains the information in the following table (note: seats should be a single fieldname with a (1x2) array inside it). Class Title Introduction to Engineering Computers in Psychology Writing and Rhetoric Beginning Spanish Introduction to Africana Studies Number Seats (Max, Enrolled) 10111 (45, 32) 20000 (28, 25) 12100 (14, 14) 10101 (19,13) (36, 32) 20082 From this...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT