Challenge

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

1.

#include <stdio.h>

// Enum representing the states of the landing gear FSM
typedef enum {
    RETRACTED,
    EXTENDED,
    IN_TRANSITION
} LandingGearState;

typedef struct {
    LandingGearState currentState;
} LandingGearController;

void retractGear(LandingGearController* controller) {
    switch (controller->currentState) {
        case RETRACTED:
            printf("Landing gear is already retracted.\n");
            break;
        case EXTENDED:
            printf("Retracting landing gear...\n");
            controller->currentState = IN_TRANSITION;
            // Code to physically retract the landing gear
            // ...
            controller->currentState = RETRACTED;
            printf("Landing gear retracted successfully.\n");
            break;
        case IN_TRANSITION:
            printf("Landing gear is in transition. Please wait.\n");
            break;
    }
}

void extendGear(LandingGearController* controller) {
    switch (controller->currentState) {
        case RETRACTED:
            printf("Extending landing gear...\n");
            controller->currentState = IN_TRANSITION;
            // Code to physically extend the landing gear
            // ...
            controller->currentState = EXTENDED;
            printf("Landing gear extended successfully.\n");
            break;
        case EXTENDED:
            printf("Landing gear is already extended.\n");
            break;
        case IN_TRANSITION:
            printf("Landing gear is in transition. Please wait.\n");
            break;
    }
}

int main() {
    LandingGearController gearController;
    gearController.currentState = RETRACTED;

    // Testing the FSM by calling retractGear() and extendGear() functions
    retractGear(&gearController);
    extendGear(&gearController);
    extendGear(&gearController);
    retractGear(&gearController);

    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.