Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A small theatre has 5 rows of 8 seats. The program marks booked seats.
DECLARE Booked : ARRAY[1:5, 1:8] OF BOOLEAN
DECLARE Row : INTEGER
DECLARE Seat : INTEGER
DECLARE Free : INTEGER
FOR Row ← 1 TO 5
FOR Seat ← 1 TO 8
Booked[Row, Seat] ← FALSE
NEXT Seat
NEXT Row
Booked[3, 6] ← TRUE
Booked[1, 1] ← TRUE
Booked[3, 6] ← TRUE
Free ← 0
FOR Row ← 1 TO 5
FOR Seat ← 1 TO 8
IF NOT Booked[Row, Seat]
THEN
Free ← Free + 1
ENDIF
NEXT Seat
NEXT Row
OUTPUT FreeHow many elements does the array have? Which seat is Booked[3, 6]? What is output?
DECLARE Name : ARRAY[1:Rows, 1:Columns] OF TYPE has rows times columns elements. An element is Name[Row, Column]. Nested loops visit every element: the outer loop for the rows, the inner loop for the columns. A row total is set to zero inside the outer loop, before the inner one.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Step 1. The size. elements.
Step 2. The seat. Booked[3, 6] is row 3, seat 6.
Step 3. The count. Two different seats are booked: row 3 seat 6, and row 1 seat 1. Booking row 3 seat 6 a second time changes nothing. So seats are free, and the output is 38.
Answer: 40 elements; row 3, seat 6; output 38.
Log in to join the discussion.
Loading…