Answered step by step
Verified Expert Solution
Question
1 Approved Answer
CS 241: Advanced Object-Oriented Programming using C++ Spring 2021 Homework 4 A school building has two floors with equal number of classrooms in each floor.
CS 241: Advanced Object-Oriented Programming using C++ Spring 2021 Homework 4 A school building has two floors with equal number of classrooms in each floor. On a Saturday, the principle went to the school to finish some work and he took his son with him. Being bored, the son decided to entertain himself by going around the building and checking each classroom door. If the door is closed, he opens it and if it is open he closes it. To make it more exciting for himself, he decided to do this game in several rounds and change the status of certain doors each round. Here is how he played the game: - For odd rounds (rounds 1, 3, 5, etc.) he visits the first floor only and for even rounds, he visits the second floor only. In each round, he doesn't visit all the doors on the floors he is visiting. He skips some doors and visits other doors depending on the round number. For example: In round 1 (odd round number), he visits the first floor and skips 1 door and visits door next to it and so until he finishes the floor. For example, he skips door 1 and visits door 2 then skips door 3 and visits door 4 and so on. In other words, in round 1, he visits every other door in floor 1. In round 2 (even round number), he visits the second floor and skips 2 doors and visits the third. For example, he skips doors 1 and 2 and visits door 3 then skips doors 4 and 5 and visits door 6 and so on. This means, in round 2, he visits every second door in floor 2. In round 3 (odd round number), he visits the first floor and skips 3 doors and visits the fourth. For example, he skips doors 1, 2, and 3 and visits door 4 then skips doors 5, 6, and 7 and visits door & and so on until he finishes the floor. In other words, he visits every third door in floor 1. Let's assume all the doors are closed when he starts. Write a C++ program that simulates this game. The program should do the following: 1- Ask the user to enter the number of classrooms per floor. 2- Ask the user for the number of rounds in the game. 3. Once this data is entered, the program should simulate the game for the number of requested rounds then displays the total number of doors that will remain open at the end of the game and list them in each floor. 4- The program should ask the user if they want to play again. If the answer is yes, then it repeats again by asking for the parameters of the new game (number of classrooms and rounds). Here is an example of how the program should look like when it runs if the user enters 30 classrooms per floor and 5 rounds. How many classrooms in each floor?: If the user enters 30 in response to this question, the program asks for the number of rounds. How many classrooms in each floor: 38 How many rounds in this gane?: If the user enters 5 in response to this question, the program will run for 5 rounds then displays the following at the end of the game. How many classrooms in each floor?: 30 How many rounds in this game?: 5 At the end of round 5 the total number of open doors is 19 (7 in floori and 12 in floor2) The open doors are: First Floor: Door2 Doorle Door12 Door14 Door22 Door24 Door26 Second Floor: Door Doors Doors Doors Doorle Door12 Door18 Doorze Door21 Door24 Door25 Door27 Play the gane again (Y/N)? If the user answers Y or y in response to the play again question, the game restarts again by asking for the number of classrooms and the number of rounds. Any other input should terminate the program. Programming Requirements and Guidelines: Use pointers to dynamically allocate memory to store the status of each classroom door in each floor based on the number of classrooms provided by the user during program execution (runtime). Do not declare static arrays instead of using pointers and dynamic memory allocation. Include data validation for the values entered by the user. For example, your program should not accept 0 or any negative value for the number of classrooms or the number of game rounds. Design your program to be modular by having functions that perform various tasks and call these functions from the main. Each time you run the game, you should start round 1 with all the doors closed. This can be done by calling a function called closeAllDoors() which will reset the status of all doors to "closed". You may also include the following functions: o visitFloor(): This function will simulate visiting the classroom doors in a specific floor according to the rules mentioned above. The input to this function could be a pointer to the floor classroom doors array for the floor being visited as well as the round number so that it can decide which doors to visit on that specific floor. You may also want to pass the number of classrooms in the floor. For each door visited, if the door is open, it will close it and if it is closed it will open it. o getNumberOfOpenDoors(): This function counts the number of open door in a specific floor and returns this number. The input to this function could
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started