What an array is
An array is a data structure that holds many values of the same data type under one name.
Each value is an element. Each element has a position number called its index.
Without an array, 30 marks need 30 variables. With an array they need one name: `Marks`.
`ARRAY[1:5]` means the first index is 1 and the last is 5. The first index can be 0 or 1, and the exam says which.
DECLARE Marks : ARRAY[1:5] OF INTEGER
Marks[1] β 56
Marks[2] β 71
Marks[3] β 48
Marks[4] β 90
Marks[5] β 65
OUTPUT Marks[4]
marks = [56, 71, 48, 90, 65]
print(marks[3])
?Why?One name plus an index means a loop can visit every element. The same few lines work for 5 values or 5000.
!Watch outPython lists always start at index 0. `marks[3]` in Python is the fourth element, the same one as `Marks[4]` in a pseudocode array that starts at 1.
Using a variable as the index
The index can be a variable. `Marks[Count]` means the element whose index is the value in Count.
Put that inside a FOR loop, and the loop visits every element in turn.
e.g.ExampleMarks typed: 56, 71, 48, 90, 65
Total = 56 + 71 + 48 + 90 + 65 = 330
Highest starts at 56. 71 > 56, so 71. 48 is not bigger. 90 > 71, so 90. 65 is not bigger.
Output: Total: 330 and Highest: 90
DECLARE Marks : ARRAY[1:5] OF INTEGER
DECLARE Count : INTEGER
DECLARE Total : INTEGER
DECLARE Highest : INTEGER
FOR Count β 1 TO 5
OUTPUT "Enter mark ", Count
INPUT Marks[Count]
NEXT Count
Total β 0
Highest β Marks[1]
FOR Count β 1 TO 5
Total β Total + Marks[Count]
IF Marks[Count] > Highest
THEN
Highest β Marks[Count]
ENDIF
NEXT Count
OUTPUT "Total: ", Total
OUTPUT "Highest: ", Highestmarks = [0] * 5
for count in range(5):
marks[count] = int(input("Enter mark " + str(count + 1) + ": "))
total = 0
highest = marks[0]
for count in range(5):
total = total + marks[count]
if marks[count] > highest:
highest = marks[count]
print("Total:", total)
print("Highest:", highest)?Why?Highest starts at the first element, not at 0. If every value were negative, starting at 0 would give the wrong answer.
2D arrays: rows and columns
A 2D array is a table of values with rows and columns. Every element has the same data type.
`ARRAY[1:3, 1:4]` has 3 rows and 4 columns, so 3 Γ 4 = 12 elements.
You give two indexes: row first, then column. `Scores[2, 3]` is row 2, column 3.
Try the toy: tap a cell to read its address and value, then type a new value in Change the value. Then press Step to run the nested loop total. The toy uses different marks from the table below, so its Scores[2, 3] holds 16.
e.g.ExampleScores[2, 3] is row 2 (Ben), column 3 β 4.
After Scores[2, 3] β 10, Ben's third test is 10.
Output: 4, then 10
DECLARE Scores : ARRAY[1:3, 1:4] OF INTEGER
// (Scores holds the values in the table)
OUTPUT Scores[2, 3]
Scores[2, 3] β 10
OUTPUT Scores[2, 3]
scores = [[7, 5, 9, 6],
[8, 6, 4, 9],
[5, 9, 7, 8]]
print(scores[1][2])
scores[1][2] = 10
print(scores[1][2])!Watch outRow first, then column. In Python each row is its own list, so you write `scores[row][column]` with two pairs of brackets, and both start at 0.
There is something to try for this step in the IGCSE library.
Nested loops to visit every element
To visit every element of a 2D array, use a nested loop: one loop inside another.
The outer loop goes through the rows. The inner loop goes through the columns of the current row.
The inner loop does all its passes for each single pass of the outer loop. Here that is 3 Γ 4 = 12 passes in total.
e.g.ExampleRow 1: 7 + 5 + 9 + 6 = 27
Row 2: 8 + 6 + 4 + 9 = 27
Row 3: 5 + 9 + 7 + 8 = 29
Grand total: 27 + 27 + 29 = 83
DECLARE Scores : ARRAY[1:3, 1:4] OF INTEGER
DECLARE Row : INTEGER
DECLARE Column : INTEGER
DECLARE RowTotal : INTEGER
DECLARE GrandTotal : INTEGER
// (Scores holds the values in the table above)
GrandTotal β 0
FOR Row β 1 TO 3
RowTotal β 0
FOR Column β 1 TO 4
RowTotal β RowTotal + Scores[Row, Column]
NEXT Column
OUTPUT "Row ", Row, " total: ", RowTotal
GrandTotal β GrandTotal + RowTotal
NEXT Row
OUTPUT "Grand total: ", GrandTotalscores = [[7, 5, 9, 6],
[8, 6, 4, 9],
[5, 9, 7, 8]]
grand_total = 0
for row in range(3):
row_total = 0
for column in range(4):
row_total = row_total + scores[row][column]
print("Row", row + 1, "total:", row_total)
grand_total = grand_total + row_total
print("Grand total:", grand_total)!Watch outSet RowTotal to 0 inside the outer loop but before the inner loop. In any other place, totals carry over from one row to the next, or reset too often.
Initialising a 2D array
Initialising means giving every element a starting value before the array is used.
A nested loop can set every element. Here a cinema has 5 rows of 8 seats. FALSE means the seat is free.
e.g.Example5 rows Γ 8 seats = 40 elements, all set to FALSE.
Booking row 2, seat 5 sets that one element to TRUE.
Output: TRUE FALSE (Python shows True False)
DECLARE Seats : ARRAY[1:5, 1:8] OF BOOLEAN
DECLARE Row : INTEGER
DECLARE Seat : INTEGER
FOR Row β 1 TO 5
FOR Seat β 1 TO 8
Seats[Row, Seat] β FALSE
NEXT Seat
NEXT Row
Seats[2, 5] β TRUE // book row 2, seat 5
OUTPUT Seats[2, 5], " ", Seats[1, 1]seats = [[False] * 8 for row in range(5)]
seats[1][4] = True # book row 2, seat 5
print(seats[1][4], seats[0][0])
!Watch outIn Python, do not write `[[False] * 8] * 5`. That makes five copies of the same row, so booking one seat books it in every row.