Site icon DataFlair

DSA Project – Movie Ticket Booking System

Learn DSA & Get Ready for MAANG Companies Start Now!!

Program 1

// Project --- Movie Ticket Booking System (2D Array and Queue)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define ROWS 10
#define COLS 10

// Structure to hold customer booking
typedef struct 
{
    int booked;         // 0 = available, 1 = booked
    char name[50];      // Name of person who booked
} Seat;

// Declare theater seating
Seat theater[ROWS][COLS];

// Initialize all seats to available
void initializeSeats() 
{
    for (int i = 0; i < ROWS; i++)
        for (int j = 0; j < COLS; j++) 
        {
            theater[i][j].booked = 0;
            strcpy(theater[i][j].name, "");
        }
}

// Display seating arrangement
void displaySeats() 
{
    printf("\nSeating Arrangement (1 = Booked, 0 = Available):\n");
    for (int i = 0; i < ROWS; i++) 
    {
        printf("Row %d: ", i + 1);
        for (int j = 0; j < COLS; j++)
         {
            printf("%d ", theater[i][j].booked);
        }
        printf("\n");
    }
}

// Book a ticket
void bookSeat() 
{
    int row, col;
    char name[50];

    printf("Enter row (1-%d): ", ROWS);
    scanf("%d", &row);   // 1 
    printf("Enter seat number (1-%d): ", COLS);
    scanf("%d", &col);   //1
    getchar();  // consume newline   

    if (row < 1 || row > ROWS || col < 1 || col > COLS) 
    {
        printf("Invalid seat selection.\n");
        return;
    }

    if (theater[row - 1][col - 1].booked)   
    {
        printf("Seat already booked.\n");
    } 
    else 
    {
        printf("Enter your name: ");
        fgets(name, sizeof(name), stdin);
        name[strcspn(name, "\n")] = 0;  // remove newline

        theater[row - 1][col - 1].booked = 1;   
        strcpy(theater[row - 1][col - 1].name, name);

        printf("Seat (%d, %d) booked successfully for %s.\n", row, col, name);
    }
}

// Cancel a ticket
void cancelSeat() 
{
    int row, col;

    printf("Enter row of the seat to cancel (1-%d): ", ROWS);
    scanf("%d", &row);
    printf("Enter seat number (1-%d): ", COLS);
    scanf("%d", &col);

    if (row < 1 || row > ROWS || col < 1 || col > COLS) 
    {
        printf("Invalid seat.\n");
        return;
    }

    if (theater[row - 1][col - 1].booked==1) 
    {
        printf("Booking for %s canceled.\n", theater[row - 1][col - 1].name);
        theater[row - 1][col - 1].booked = 0;
        strcpy(theater[row - 1][col - 1].name, "");
    } else
     {
        printf("Seat is already available.\n");
    }
}

// Display all bookings
void showBookings() 
{
    printf("\nBooked Seats:\n");
    int found = 0;
    for (int i = 0; i < ROWS; i++)
        for (int j = 0; j < COLS; j++) 
        {
            if (theater[i][j].booked==1) 
            {
                printf("Seat (%d, %d) - %s\n", i + 1, j + 1, theater[i][j].name);
                found = 1;
            }
        }

    if (!found)
     {
        printf("No bookings yet.\n");
     }
}

// Main menu
int main() 
{
    int choice;
    initializeSeats();

    while (1) 
    {
        printf("\n------------Movie Ticket Booking System-----------------------\n");
        printf("1. Show Seat Layout\n");
        printf("2. Book a Seat\n");
        printf("3. Cancel a Booking\n");
        printf("4. Show All Bookings\n");
        printf("5. Exit\n");
        printf("\n--------------------------------------------------------------------\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);
        getchar();  // consume newline

        switch (choice) 
        {
            case 1:
                displaySeats();
                break;
            case 2:
                bookSeat();
                break;
            case 3:
                cancelSeat();
                break;
            case 4:
                showBookings();
                break;
            case 5:
                printf("Exiting system. Goodbye!\n");
                return 0;
            default:
                printf("Invalid choice. Try again.\n");
        }
    }

    return 0;
}
Exit mobile version