142k views
1 vote
3. 5 marks Your car is approaching an intersection with a traffic light. Different action is required depending on the color and behavior of the traffic light, i.e. red, green, yellow, flashing red and flashing yellow. Build a MATLAB program using switch-case-otherwise structure to recommend the cars' users what is the required action. The program may start with this statement: % Program to suggest action when approaching an intersection clear all; clc; disp('Upon approaching an intersection with working traffic lights...') inp_status=input('Enter a traffic light status/condition in single quotes: ");

1 Answer

4 votes

Here's a MATLAB program using the switch-case-otherwise structure to recommend the required action based on the traffic light status/condition:



% Program to suggest action when approaching an intersection

clear all;

clc;

disp('Upon approaching an intersection with working traffic lights...')

inp_status = input('Enter a traffic light status/condition in single quotes: ');

switch inp_status

case 'red'

disp('Stop at the red light and wait for it to turn green.');

case 'green'

disp('Proceed through the intersection if it is safe to do so.');

case 'yellow'

disp('Prepare to stop if it is safe to do so, as the light will soon turn red.');

case 'flashing red'

disp('Stop at the intersection, treat it as a stop sign, and proceed when it is safe.');

case 'flashing yellow'

disp('Proceed with caution, but yield to any vehicles or pedestrians in the intersection.');

otherwise

disp('Invalid traffic light status/condition.');

end

User Arimbun
by
8.7k points