Question

Write a C++ computer program that simulates the action of an elevator that is out of...

Write a C++ computer program that simulates the action of an elevator that is out of order. After simulating the motion of the elevator, your program will display a bar chart that shows the number of times the elevator stays on each floor of a twenty-five-story building.

When you arrive on the scene to begin studying the elevator, it is on the twelfth floor of the twenty-five-story building. Every five minutes the elevator either moves up one floor or down one floor. There is the same chance that the elevator will move up as move down, it is totally random. Keep track of the number of times the elevator stays at each of the twenty-five floors and display that information as a bar chart using a character like an asterisk to represent each time the elevator stays on a particular floor.

For example, our elevator starts on the twelfth floor and moves up to the thirteenth floor, five minutes later it moves up to the fourteenth floor and after another five minutes it moves back down to the thirteenth floor. A portion of a bar chart showing the number of visits the elevator made to each floor might look like this:

Floor Number of Visits
----- -----------------------
   17
   16
   15
   14   *
   13   **
   12   *
   11
   10

This is only a portion of the output your program will produce. Your Bar Chart will include all 25 floors from 25 through 1. Your output will display the top floor (25) at the top and the bottom floor (1) at the bottom to properly represent the floors in the building.

Observe the elevator’s behavior for a number of hours that is entered by your user. Do not forget to validate your user’s input. Your bar chart will show how many times the elevator visited each floor.

NOTES:

  • The elevator cannot go above the twenty fifth floor or below the first floor, the ground floor.
  • Your user cannot observe for more than ten (10) hours.
  • The elevator can only go up or down one floor at a time.

This assignment allows you to use an array, generate random numbers, interact with your user, validate input, control processing with repetition structures, and format output.

In order to receive full credit for this assignment:

  • Use the correct naming convention for the file you submit.
  • Submit a program that executes correctly.
  • Include meaningful comments.

Suggested Grading Criteria:

  • Include a comment that contains your full name (5 points)
  • Include additional meaningful comments (10 points)
  • Use an array to correctly keep track of the number of visits (20 points)
  • Validate the user input (10 points)
  • Display a bar chart showing the number of visits to all 25 floors of the building. (25 points)
  • Provide clear and easy to follow directions. (10 points)
  • Use a repetition structure like a “for” loop. (10 points)

Example output:

Floor Number of Visits

----- ---------------------------

   25

   24

   23

   22

   21

   20

   19

   18 ****

   17 ******

   16 *********

   15 ***************

   14 *********************

   13 ***********************

   12 **********************

   11 ****************

   10 *****

    9

    8

    7

    6

    5

    4

    3

    2

    1

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

Cpp code:

// Header files
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;

int main()
{
/*constants variable declarations*/
const int MAX_FLOORS = 25;
const int MAX_OBSERVE_HOURS = 10;
const int MINUTES_PER_HOUR = 60;
const int MINUTES_TO_STAY_ON_FLOOR = 5;

/*setting seed to generate random numbers*/
srand(time(NULL));

/*create an array for floors*/
int floors[MAX_FLOORS + 1];

/*initializing floors with 0*/
for (int i = 0; i < MAX_FLOORS; i++)
{
floors[i] = 0;
}

/*variable declarations*/
int hours;
int minutes;
int Move;
int currFloor;

/*prompting the user to enter the number of hours to observe the elevator's behavior.*/
cout << "Enter the number of hours to observe the elevator’s behaviour: ";
cin >> hours;

/*validate the user input*/
while (hours < 0 || hours > MAX_OBSERVE_HOURS)
{
/*again taking the user input*/
cout << "Range of hours to observe is [1-" << MAX_OBSERVE_HOURS << "]. Try again!" << endl;
cout << "Enter the number of hours to observe the elevator’s behaviour: ";
cin >> hours;
}

currFloor = 12; // starting from 12th floor
floors[currFloor]++;
minutes = 0;

while(minutes < hours * MINUTES_PER_HOUR)
{
//generate a random number either 0 or 1
Move = rand() % 2;

if (Move == 0 && currFloor > 1)
{
// move to the down floor if current floor is greater than the 1st floor
currFloor--;

// increment the number of visits
floors[currFloor]++;

// stay 5 minutes at the current floor
minutes += MINUTES_TO_STAY_ON_FLOOR;
}
else if (Move == 1 && currFloor < 25)
{
// Move to the up floor if current floor is less than the 25th floor
currFloor++;

// increment the number of visits
floors[currFloor]++;

// stay 5 minutes at the current floor
minutes += MINUTES_TO_STAY_ON_FLOOR;
}
}

// display a bar chart showing the number of visits to all 25 floors of the building
cout << endl;
cout << "Floor Number of Visits" << endl;
cout << "----- ---------------------------" << endl;
for (currFloor = MAX_FLOORS; currFloor > 0; currFloor--)
{
cout << right << setw(5) << currFloor << " " << left;
for (int i = 0; i < floors[currFloor-1]; i++)
{
cout << "*";
}
cout << endl;
}
return 0;
}

Images of code:

Output:

Add a comment
Know the answer?
Add Answer to:
Write a C++ computer program that simulates the action of an elevator that is out of...
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
  • Write a C++ program that simulates a lottery game. Your program should use functions and arrays....

    Write a C++ program that simulates a lottery game. Your program should use functions and arrays. Define two global constants: - ARRAY_SIZE that stores the number of drawn numbers (for example 5) -MAX_RANGE that stores the highest value of the numbers ( for example 9 ) The program will use an array of five integers named lottery, and should generate a random number in the range of 0 through 9 for each element of the array. The user should enter...

  • Write a complete Java program with methods that prompt user for the number of floors, rooms,...

    Write a complete Java program with methods that prompt user for the number of floors, rooms, occupied rooms in a hotel. You must validate floors, rooms, occupied rooms. Compute vacant rooms, occupancy rate on each floor and display rooms, occupied rooms, vacant rooms and occupancy rate for each floor. Use the given method names. See validation rules below: 1. getFloors(). This method prompts user for number of floors in a hotel and returns floors to the caller. 2. testFloors(floors). Do...

  • Using Microsoft Visual Studio 2017 C++, write a C++ program to calculate to analyze the data...

    Using Microsoft Visual Studio 2017 C++, write a C++ program to calculate to analyze the data from the number of visitors to Sleeping Bear Dunes in Michigan in 2015. Requirements Read the data from the Months.txt and Visitors.txt files. The text files are provided for you to download. The data was obtained from the National Park Service website. Design the program so the user enters a menu item and the following is displayed Display the chart below Totals and display...

  • Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program...

    Using C++ create a lotto program Lottery Design a program that simulates a lottery. Requirements:  The program should have an array of 5 integers named lottery and should generate a random number in the range of 1 through 99 for each element of the array. The user should enter five digits, which should be stored in an integer array named user. The program is to compare the corresponding elements in the two arrays and keep a count of the digits that...

  • Solve it for java Question Remember: You will need to read this assignment many times to...

    Solve it for java Question Remember: You will need to read this assignment many times to understand all the details of the you need to write. program Goal: The purp0se of this assignment is to write a Java program that models an elevator, where the elevator itself is a stack of people on the elevator and people wait in queues on each floor to get on the elevator. Scenario: A hospital in a block of old buildings has a nearly-antique...

  • This is a repost of my question from before. I am having trouble writting the code...

    This is a repost of my question from before. I am having trouble writting the code for this project. Below is the project description with various methods and classes the code in C++ should implement. Project Description The goal of this project is to design and develop C++ code and algorithms to control a bank of elevators, which services many floors and transports people “efficiently ", that is, as per the given specifications. A second goal is to effectively employ...

  • Write a C+OOP program that simulates a vending machine. The program should define at least two...

    Write a C+OOP program that simulates a vending machine. The program should define at least two classes. Less than two classes will result in 20 points deduction. (30 points) One must be general vending machine (50 points) The other can be either vending machine of soda, or vending machine of snack. It must be a subclass of above general machine These vending machines should have following components and functions e Coin slot o Allow adding coins o Display total coin...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • Java BasketBall Bar Chart Write a program that allows you to create a bar chart for...

    Java BasketBall Bar Chart Write a program that allows you to create a bar chart for the active members of a basketball team during a game. (Recall: there are only 5 active players on the court per team in a standard basketball game.) Your program should do the following tasks: 1) Prompt the user to store the first name of the five players 2) Prompt the user to enter the points scored by each player a. Use a do...while loop...

  • Please post the code in c++! Computer Labs Write a Computer Labs program to store the...

    Please post the code in c++! Computer Labs Write a Computer Labs program to store the list of user IDs for each computer station using a linked list. Problem Description: You run four computer labs. Each lab contains computer stations that are numbered as shown in the table below: Lab Number Computer station Numbers 1-5 2 1-6 1-4 4 1-3 Each user has a unique five-digit ID number. Whenever a user logs on, the User's ID, labnumber, and the computer...

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