Challenge

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

Program

#include

// Enum to represent the landing gear status

enum LandingGearStatus {

    RETRACTED,

    EXTENDED

};

// Structure to represent the airplane

struct Airplane {

    enum LandingGearStatus landing_gear_status;

};

// Function to retract the landing gear

void retract_landing_gear(struct Airplane *airplane) {

    if (airplane->landing_gear_status == RETRACTED) {

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

    } else {

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

        airplane->landing_gear_status = RETRACTED;

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

    }

}

// Function to extend the landing gear

void extend_landing_gear(struct Airplane *airplane) {

    if (airplane->landing_gear_status == EXTENDED) {

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

    } else {

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

        airplane->landing_gear_status = EXTENDED;

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

    }

}

int main() {

    // Create an instance of the Airplane structure

    struct Airplane airplane;

    airplane.landing_gear_status = RETRACTED;

    while (1) {

        // User menu

        printf("\nAirplane's Landing Gear Control:\n");

        printf("1. Retract Landing Gear\n");

        printf("2. Extend Landing Gear\n");

        printf("3. Quit\n");

        // User input

        int choice;

        printf("Enter your choice (1/2/3): ");

        scanf("%d", &choice);

        // Perform action based on user choice

        switch (choice) {

            case 1:

                retract_landing_gear(&airplane);

                break;

            case 2:

                extend_landing_gear(&airplane);

                break;

            case 3:

                printf("Exiting program.\n");

                return 0;

            default:

                printf("Invalid choice. Please enter 1, 2, or 3.\n");

        }

    }

    return 0;

}

 Expected Output

Airplane's Landing Gear Control:

1. Retract Landing Gear

2. Extend Landing Gear

3. Quit

Enter your choice (1/2/3): 1

Retracting landing gear...

Landing gear retracted.

Airplane's Landing Gear Control:

1. Retract Landing Gear

2. Extend Landing Gear

3. Quit

Enter your choice (1/2/3): 2

Extending landing gear...

Landing gear extended.

Airplane's Landing Gear Control:

1. Retract Landing Gear

2. Extend Landing Gear

3. Quit

Enter your choice (1/2/3): 1

Landing gear is already retracted.

Airplane's Landing Gear Control:

1. Retract Landing Gear

2. Extend Landing Gear

3. Quit

Enter your choice (1/2/3): 3

Exiting program.

Screenshot

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.