Traffic Light Problem-Computer Programming-Assignment, Exercises of Computer Engineering and Programming

This assignment was given by Dr. Jyoti Lokesh at Biju Patnaik University of Technology, Rourkela for Computer Programming course. It includes: Traffic, Problems, Program, Procedure, Sample, Run, Timer, Value, Statement, Indentation, Code, Commenting

Typology: Exercises

2011/2012

Uploaded on 07/13/2012

ekvir
ekvir 🇮🇳

4

(1)

35 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Tuesday March 13, 2012 11:00am
Assignment 1
Course No. CS1124
Course Title Computer Programming
Deadline: Monday 19th March, 2012 12:00pm
The Story:
Today when you were entering the university you realized that our beloved university, MAJU, is
facing traffic problems on the main gate. A guard has to be always there to direct the traffic.
You being an efficient CS student offer your services to resolve this issue. You suggest that
traffic lights be inserted on the three roads meeting at MAJU gate. Traffic moves out of gate
through one road (let us call it the “out-road”) and can come in from two roads (let us call these
“in-road1” and “in-road2”). You are now writing the software for traffic lights on these three
roads (i.e. out-light, in-light1 and in-light2 respectively).
Each traffic light stays green for 3 seconds, yellow for 1 second and red for 8 seconds (we
assume traffic is moving at the speed of light for this assignment!). The lights turn green in this
order: out-light, in-light1, in-light2.
You can represent the three signals of a traffic light: red, yellow and green, by the characters
‘R’, ‘Y’ and ‘G’ respectively. The on signal can be represented by writing the word “on” with
respective signal, e.g. if green is on for in-light1, this state can be represented as:
In-Light1: R Y G(on)
You have to display a timer too and change the signals accordingly.
The Program:
Your program starts by prompting user to enter a timer value. The program then displays lights’ statuses
for decrementing timer values till the timer turns down to 0.
Then the program asks the user if he wants to enter another timer value.
1. If user enters y or Y, the program should repeat the above procedure.
2. If user enters n or N, the program stops after printing a greeting message.
3. If user enters any other character, the program prompts him to answer with yes or no only and
asks again if he wants to enter another timer value.
Sample Run:
Here is a sample run of your program:
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Traffic Light Problem-Computer Programming-Assignment and more Exercises Computer Engineering and Programming in PDF only on Docsity!

Assignment 1

Course No. CS Course Title Computer Programming

Deadline: Monday 19th^ March, 2012 12:00pm

The Story:

Today when you were entering the university you realized that our beloved university, MAJU, is facing traffic problems on the main gate. A guard has to be always there to direct the traffic. You being an efficient CS student offer your services to resolve this issue. You suggest that traffic lights be inserted on the three roads meeting at MAJU gate. Traffic moves out of gate through one road (let us call it the “out-road”) and can come in from two roads (let us call these “in-road1” and “in-road2”). You are now writing the software for traffic lights on these three roads (i.e. out-light, in-light1 and in-light2 respectively). Each traffic light stays green for 3 seconds, yellow for 1 second and red for 8 seconds (we assume traffic is moving at the speed of light for this assignment!). The lights turn green in this order: out-light, in-light1, in-light2.

You can represent the three signals of a traffic light: red, yellow and green, by the characters ‘R’, ‘Y’ and ‘G’ respectively. The on signal can be represented by writing the word “on” with respective signal, e.g. if green is on for in-light1, this state can be represented as:

In-Light1: R Y G(on)

You have to display a timer too and change the signals accordingly.

The Program:

Your program starts by prompting user to enter a timer value. The program then displays lights’ statuses for decrementing timer values till the timer turns down to 0. Then the program asks the user if he wants to enter another timer value.

  1. If user enters y or Y , the program should repeat the above procedure.
  2. If user enters n or N , the program stops after printing a greeting message.
  3. If user enters any other character, the program prompts him to answer with yes or no only and asks again if he wants to enter another timer value.

Sample Run:

Here is a sample run of your program:

Please enter the timer value: (user enters 26)

Timer: 26 Out-Light: R Y G(on) In-Light1: R(on) Y G In-Light2: R(on) Y G Timer: 25 Out-Light: R Y G(on) In-Light1: R(on) Y G In-Light2: R(on) Y G Timer: 24 Out-Light: R Y G(on) In-Light1: R(on) Y G In-Light2: R(on) Y G Timer: 23 Out-Light: R Y(on) G In-Light1: R Y(on) G In-Light2: R(on) Y G Timer: 22 Out-Light: R(on) Y G In-Light1: R Y G(on) In-Light2: R(on) Y G Timer: 21 Out-Light: R(on) Y G In-Light1: R Y G(on) In-Light2: R(on) Y G Timer: 20 Out-Light: R(on) Y G In-Light1: R Y G(on) In-Light2: R(on) Y G Timer: 19 Out-Light: R(on) Y G In-Light1: R Y(on) G In-Light2: R Y(on) G Timer: 18 Out-Light: R(on) Y G In-Light1: R(on) Y G In-Light2: R Y G(on) Timer: 17 Out-Light: R(on) Y G In-Light1: R(on) Y G In-Light2: R Y G(on) Timer: 16

Out-Light: R(on) Y G In-Light1: R(on) Y G In-Light2: R Y G(on) Timer: 4 Out-Light: R(on) Y G In-Light1: R(on) Y G In-Light2: R Y G(on) Timer: 3 Out-Light: R Y(on) G In-Light1: R(on) Y G In-Light2: R Y(on) G Timer: 2 Out-Light: R Y G(on) In-Light1: R(on) Y G In-Light2: R(on) Y G Timer: 1 Out-Light: R Y G(on) In-Light1: R(on) Y G In-Light2: R(on) Y G Timer: 0 Out-Light: R Y G(on) In-Light1: R(on) Y G In-Light2: R(on) Y G

Do you want to enter another timer value? Please enter y for yes or n for no: (user enters x) Please answer with yes or no only.

Do you want to enter another timer value? Please enter y for yes or n for no: (user enters 7) Please answer with yes or no only.

Do you want to enter another timer value? Please enter y for yes or n for no: (user enters Y) Please enter the timer value: (user enters 1)

Timer: 1 Out-Light: R Y G(on) In-Light1: R(on) Y G In-Light2: R(on) Y G Timer: 0 Out-Light: R Y G(on) In-Light1: R(on) Y G In-Light2: R(on) Y G

Do you want to enter another timer value? Please enter y for yes or n for no: (user enters N)

Have a nice day! Good bye : )

Note:

Do not use “go to” statement.

There are marks for good programming practices. Marks will be deducted for not

following good practices which include (but are not limited to):

 Proper indentation of code

 Using meaningful variable names

 Proper Commenting of code

Submission Instructions:

Go to run and enter this path

\dataserver\assignments$\Raabia Mumtaz\CP-1\Assignment

Copy your .cpp file and paste it in this folder.

Only submit .cpp file and do not make multiple submissions.

The name of file should be your registration no.

Inside your .cpp file, the first two lines should be your name and registration no.

in comments.

Marks will be deducted if you do not follow these instructions, or if you make

multiple submissions.