EECE 3220 using queues problem

0 comments

1. Introduction

This assignment gives you experience working with the queue class template we developed in lecture. Your program will model two lanes–an express lane and normal lane–in a grocery store, using a Queue object for each. You’ll read a file telling you when each customer arrives and how long they’ll take, use that information to fill a pair of queues, and then empty each queue to simulate the process of serving customers.

Section 2 provides a specification for the assignment, providing details on how your program should handle input and output. The output specification includes a breakdown of what the auto-graded test cases evaluate. Section 3 lists a few useful hints for completing the program.

2. Specification

Input specification

The only user input to this program is a file name. Your program should open and read the specified file, which always contains 10 input lines representing 10 customers. Each line contains the following information:

  • A single character representing the lane: ‘N’ for normal, ‘E’ for express
  • An unsigned integer representing the customer’s arrival time
    • File contents are sorted in order of arrival time
  • An unsigned integer representing the customer’s service time (how long it takes to serve a customer once they reach the front of the line)

So, for example, the first three lines of the input file file1.txt represent two express lane customers and one normal lane customer arriving at times 2, 7, and 8, with service times 3, 1, and 4:

E 2 3
E 7 1
N 8 4

Output specification and grading rubric

Your program is essentially broken into two parts: reading the file and filling the queues, then emptying the queues to simulate serving customers. The test cases use two separate input files, file1.txt and file2.txt. I’ve collected the full program output based on each input file in a pair of files, file1_output.txt and file2_output.txt, which are provided below as downloadable files. Remember, your program doesn’t print to a file–it prints to the screen!

Reading the file (10 points)

To show that your program reads the file correctly, the program must do the following:

  • Prompt for and read the file name
  • Print a line indicating you’ve started reading the file
  • As each line of the file is read, print a message listing:
    • The customer number
    • The lane to which they’re going
    • The customer’s arrival and service times
  • Print a line indicating you’re done reading the file

For example, here’s the output my program generates while reading file1.txt. The file name on the first line is user input; the program prints everything else:

Enter input file name: file1.txt
Reading file1.txt ...
Customer 1 to normal lane (A = 1, S = 5)
Customer 2 to express lane (A = 2, S = 3)
Customer 3 to express lane (A = 3, S = 2)
Customer 4 to normal lane (A = 5, S = 7)
Customer 5 to express lane (A = 6, S = 1)
Customer 6 to express lane (A = 8, S = 2)
Customer 7 to normal lane (A = 9, S = 4)
Customer 8 to express lane (A = 11, S = 3)
Customer 9 to normal lane (A = 12, S = 3)
Customer 10 to express lane (A = 13, S = 2)
Done reading file1.txt

Serving customers (40 points)

This part of the program is a loop that runs as long as at least one of four conditions is satisfied:

  • You’re serving a customer from the express lane
  • You’re serving a customer from the normal lane
  • There are customers waiting in the express lane
  • There are customers waiting in the normal lane

While those conditions control when the loop ends, your loop also needs a variable representing time, which starts at 1 and increments every loop iteration. Each time through the loop, your program may take at least one of four actions (although it may do none of these), each of which should be indicated by a line of output:

  • Start serving a new customer from the express lane
  • Start serving a new customer from the normal lane
  • Finish serving a customer from the express lane
  • Finish serving a customer from the normal lane

To start serving a new customer from either lane, three conditions have to be satisfied:

  • You’re not serving anyone from that lane
  • There’s at least one customer waiting in that lane
  • The current time is greater than or equal to the arrival time of the customer at the front of the queue for that lane

Each customer’s service time tells you how many cycles you need to spend serving them. Once that time is up, you can treat the current customer as done and you’ll be available to serve a new customer. Note that:

  • You’ll need to track how long the current customer has left
  • A customer with a service time of 1 will start and finish in the same loop iteration
  • If you finish serving a customer in one iteration, the next customer starts in the following iteration.

The output my program generates while serving the customers represented in file1.txt is below:

T = 1: Serving customer 1 in normal lane
T = 2: Serving customer 2 in express lane
T = 4: Done serving customer 2 in express lane
T = 5: Serving customer 3 in express lane
T = 5: Done serving customer 1 in normal lane
T = 6: Serving customer 4 in normal lane
T = 6: Done serving customer 3 in express lane
T = 7: Serving customer 5 in express lane
T = 7: Done serving customer 5 in express lane
T = 8: Serving customer 6 in express lane
T = 9: Done serving customer 6 in express lane
T = 11: Serving customer 8 in express lane
T = 12: Done serving customer 4 in normal lane
T = 13: Serving customer 7 in normal lane
T = 13: Done serving customer 8 in express lane
T = 14: Serving customer 10 in express lane
T = 15: Done serving customer 10 in express lane
T = 16: Done serving customer 7 in normal lane
T = 17: Serving customer 9 in normal lane
T = 19: Done serving customer 9 in normal lane
Done serving all customers

3. Hints

The template files I’ve provided contain some hints, particularly the main program. Additional hints are here:

Items in queue

You can use the array-based queue template we designed in class (or, if you’re feeling unnecessarily ambitious, redesign it), which is provided below. You do need some way of representing the data stored in each queue entry, though. I used the QItem class that’s also provided with the assignment, which stores each customer’s number, arrival time, and service time. I’ll let you decide what functions are necessary for your solution; feel free to redefine that class or completely ignore it.

File handling hints

We talked briefly about file I/O in Lecture 2, covering ifstream objects for input and noting that you can use an ifstream the same way you use cin.

While there’s an eof() function for ifstream objects, you don’t necessarily need it. A statement using the input extraction operator >> returns false if it fails to read anything, which you can use to your advantage. If myfile.txt contains a list of integers, the code below will read all of those values:

int main() {
    ifstream in1;      // Input file stream
    int inval;              // Input value

    in1.open("myfile.txt");
    while (in1 >> inval) {
        // do something to process input
    }
    in1.close();
    return 0;
}

Customer service hints

Did you carefully read the description of the customer service process, particularly the short lists that describe:

  • The conditions under which you continue to handle customers?
  • The actions this loop might take each time you go through?
  • The conditions under which you start handling a new customer?
  • The conditions under which you finish handling a new customer?

If not, you might want to go back and take some detailed notes on that section. Those points come directly from my solution so, while you’re absolutely welcome to solve this problem any way you want, do know that the description’s based on a working program.

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}