Challenge

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

Code:

#include
#include
#include

enum GearState {
    RETRACTED,
    EXTENDED,
    TRANSITIONING
};

// switch states
enum SwitchState {
    OPEN,
    CLOSED
};

// Function to simulate hydraulic pump control
void hydraulicControl(enum GearState *gear, const char *action) {
    if (action == "extend") {
        if (*gear == RETRACTED) {
            printf("Extending gear...\n");
            *gear = TRANSITIONING;
            sleep(2);  // Gear transition time
            *gear = EXTENDED;
        }
    } else if (action == "retract") {
        if (*gear == EXTENDED) {
            printf("Retracting gear...\n");
            *gear = TRANSITIONING;
            sleep(2);
            *gear = RETRACTED;
        }
    } else {
        printf("Unknown action\n");
    }
}

int main() {
    enum GearState noseGear = RETRACTED;
    enum GearState leftGear = RETRACTED;
    enum GearState rightGear = RETRACTED;
    enum SwitchState squatSwitch = CLOSED;

    // Simulating cases
    // Case 1: Plane is about to take off, emergency comes, abort takeoff
    hydraulicControl(&noseGear, "retract");
    hydraulicControl(&leftGear, "retract");
    hydraulicControl(&rightGear, "retract");
    sleep(2);  // Simulating emergency
    hydraulicControl(&noseGear, "extend");
    hydraulicControl(&leftGear, "extend");
    hydraulicControl(&rightGear, "extend");

    // Case 2 : During takeoff, check squat switch for 4 seconds
    sleep(4);  
    if (squatSwitch == OPEN) {
        printf("Squat switch open, raising gear...\n");
        hydraulicControl(&noseGear, "retract");
        hydraulicControl(&leftGear, "retract");
        hydraulicControl(&rightGear, "retract");
    } else {
        printf("Squat switch closed, not raising gear\n");
    }

    return 0;
}

Result:


Extending gear...
Extending gear...
Extending gear...
Squat switch closed, not raising gear

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.