Challenge

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

Program Code:

#include <stdio.h>

typedef enum {

GEAR_UP,

GEAR_DOWN,

GEAR_MOVING

} GearState;

typedef enum {

EVENT_EXTEND,

EVENT_RETRACT,

EVENT_COMPLETE

} GearEvent;

GearState currentState = GEAR_UP;

void handleEvent(GearEvent event) {

switch (currentState) {

case GEAR_UP:

if (event == EVENT_EXTEND) {

printf("Extending landing gear...\n");

currentState = GEAR_MOVING;

}

break;

case GEAR_DOWN:

if (event == EVENT_RETRACT) {

printf("Retracting landing gear...\n");

currentState = GEAR_MOVING;

}

break;

case GEAR_MOVING:

if (event == EVENT_COMPLETE) {

if (currentState == GEAR_UP) {

printf("Landing gear extended.\n");

currentState = GEAR_DOWN;

} else if (currentState == GEAR_DOWN) {

printf("Landing gear retracted.\n");

currentState = GEAR_UP;

}

}

break;

}

}

int main() {

handleEvent(EVENT_EXTEND); // Extend the landing gear

handleEvent(EVENT_COMPLETE); // Complete the extension

handleEvent(EVENT_RETRACT); // Retract the landing gear

handleEvent(EVENT_COMPLETE); // Complete the retraction

return 0;

}

Output:

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.