Introduction to Queues and Simulations
- Due Oct 17, 2019 by 1pm
- Points 0
- Submitting a file upload
- File Types cpp
Introduction
This lab is designed to introduce you to a some code that you will build on for your first class project. The code runs a simulation of a checkout at a retail store. The first thing you should do is download the checkout.cpp, conio.h, and conio.cpp files and put them in a separate directory. Read through the code and familiarize yourself with what it is doing and how it works.
Compiling the code
This is the first example of a program that has code written in multiple source files. The main program is written in checkout.cpp, but it uses the conio library to print out things nicely on the screen. In the last lab, you used the libsodium library that was already installed on the system, but this time you will have to compile the code since it isn't installed on the system.
The conio library uses a file called conio.h to provide the compiler with a list of function prototypes and constant values that can be used by any other file. If that file uses
#include "conio.h"
it will have the ability to call those functions and use those values even though they don't have implementation of those functions.
With multiple source files, you still need to compile them, but will need multiple steps to create the final executable. The first step is to create an object file for each source file. An object file stores all the machine instructions for a source file, but does not include machine instructions for other source files. On UNIX, an object file typically has a .o suffix and is created by using the -c flag for gcc. For example,
g++ -c main.cpp
creates the object file main.o . You should do something similar to create an object file for checkout.cpp and conio.cpp .
Now that you have two object files (main.o and conio.o), you need to combine all the machine instructions into a single executable file. This is done by providing g++ with both object files and telling it the name of the executable file:
g++ -o checkout checkout.o conio.o
In the future, if you update a source file, you will need to rebuild its corresponding object file to incorporate the new code you wrote. You will also need to rebuild the final executable to incorporate the new machine instructions from your object file.
Running the program
You should run the program several times to explore how it runs and what it is displaying while it does. The program takes one second of real time to simulate one minute in the simulation, so when it asks how long to run the simulation, you should pick a reasonable amount of time (e.g., 60 simulated minutes takes 60 seconds = 1 minute of your time). Try giving the program different values to see how that changes the code execution.
Updating the program
Your task for this lab is to add a second checkout register to the simulation. You need to figure out what code is necessary to add this second register so that it
- Displays the register properly. You may need to change where other values are output on the screen to do this well.
- Interacts with the queue of customers. If there is a customer waiting, the second register should not sit idle, but instead begin to serve that customer.
- Updates the statistics properly. When the second register is done with a customer, the statistics should be maintained correctly.
After you get these three pieces of functionality working, you should make your second register adaptive. Instead of having the second register available at all times, you should wait to open the second register until one or more of the customers is "angry" (appear red in the simulation). The register should also close again when everyone is "happy" again (appear green in the simulation).
When you have completed these tasks, you can submit your checkout.cpp file. We have our own copies of conio.h and conio.cpp so you do not need to submit them. This also means that you should not be modifying either of those files because we will not be able to use those modifications.
Begin Project #0
Once you have submitted your code for this lab, you can begin work on the first project of the course, which entails using the checkout simulator to investigate the best way to build a customer queue at a retail location.