Question: #include trafficSimulator.h / / optional TODO: Put your new function declarations here int max ( int a , int b ) ; / *
#include "trafficSimulator.h
optional TODO: Put your new function declarations here
int max int a int b ;
printNames
input: none
output: none
Prints names of the students who worked on this solution
REMINDER: You may only work with one other cs student
void printNames
TODO : Fill in you and your partner's names or NA if you worked individually
printf
This solution was completed by:
;
printf
;
printf
;
createTrafficData
input: char filename of file to read
output: TrafficData which stores the data of this road network
Create a new TrafficData variable and read in the traffic data from the file whose name is stored in char filename
TrafficData createTrafficData char filename
open the file
FILE pFile fopen filename, r;
if pFileNULL
printfERROR the file s was not found!
filename;
exit;
TODO: read in all the data in pFile
HINT: use fscanf pFile, d & to read an int from pFile
HINTs:
Each road can be stored in a RoadData struct see roadh
You'll want to store all of these RoadData structs in a single array. This is important to track the order that
roads were added to your graph. These are also the order they are processed in each time step. You can store this
in the arrayOfRoads variable in your TrafficData struct.
You'll add edges to a graph to represent each road using setEdgesee graphc The vertices of the graph
represent intersections. The graph will do the work of finding shortest paths see getNextOnShortestPath in graph.c
and the longest shortest path problem on Canvas
It will also help to add your RoadData structs to your graph using setEdgeDatasee graphc You can then
retrieve that RoadData struct by calling getEdgeData This will be helpful for finding roads when cars pass
through intersections.
Each event will be stored in an Event struct see eventh
It's recommended to store all of the Event structs in a single priority queue using time step as the priority This
allows you to quickly find the next event to execute by time step see enqueueByPrioritygetFrontPriority and
dequeuePQ
Each car is stored in a Car struct see carh
close file
fclose pFile ;
return NULL; TODO: Replace this with your TrafficData pointer
trafficSimulator
input: char name of file to read
output: NA
Simulate the road network in the given TrafficData variable
void trafficSimulator TrafficData pTrafficData
TODO: complete this function
Loop until all events processed and either all cars reached destination
You can also assume all test data will finish in steps
This fact can be used to help you avoid infinite loops while completing the project
Print the current step number
Update the state of every traffic light
Loop on events associated with this time step
If ADDCAREVENT, use mergeQueues from queue.h to add the queue of cars to the queue of the road associated with the event
If ROADACCIDENTEVENT, add one to the numAccidents of the road associated with the event
If ROADRESOLVEDEVENT, subtract one from the numAccidents of the road associated with the event
For each road, try to move waiting cars onto the end of that road if possible remember to check that numAccidents for the road
For each road, try to move cars, which haven't already moved, forward one space on every road remember to check that numAccidents for the road
For each road, try to move cars, which haven't already moved, through the next intersection remember to check that numAccidents for both roads
After the loop finishes print the average and max number of steps it took for a car to reach its destination
freeTrafficData
input: TrafficData pTrafficData
output: NA
Free the data in the TrafficData variable
void freeTrafficData TrafficData pTrafficData
TODO: complete this function
int max int a int b
ifab
return a;
return b;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
