title: Multi Server Queue Simulation C++
body: <p>This assignment (I've linked the full description <a href="https://web.stcloudstate.edu/bajulstrom/cs301/projects/p7f21.html" rel="nofollow noreferrer">here</a>) asks us to create a multi server queue simulation (i.e. grocery store type scenario). My code seems to be working for the most part however I currently have 2 questions pertaining to my output that I am having a hard time answering.</p>
  <ol>
  <li><p>Why does queue_total not match the actual number of elements displayed in each queue at the end of the program?</p>
  </li>
  <li><p>Why won't new items be enqueued to the shortest queue even though the shortest_queue function seems to be working?</p>
  </li>
  </ol>
  <p>The main code:</p>
  <pre><code>#include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &quot;queue_2.h&quot;
  using namespace std;
  
  
  // Utility function to return shortest queue.
  int shortest_queue(Queue line[], int queuecount)
  {
      int shortest = 0;
  
      // Condition 1: If a queue is empty 
      // with no active transactions, return it.
      for (int i = 0; i &lt; queuecount; i++) {
          if (line[i].empty() &amp;&amp; line[i].get_trans_time() == 0)
              return i;
      }
  
      // Condition 2: If a queue is simply empty, 
      // return it next.
      for (int i = 0; i &lt; queuecount; i++) {
          if(line[i].empty()) {
              return i;
          }
      }
  
      // Condition 3: (otherwise) If all queues are
      // occupied check for shortest queue.
      for (int i = 1; i &lt; queuecount; i++) {
          if(line[i].size() &lt; line[shortest].size())
              shortest = i;
      }
      return shortest;
  }
  
  // Utility function to return total number 
  // of customers waiting in queues.
  int queue_total(Queue q[], int queuecount)
  {
      int custcount = 0;
      for (int i = 0; i &lt; queuecount; ++i)
          custcount += q[i].size();
      return custcount;
  }
  
  int main()
  {
      // Variable decelrations
      int trans_time = 0;
      int count = 0;
      int entry_time;
      int wait_sum = 0;
      int wait_time = 0;
      int seed = 3;
      int ariv_prob = 80;
      int MAX_TRANS_TIME = 12;
      int DURATION = 120;
      int queuecount = 4;
      int shortline;
      int temp;
  
      // Create a list of queues
      // and random number generator
      Queue line[queuecount];
      srand(seed);
  
      for (int time = 1; time &lt; DURATION + 1; time++)
      {
          // Display the time
          cout &lt;&lt; &quot;Time: &quot; &lt;&lt; time &lt;&lt; endl;
  
          // Check probablity only once per iteration
          if ( rand() % 100 &lt; ariv_prob ) {
              shortline = shortest_queue(line, queuecount);
              line[shortline].enqueue(time);
          }
  
          // Update the transaction times for
          // the corresponding queues and compute 
          // summary statistics.
          for (int i = 0; i &lt; queuecount; i++) {
              if ( line[i].get_trans_time() == 0 ) {
                  if ( !line[i].empty() ) {
                      entry_time = line[i].print_front();
                      line[i].dequeue();
                      temp = (time - entry_time);
  
                      if(temp &gt; wait_time)
                          wait_time = temp;
  
                      wait_sum += (time - entry_time);
                      ++count;
                      line[i].set_trans_time((rand() % MAX_TRANS_TIME) + 1);
                  }
              }
  
              // Decrement and update transaction time.
              else {
                  trans_time = line[i].get_trans_time() - 1;
                  line[i].set_trans_time(trans_time);
              }
          }
          
          // Display status of the queues for the 
          // the given iteration.
          for (int i = 0; i &lt; queuecount; i++) {
              cout &lt;&lt; setw(4) &lt;&lt; line[i].get_trans_time() &lt;&lt; &quot;  &quot;;
              line[i].display();
          }
          cout &lt;&lt; endl;
      }
  
      cout &lt;&lt; count &lt;&lt; &quot; customers waited an average of &quot;;
      cout &lt;&lt; wait_sum / count &lt;&lt; &quot; ticks.&quot; &lt;&lt; endl;
      cout &lt;&lt; &quot;The longest time a customer waited was &quot; &lt;&lt; wait_time &lt;&lt; &quot; ticks.&quot; &lt;&lt; endl;
      cout &lt;&lt; queue_total(line, queuecount) &lt;&lt; &quot; customers remain in the lines.&quot; &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  <p>The queue implementation</p>
  <pre><code>#ifndef QUEUE_H
  #define QUEUE_H
  #include &lt;iostream&gt;
  using namespace std;
  
  class Queue {
  private:
      struct Node {
          int data;
          Node* next;
          Node(int d) {
              data = d;
              next = NULL;
          }
      };
      Node* front;
      Node* rear;
      int count;
      int trans_time;
  
  public:
      Queue() {
          front = rear = NULL;
          count = trans_time = 0;
      }
  
      void enqueue(int x) {
          // Create a new linked Node.
          Node* temp = new Node(x);
  
          // If the queue is already empty
          // then new node is front and rear
          if (empty()) {
              front = rear = temp;
              return;
          }
  
          // Add the new node at the end
          // of the queue and change the rear
          rear-&gt;next = temp;
          rear = temp;
  
          ++count;
      }
  
      void dequeue() {
          // If the queue is empty 
          // then we can return NULL
          if (empty())
              return;
  
          // Store the previous front and
          // move the front one node ahead
          Node* temp = front;
          front = front-&gt;next;
  
          // If front becomes NULL, then
          // Change the rear to be NULL as well
          if (front == NULL) 
              rear = NULL;
  
          delete(temp);
  
          --count;
      }
  
      // Utility function to check
      // if the queue is empty
      bool empty() {
          return (front == NULL &amp;&amp; rear == NULL);
      }
  
      int size() {
          return count;
      }
  
      // Utility function to print front
      // element of the queue
      int print_front() {
          return front-&gt;data;
      }
  
      void display() {
          Node* temp = front;
  
          while (!(temp == NULL)) {
              cout &lt;&lt; temp-&gt;data &lt;&lt; &quot; &quot;;
              temp = temp-&gt;next;
          }
          cout &lt;&lt; endl;
      }
  
      void set_trans_time(int i) {
          trans_time = i;
      }
  
      int get_trans_time() {
          return trans_time;
      }
  };
  
  #endif
  </code></pre>