IGCSE cs 0478 2023 Nov 21 10

IGCSE cs 0478 2023 Nov 21 10

Drama students put on a performance of a play for one evening. Seats in a small theatre can be booked for this performance.

The theatre has 10 rows of 20 seats. The status of the seat bookings for the evening is held in the two-dimensional (2D) Boolean array Evening[]

Each element contains FALSE if the seat is available and TRUE if the seat is booked.

Up to and including four seats can be booked at one time. Seats are allocated in order from those available. A row or seat number cannot be requested.

The array Evening[] has already been set up and some data stored.

Write a program that meets the following requirements:

  • counts and outputs the number of seats already booked for the evening

  • allows the user to input the number of seats required

  • validates the input

  • checks if enough seats are available:

    • if they are available

      • changes the status of the seats

      • outputs the row number and seat number for each seat booked

    • if they are not available:

      • outputs a message giving the number of seats left or ‘House full’ if the theatre is fully booked.

You must use pseudocode or program code and add comments to explain how your code works. You do not need to declare any arrays or variables; you may assume that this has already been done.

You do not need to initialise the data in the array Evening[] All inputs and outputs must contain suitable messages.

Python版初始化代码,练习的时候使用如下的代码


import string
import random

#################################################
# Declare
#################################################

ROW_NUMBER = 10
SEAT_NUMBER = 20
evening = []

#################################################
# Initialize Data (not include)
#################################################

for row_index in range(ROW_NUMBER):
    row_array = []
    for seat_index in range(SEAT_NUMBER):
        row_array.append(False)
    evening.append(row_array)

def booking_from_first(ticket_num: int):
    booked_number = 0
    for row_index in range(ROW_NUMBER):
        for seat_index in range(SEAT_NUMBER):
            if booked_number < ticket_num:
                evening[row_index][seat_index] = True
                booked_number += 1
            else:
                return None

print(evening)
booking_from_first(39)
print(evening)

#################################################
# For you to write
#################################################

图解

Python版答案


import string
import random

#################################################
# Declare
#################################################

ROW_NUMBER = 10
SEAT_NUMBER = 20
evening = []

#################################################
# Initialize Data (not include)
#################################################

for row_index in range(ROW_NUMBER):
    row_array = []
    for seat_index in range(SEAT_NUMBER):
        row_array.append(False)
    evening.append(row_array)

def booking_from_first(ticket_num: int):
    booked_number = 0
    for row_index in range(ROW_NUMBER):
        for seat_index in range(SEAT_NUMBER):
            if booked_number < ticket_num:
                evening[row_index][seat_index] = True
                booked_number += 1
            else:
                return None

print(evening)
booking_from_first(39)
print(evening)

#################################################
# For you to write
#################################################

already_booked_count = 0
row_index = 0
column_index = 0
book_seat_num = 0
book_index = 0

# counts and outputs the number of seats already booked for the evening
for row_index in range(ROW_NUMBER):
    for column_index in range(SEAT_NUMBER):
        if evening[row_index][column_index]:
            already_booked_count = already_booked_count + 1

print(already_booked_count, "seats already booked for the evening")

# allows the user to input the number of seats required
book_seat_num = int(input("How many seats do you want to book? 1, 2, 3 or 4 "))
while book_seat_num < 1 or book_seat_num > 4:
    print("Input seat number invalid")
    book_seat_num = int(input("How many seats do you want to book? 1, 2, 3 or 4 "))

if already_booked_count >= 200:
    print("House full")
else:
    if already_booked_count + book_seat_num > 200:
        print("Only ", ROW_NUMBER * SEAT_NUMBER - already_booked_count, " seats left")
    else:
        for book_index in range(book_seat_num):
            # changes the status of the seats
            seat_row = (already_booked_count + book_index) // SEAT_NUMBER
            seat_column = (already_booked_count + book_index) % SEAT_NUMBER
            evening[seat_row][seat_column] = True
            print("Row ", seat_row + 1, "Seat ", seat_column + 1, " booked.")

Last updated

Was this helpful?