This program is going to be written in C++ and requires NO graphics (ONLY calcul
ID: 3727053 • Letter: T
Question
This program is going to be written in C++ and requires NO graphics (ONLY calculations and the output basically). Also can you state the compiler you used.
1. A traffic light exists which permits precisely one car out from each street at a time (yes, this is not a normal intersection). The “green” light is active in a counter-clockwise pattern around the intersection, starting at the top. The intersection looks like the figure below. https://imgur.com/a/pPc7q
When one light is green, all of the other streets’ lights are red.
2. It takes precisely two seconds for each car to drive from any street to any other street through the intersection. Cars begin driving immediately when the light changes green and there is a sign at each street instructing drivers “one car per green light”.
3. It takes three seconds for the light to change from green to red.
4. Each time the light changes, there is a 25% chance that a new car will show up at the intersection on each street. Meaning, there’s a 25% chance a new car will show up at the “top” street, another 25% chance that a car will show up at the “left” street, and so on every time the light changes.
5. If there are no cars waiting at a particular street entering the intersection, the traffic light still waits for a car to proceed. (There’s no sensors or fancy equipment advising the traffic light to skip an empty street).
6. At any one time, only one car may be in the intersection.
7. A car cannot enter the road and cross the intersection both on the same green light (they must come to a complete stop even if there’s no car in front of them). That is to say, if a new car arrives and there’s no car in front it, the new car cannot proceed through the intersection until the next time that the light turns green.
8. You will need to track how much “virtual time” has passed. This is much easier than it sounds. You simply need to have a variable to which you add a number of seconds for each event that occurs. You may need to make multiple “virtual clocks” to measure both elapsed time and time that the intersection has a car in it. You also need to be careful about ensuring that you don’t double-count time. (e.g. a green light lasts three seconds and the car takes 2 seconds to go through the intersection – only three seconds have elapsed).
This is the first implementation of four and each one needs to be in its own c++ file.
1. You should create a class to represent the roads and instances of the roads. Create other classes and instances as necessary. You may wish to consider using an array to represent the four in-bound roads as it will ultimately make it easy to manage your code. (file: sim-1.cpp)
2. You should run your simulation for 1 million simulated seconds and report the following statistics:
a. The average number of cars waiting to enter the intersection from each road. (The “average” to be calculate by measuring once per cycle the number of cars waiting to enter the intersection and then dividing the sum of all of the measurements by the number of cycles.)
b. The average “utilization” of the intersection (what percent of the time is there a car in the intersection?)
Second implementation:
1. The traffic light is made more intelligent by the introduction of sensors such that the light will only turn green if there is a car waiting at the road (it skips roads with no waiting cars). (file: sim-2.cpp)
2. You should run your simulation for 1 million simulated seconds and report the following statistics:
a. The average number of cars waiting to enter the intersection.
b. The average “utilization” of the intersection (what percent of the time is there a car in the intersection?)
Third implentation:
a. If the probability of a car arriving increases from 25% to 50%, how is traffic affected?
Fourth implementation:
b. Given the (a) above, if the amount of time between lights is decreased from 3 seconds to 2.5 seconds, how is traffic affected?
This part is for the third and fouth implementaions:
For each of the two above scenarios, you should run your simulation for 1 million simulated seconds and report the following statistics:
a. The average number of cars waiting to enter the intersection.
b. The average “utilization” of the intersection (what percent of the time is there a car in the intersection?)
Explanation / Answer
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int num_vehicles;
int t;
int max_time;
int timer; //I need to initialize timer in such a way and as such an entity that its keeps resizing itself .
int add;
while(1)
{
cout<<"Enter the number of vehicles at the red light: ";
cin>>num_vehicles;
cout<<" ";
if(num_vehicles<30)
timer=num_vehicles;
else if(num_vehicles>=30)
{
timer=num_vehicles/2;
add=num_vehicles/2;
}
cout<<"The red light turns green for "<<num_vehicles<<" number of time steps. ";
if(t=0)
{
cout<<"Total waiting time for first car at current red light since entry into system: 0";
}
else if(t=1)
{
cout<<"Total waiting time for first car at current red light since entry into system: ";/*timer for first green light*/cout<<" ";
}
else if(t=2)
{
cout<<"Total waiting time for first car at current red light since entry into system: ";/*timer for first green light+second green light*/cout<<" ";
}
else
{
cout<<"Total waiting time for first car at current red light since entry into system: ";/*sum of timers for last three green lights*/cout<<" ";
}
t++;
cout<<"Moving to the next traffic light in clockwise order. ";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.