The goal of this lab is to develop a C++ program to simulate a traffic light control system using propositional logic. The system will take input conditions such as the presence of a car, pedestrian crossing, and timer status to determine whether the traffic light should turn green or remain red. Students will learn how to apply logical expressions to solve real-world problems and optimize decision-making.
A traffic light system is required to control the flow of vehicles and pedestrians efficiently. The system should allow vehicles to pass only if they meet specific conditions. The traffic light should turn green if:
In the traffic light system, the conditions can be represented as propositional variables:
The well-formed formula to determine if the traffic light should turn green: (C ∨ T) ∧ ¬P
Explanation of the formula:
We can represent the decision-making process using a truth table, which is a key characteristic of propositional logic.
| C | T | P | C ∨ T | ¬P | (C ∨ T) ∧ ¬P |
|---|---|---|---|---|---|
| T | T | T | T | F | F |
| T | T | F | T | T | T |
| T | F | T | T | F | F |
| T | F | F | T | T | T |
| F | T | T | T | F | F |
| F | T | F | T | T | T |
| F | F | T | F | F | F |
| F | F | F | F | T | F |
Key Observations from the Truth Table:
The traffic light turns green when (Final Column = True):
Traffic Light Control System Simulation
-------------------------------------------------------------
Is a car detected? (1 for Yes, 0 for No): 1
Is the timer condition allowing? (1 for Yes, 0 for No): 1
Is a pedestrian crossing? (1 for Yes, 0 for No): 0
Result: The traffic light is GREEN (Cars can go)
Do you want to try again? (y/n): y
Is a car detected? (1 for Yes, 0 for No): 1
Is the timer condition allowing? (1 for Yes, 0 for No): 0
Is a pedestrian crossing? (1 for Yes, 0 for No): 0
Result: The traffic light is GREEN (Cars can go)
Do you want to try again? (y/n): y
Is a car detected? (1 for Yes, 0 for No): 0
Is the timer condition allowing? (1 for Yes, 0 for No): 1
Is a pedestrian crossing? (1 for Yes, 0 for No): 0
Result: The traffic light is GREEN (Cars can go)
Do you want to try again? (y/n): y
Is a car detected? (1 for Yes, 0 for No): 0
Is the timer condition allowing? (1 for Yes, 0 for No): 0
Is a pedestrian crossing? (1 for Yes, 0 for No): 1
Result: The traffic light is RED (Cars must stop)
Do you want to try again? (y/n): n
Simulation ended. Thank you!
-------------------------------------------------------------
| Category | Criteria | Points |
|---|---|---|
| Variable Declarations (10 points) |
Boolean variables:
|
8 |
Character variable:
|
2 | |
| User Inputs 10 points |
Reads input from standard input and stores it in the variable correctly. | 10 |
| trafficLightControl Function Implementation (30 points) |
Function Prototype: bool trafficLightControl(bool carDetected, bool timerCondition, bool pedestrianCrossing); | 5 |
| Function Implementation: Accurately implement the logical expression (C ∨ T) ∧ ¬P. | 20 | |
| Function Activation: The function is called correctly in the main function. | 5 | |
| Main Function Implementation (30 points) |
Prompt the user to enter input in a clear and correct format. | 5 |
| Proper formatting of program output for readability and clarity. | 5 | |
| do-while loop is implemented correctly. | 20 | |
| Code Readability & Comments (10 points) |
Maintain consistent indentation and spacing throughout the code. | 5 |
| Include comments that explain the purpose of key sections and logic. | 5 | |
| Test Cases & Output Accuracy (10 points) |
The output for different test cases is correct and consistent. | 10 |
| Errors |
Your program has syntax errors, compiling errors, running errors, or infinite loops. | -50 points |
/*
Author: Your Name
Date: Month/Day/Year
Lab Purpose: Traffic Light Control System Simulation
*/
Please submit your Lab 1 source code file: lab1.cpp in D2L!