Challenge

Project 2 - Implement the Code for controlling the retraction and extension of Airplane’s landing gear

#include <stdio.h>

// Enumeration to represent the states of the finite state machine
enum State {
STATE_GEAR_UP,
STATE_GEAR_DOWN,
STATE_GEAR_TRANSIT,
};

// Enumeration to represent the inputs to the finite state machine
enum Input {
INPUT_CHECK_BEFORE_TAKEOFF,
INPUT_CHECK_BEFORE_LANDING,
INPUT_TURN_GEAR_ON,
INPUT_TURN_GEAR_OFF,
INPUT_RAISE_GEAR,
INPUT_LOWER_GEAR,
};

// Function for the GEAR UP state
enum State stateGearUp(enum Input input, int leverPosition) {
if (leverPosition == 1) {
return STATE_GEAR_DOWN;
}
return STATE_GEAR_UP;
}

// Function for the GEAR DOWN state
enum State stateGearDown(enum Input input, int leverPosition) {
if (input == INPUT_CHECK_BEFORE_LANDING || input == INPUT_TURN_GEAR_OFF) {
return STATE_GEAR_UP;
} else if (input == INPUT_RAISE_GEAR || (input == INPUT_LOWER_GEAR && leverPosition == 1)) {
return STATE_GEAR_TRANSIT;
}
return STATE_GEAR_DOWN;
}

// Function for the GEAR TRANSIT state
enum State stateGearTransit(enum Input input, int leverPosition) {
if (input == INPUT_LOWER_GEAR && leverPosition == 1) {
return STATE_GEAR_DOWN;
} else if (input == INPUT_RAISE_GEAR || (input == INPUT_LOWER_GEAR && leverPosition == 0)) {
return STATE_GEAR_UP;
}
return STATE_GEAR_TRANSIT;
}

// Function to update the lights based on the state of the finite state machine
void updateLights(enum State currentState) {
if (currentState == STATE_GEAR_UP) {
printf("Gear is up - Light: OFF\n");
} else if (currentState == STATE_GEAR_DOWN) {
printf("Gear is down - Light: Green\n");
} else if (currentState == STATE_GEAR_TRANSIT) {
printf("Gear in transit - Light: Red\n");
}
}

int main() {
enum State currentState = STATE_GEAR_UP;
int leverPosition;

printf("Enter the initial position of the pilot's lever (0 for down, 1 for up): ");
scanf("%d", &leverPosition);

while (1) {
int choice;

printf("Enter your choice:\n");
printf("1. Check Before Takeoff\n");
printf("2. Check Before Landing\n");
printf("3. Turn Gear On\n");
printf("4. Turn Gear Off\n");
printf("5. Raise Gear\n");
printf("6. Lower Gear\n");
printf("Choice (0 to exit): ");
scanf("%d", &choice);

if (choice == 0) {
break;
}

// Map the user's choice to the corresponding input for the finite state machine
enum Input input;
switch (choice) {
case 1:
input = INPUT_CHECK_BEFORE_TAKEOFF;
break;
case 2:
input = INPUT_CHECK_BEFORE_LANDING;
break;
case 3:
input = INPUT_TURN_GEAR_ON;
break;
case 4:
input = INPUT_TURN_GEAR_OFF;
break;
case 5:
input = INPUT_RAISE_GEAR;
break;
case 6:
input = INPUT_LOWER_GEAR;
break;
default:
printf("Invalid choice\n");
continue;
}

// Update the current state based on the input and lever position
switch (currentState) {
case STATE_GEAR_UP:
currentState = stateGearUp(input, leverPosition);
break;
case STATE_GEAR_DOWN:
currentState = stateGearDown(input, leverPosition);
break;
case STATE_GEAR_TRANSIT:
currentState = stateGearTransit(input, leverPosition);
break;
}

// Update the lights based on the current state of the finite state machine
updateLights(currentState);
}

return 0;
}


Enter the initial position of the pilot's lever (0 for down, 1 for up): 0
Enter your choice:
1. Check Before Takeoff
2. Check Before Landing
3. Turn Gear On
4. Turn Gear Off
5. Raise Gear
6. Lower Gear
Choice (0 to exit): 1
Gear is up - Light: OFF
Enter your choice:
1. Check Before Takeoff
2. Check Before Landing
3. Turn Gear On
4. Turn Gear Off
5. Raise Gear
6. Lower Gear
Choice (0 to exit): 2
Gear is up - Light: OFF
Enter your choice:
1. Check Before Takeoff
2. Check Before Landing
3. Turn Gear On
4. Turn Gear Off
5. Raise Gear
6. Lower Gear
Choice (0 to exit): 3
Gear is up - Light: OFF
Enter your choice:
1. Check Before Takeoff
2. Check Before Landing
3. Turn Gear On
4. Turn Gear Off
5. Raise Gear
6. Lower Gear
Choice (0 to exit): 4
Gear is up - Light: OFF
Enter your choice:
1. Check Before Takeoff
2. Check Before Landing
3. Turn Gear On
4. Turn Gear Off
5. Raise Gear
6. Lower Gear
Choice (0 to exit): 5
Gear is up - Light: OFF
Enter your choice:
1. Check Before Takeoff
2. Check Before Landing
3. Turn Gear On
4. Turn Gear Off
5. Raise Gear
6. Lower Gear
Choice (0 to exit): 6
Gear is up - Light: OFF
Enter your choice:
1. Check Before Takeoff
2. Check Before Landing
3. Turn Gear On
4. Turn Gear Off
5. Raise Gear
6. Lower Gear
Choice (0 to exit): 7
Invalid choice
Enter your choice:
1. Check Before Takeoff
2. Check Before Landing
3. Turn Gear On
4. Turn Gear Off
5. Raise Gear
6. Lower Gear
Choice (0 to exit): 0
[?2004h

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.