Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7698925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:21:27+00:00 2026-05-31T22:21:27+00:00

Hi I have a homework assignment where I need to implement an intersection of

  • 0

Hi I have a homework assignment where I need to implement an intersection of two single lane streets over a two hour period. I need to adjust the phasing to get ideally less than 5 cars per queue, 9 is also acceptable.

It all works except something is amiss with the way my phasing is implemented and I just can’t seem to get my head around the problem. The absolute best I can get is one queue being 0 or 1 and the other being 40+. I can’t seem to get both of them under 9. I have got the problem down to my phase checks but can’t think of a way to fix it. I understand that I want to favour Q1 slightly as the cars arrive to that slightly faster than Q2.

Thanks in advance for any help.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

struct Node {
    int data;
    Node *next;
};

class Queue {
private:                         
    Node *listpointer;
public:                          
    Queue();
    ~Queue();
    void push(int newthing);
    void pop();
    int top();
    bool isEmpty();
    int queueCount();
    void Queue::popTwo();
    bool Queue::twoOrMore();
};

Queue::Queue() {
//constructor
    listpointer = NULL;
}

Queue::~Queue() {
//destructor

}

void Queue::push(int newthing) {
//place the new thing on top of the Queue
    Node *temp;
    temp = new Node;             
    temp->data = newthing;
    temp->next = listpointer;
    listpointer = temp;
}

void Queue::pop() {
//remove top item from the Queue
    Node *p;
    p = listpointer;
    if (listpointer != NULL) {
        listpointer = listpointer->next;
        delete p;  
  }
}

int Queue::top() {
//return the value of the top item
    return listpointer->data;
}

bool Queue::isEmpty() {
//returns true if the Queue is empty
    if (listpointer == NULL) {
        return true;
    }
    return false;
}

int Queue::queueCount() {
    Node *temp;
    int count = 0;
    temp = listpointer;
    while (temp != NULL) {
        ++count;
        temp = temp->next;
    }
    return count;
    delete temp;
}

void Queue::popTwo() {
// remove the 2 top items from the stack
    Node *p;
    p = listpointer;
    if (listpointer != NULL) {
        listpointer = listpointer->next;
        delete p;
    }
    p = listpointer;
    if (listpointer != NULL) {
        listpointer = listpointer->next;
        delete p;                
    }
}

bool Queue::twoOrMore() {
// return true if the stack has at least two items
    if(listpointer==NULL || listpointer->next==NULL) return false;
    else return true;
}

//implement/copy your queue structure and functions above
//then, declare two instances:
//Queue Q1, Q2;
//if you want, make a separate function to change the 
//signals between the queues (either green or red)
//When the signal changes, one queue only is allowed to delete elements

Queue Q1, Q2;

int Q1phase = 30; //initial attempt
int Q2phase = 60; //initial attempt
const int Q1arrive = 18; //fixed 
const int Q2arrive = 22; //fixed
const int leave_rate = 10; //fixed, one car leaves either queue every 10 seconds

int car_id=0;
int clock=0;
bool Q1_green, Q2_green; //indicates which queue is opened, only one at a time

int main(int argc, char **argv) {
    //if(argc!=3) {printf("needs: Q1phase Q2phase\n"); exit(0); }
    //Q1phase=atoi(argv[1]);
    //Q2phase=atoi(argv[2]);
    if(Q1phase < 30 || Q2phase < 30) {printf("Minimum time for each queue to be closed is 30 seconds\n"); exit(0);}
    clock = 0;
    car_id = 0;
    Q1_green = true;
    Q2_green = false;
    int length_Q1, length_Q2;
    length_Q1 = 0;
    length_Q2 = 0;

    while (clock < 7200) {
        clock++;
        if (clock % Q1arrive == 0) {
            car_id++;
            //car_id join Q1
            Q1.push(car_id);
            length_Q1 = Q1.queueCount();
        }
        if (clock % Q2arrive == 0) {
            car_id++;
            //or car_id join Q2
            Q2.push(car_id);
            length_Q2 = Q2.queueCount();
        }

        if ((clock % Q1phase == 0) || (clock % Q2phase == 0)) {
            if (Q1_green == true) {
                Q1_green = false;
                Q2_green = true;
            } else {
                Q1_green = true;
                Q2_green = false;
            }
        }

        if (clock % leave_rate == 0) {
            if (Q1_green == true) {
                Q1.pop();
                length_Q1 = Q1.queueCount();
            }

            if (Q2_green == true) {
                Q2.pop();
                length_Q2 = Q2.queueCount();
            }
        }

    //ChangeSignal();//every second, check if it is time to change signals (phasing is important!)
    //After the signal change:
    //verify which queue is opened
    //either Q1 or Q2 will have the chance to delete one element (Q.Leave())
    //
    printf("at time %d:\nthe current length of Q1 is %d\n",clock,length_Q1);
    printf("the current length of Q2 is %d\n", length_Q2);
    //at the end of the simulation, both queues should have few cars
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:21:28+00:00Added an answer on May 31, 2026 at 10:21 pm

    Your total arrival rate exceeds the leave rate, so cars will have to backlog.

    The total arrival rate is 1/22 + 1/18 =~ 0.1010 cars/sec. This exceeds the leave rate of 0.1 cars per sec.

    The light changes every 30 seconds (Q1phase) because Q2phase is a multiple of Q1phase. So basically the queues have an equal duty cycle.

    Cars drain from each queue at half the total rate: 0.05 from one queue, 0.05 from the other (1/20).

    Note that a leave rate of 1/20 is less than 1/18. So the queue with a 1/18 arrival will backlog. The leave rate of 1/20 is greater than 1/22 so the queue with 1/22 arrival rate will not backlog.

    This slight difference is not really slight! There is a world of difference between the arrival rate exceeding the leave rate or not exceeding the leave rate.


    Oh, and here is how to calculate the cars in the backlogging queue:

    Arrival rate: 1/18.
    Leave rate: 1/20 (half of 1/10)
    Total time: 7200 seconds:

    7200 * (1/18) – 7200 * (1 / 20) == ????

    🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have a homework assignment where I need to read two matrix .txt
I have a homework assignment where I need to take input from a file
For my homework assignment, I need to implement Horners Algorithm for converting between bases.
I have to implement an API for a homework assignment, and my instructor has
I need to implement quicksort in SML for a homework assignment, and I'm lost.
I have been given a homework assignment where I need to create a captcha
I need little help on a homework assignment. I have to create a 10
I've got a homework assignment where I have a base class Package, and two
(this is indirectly a part of a much larger homework assignment) I have something
I have three questions regarding a homework assignment for C++. The goal was to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.