Jump to content

Traffic signal simulator (I need help!)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
math415

math415

    Newbie

  • Members
  • Pip
  • 2 posts
I attached the source code file:
I am about to blow this class that I am in. This is the last class that I need to finish my Mechanical Engineering Degree. I have until sunday afternoon to figure this out.
I need a 95% on this program to pass the course.

All the help that I can get to understand this stuff will be truly appreciated

/*I have an intersection with 4 traffic signals
In c++ I need to use a Class.
Here is the code, at the end of the code I will elaborate on what needs to be done that I am having problems with!*/

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

class TrafficSignal {
private:

bool Red, Yellow, Green; // bool variables to control whether the lights are lit

int Red_Duration, Yellow_Duration, Green_Duration;
//int variables to controltheir Durations

int Id; //int that is the identifer for the signal
int Elapsed_Time; //int variable indicating # of seconds since signal started
int Tick;
//this is my extra time variable to support the "time since the last cycle change" functionality.

public:
TrafficSignal(); // This is the default constructor

void setRed(bool);
void setYellow(bool);
void setGreen(bool);
void setRed_Duration(int);
void setYellow_Duration(int);
void setGreen_Duration(int);
void setId(int);
void setElapsed_Time(int);
void setTick(int); //this is the added time variable

bool getRed();
bool getYellow();
bool getGreen();
int getRed_Duration();
int getYellow_Duration();
int getGreen_Duration();
int getId();
int getElapsed_Time();
int getTick(); // part of the added time variable

void cycle();
//This is the member function called cycle that takes no parameters and returns nothing.

void printTrafficSignal();
void printState();
};


//constructor implementation
TrafficSignal::TrafficSignal() {
Red = true;
Yellow = false;
Green = false;
Elapsed_Time = 0;
Id = 1;
Red_Duration = 40; //This is the length that a light stays Red
Yellow_Duration = 10; //" " stays Yellow
Green_Duration = 35; // " " stays Green
}
/* Below:
This is the cycle member function written not in line. It is set up, if called on an object of type TrafficSignal, to move the signal's state to its next value. It will, if current light state is Red, move to Green. If the light's current state is yellow, then it WILL move to red, and if the light's current state is green, it WILL move to yellow.*/

void TrafficSignal::cycle(){

if (Red == true){
Red = false;
Green = true;
}
if (Green == true){
Green = false;
Yellow = true;
}
if (Yellow == true){
Yellow = false;
Red = true;
}
}

void TrafficSignal::printState(){
cout << "Signal " << Id << " is GREEN." << endl;
cout << "Signal " << Id << " is Yellow." << endl;
cout << "Signal " << Id << " is Red." << endl;
}

//****Start of the Sets!!!!*******

void TrafficSignal::setRed(bool RED){
Red = RED;
}
void TrafficSignal::setYellow(bool YELLOW){
Yellow = YELLOW;
}

void TrafficSignal::setGreen(bool GREEN){
Green = GREEN;
}


void TrafficSignal::setRed_Duration(int Rd_Dur){
Red_Duration = 40;
}

void TrafficSignal::setYellow_Duration(int Ylw_Dur){
Yellow_Duration = 10;
}

void TrafficSignal::setGreen_Duration(int Grn_Dur){
Green_Duration = 35;
}

void TrafficSignal::setId(int New_ID){
Id=New_ID;
}

void TrafficSignal::setElapsed_Time(int Elp_Time){

}

void TrafficSignal::setTick(int Tickcount){

}
//****End of the Sets!!!!*******



//****Start of the Gets!!!!*******
int TrafficSignal::getTick(){
return (Tick);
}

bool TrafficSignal::getRed(){
return (Red);
}
bool TrafficSignal::getYellow(){
return (Yellow);
}

bool TrafficSignal::getGreen(){
return (Green);
}


int TrafficSignal::getRed_Duration(){
return (Red_Duration);
}

int TrafficSignal::getYellow_Duration(){
return (Yellow_Duration);
}

int TrafficSignal::getGreen_Duration(){
return (Green_Duration);
}
/*Below:
This is where the printrafficsignal function retrieves the Id variables value. every time that the loop in the main function repeats it self, the Id value changes and is displayed in next c output on the screen. This keeps happening until the loop is finished.*/

int TrafficSignal::getId(){
return (Id);
}

int TrafficSignal::getElapsed_Time(){
return (Elapsed_Time);
}

/*Below:
is the print traffic signal function, it is interconnected with the 4 signal array. The cout that you see in the printTrafficSignal will be displayed 4 times because of the loop that is in the main to create the 4 separate distinguishable traffic signals.*/

void TrafficSignal::printTrafficSignal(){
cout << "Signal " << Id << " is "/*<< output here for light state<<*/<< endl;
}
//****End of the Gets!!!!*******

//TrafficSignal tick()
//if (Green){

//}
int main (){



const int NUM_SIGNALS=4;
TrafficSignal signal[NUM_SIGNALS];
for (int i=0; i < NUM_SIGNALS; i++) {// This array sets up the name of the individual traffic signals (1 2 3 4)
int localID=i+1; //This causes the printTrafficSignal to repeat the cout a specified number of times (4)
signal[i].setId(localID); //
signal[i].printTrafficSignal();

}
}

/* Ok, here is the problems that I am having:

Tick Function

Write a member function called Tick that takes no parameters, and returns a bool. That bool should indicate whether the TrafficSignal changed its state on this tick of the clock. Each time the Tick function is called, it should add one to the variable that is tracking the overall time in the TrafficSignal object. It should then, depending on the current state of the lights, and the time elapsed, either return false if it is not time to change the light, or it should call the cycle function, and return true if it is time to change the light. For example, if the light is currently green and the light's "stay green time" is 30 seconds, and it has been 30 seconds since it turned green, then call cycle and return true. If it has only been 29 seconds since the light turned green, then just return false without doing anything else. You may need an extra instance variable in the class to support this "time since the last cycle change" functionality.


below has to do with setting up the main function

In the same file, write a main function below all the class implementations that does the following.
© = Complete (P) = I need help
© 1. Allocate an array of four TrafficSignal objects. If you can't figure this out, you can use four individual TrafficSignal variables, but you'll lose 5 points.
© 2. Give each of the traffic signals a different ID number. I used 1,2, 3, and 4 so it was easy to see what was going on.
(P) 3. Set the durations for red/yellow/green to 60/15/60 for the first and third TrafficSignal, and 75/12/48 for the second and fourth.
(P)4. Set the 2nd and 4th light state to green (leave the first and third on their default-red).
(P)5. Write a loop that will count from 1 to 1000. This will be your "timer" loop. Each loop iteration
will represent 1 second of time.
(P)6. Inside the loop, call the tick function on each of the TrafficSignals each time through the loop.
(P)If any of the trafficSignals returns true (i.e., it's state changed on that tick), you should call printState on that signal. See the example to the right as to what you should print.

The bottom statement is what the instructor said that it should do!

That's it. You should end up with at set of traffic signal states displayed on the console. Yours should be exactly the same as the example I show on the right given that you use the same data that has been specified in the problem. Make sure you write all required comments in your program. Don't embed magic numbers in your code - use data stored in the classes (that's what accessor functions are for...). If your program won't at least compile and run (even if it gives incorrect answers) you'll lose 10 points. So at least make sure it compiles and runs. On the right, the top figure is the beginning of the output from my solution, and the bottom figure is the end of the output from my solution. They overlap a bit in the middle, so you can see my entire output.

Note: the main function writes the "At time..." lines, but the TrafficSignal class writes all the
Signal XX is YYYYY lines, when the printState functions are called. Main shouldn't be very long.
The class should do most of the work.

*/

Attached Files


Edited by Jaan, 02 August 2008 - 02:53 AM.
Use tags when you're posting your codes!


#2
Chewie

Chewie

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,579 posts
Whoa...*speachless* that's insane dude!

#!CrunchBang Linux ~$ apt-get into it | #!(Statler:R20101205): OpenBox | Like Linux?
“The cure for boredom is curiosity. There is no cure for curiosity.”


#3
getzephyr

getzephyr

    Learning Programmer

  • Members
  • PipPipPip
  • 51 posts
Wow............... nice collection!

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Moved to correct forum.

I'd just have const values that store the number of ticks for each color. The tick() function just needs to increment the number of seconds on the current color and, if it matches the current color's count, reset the ticks and return true.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
math415

math415

    Newbie

  • Members
  • Pip
  • 2 posts
Which is the correct forum?

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The C and C++ programming forum, where it is now.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
Chewie

Chewie

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,579 posts
LOL such a great mod you're! =p

#!CrunchBang Linux ~$ apt-get into it | #!(Statler:R20101205): OpenBox | Like Linux?
“The cure for boredom is curiosity. There is no cure for curiosity.”