Year 10 · Computer Science · Summer 1
Programming · Lesson 5 of 5
Arrays and files; the scenario question
Exam practice8.2 Arrays8.3 File handling7.9 Writing and amending algorithms
Year 10 · Computer Science · Programming · Lesson 5 of 5
You will learn to
- Declare and use a 1D array, with a loop and a variable as the index.
- Use nested loops to work through a 2D array.
- Write data to a text file, close it, then open it and read the data back.
- Plan a 15-mark scenario question before writing any code.
Year 10 · Computer Science · Programming · Lesson 5 of 5
Success criteria
You have got it when you can say:
- I can total every score in a 1D array with a loop.
- I can find an empty seat in a 2D seating plan with nested loops.
- I can save a list to a file and read it back.
- I can list the steps my scenario answer needs before I write it.
Year 10 · Computer Science · Programming · Lesson 5 of 5
Retrieval starter
Questions from before. Write your answers, then show them.
- Last lesson
A local variable…
A Never changesB Can be used anywhereC Exists only inside its subroutine
C. Exists only inside its subroutine
- Last unit
A range check on age 0 to 120 would reject…
A 120B 0C 150
C. 150
- Earlier this year
Adding 11001100 and 01011010 in an 8-bit register gives…
A An overflow errorB 00100110 with no problemC 11111111
A. An overflow error
Year 10 · Computer Science · Programming · Lesson 5 of 5
Key words (1 of 4)
- Variable
- A named place in memory whose value can change while the program runs.
- Identifier
- The name given to a variable, constant, array or subroutine.
- Data type
- The kind of value stored: INTEGER, REAL, CHAR, STRING or BOOLEAN.
- Selection
- Choosing which statements to run using a condition (IF or CASE).
Year 10 · Computer Science · Programming · Lesson 5 of 5
Key words (2 of 4)
- Iteration
- Repeating a block of statements in a loop.
- Totalling
- Adding values into a running total, for example Total ← Total + Value.
- Counting
- Adding 1 to a counter each time something happens.
- Array
- A data structure holding many values of the same data type under one name, each found by its index.
Year 10 · Computer Science · Programming · Lesson 5 of 5
Key words (3 of 4)
- Algorithm
- A precise, ordered set of steps that solves a problem.
- Program development life cycle
- The four stages of making a program: analysis, design, coding and testing.
- Structure diagram
- A top-down diagram that shows how a system is split into sub-systems.
- Pseudocode
- A structured, code-like way of writing an algorithm that is not tied to one language.
Year 10 · Computer Science · Programming · Lesson 5 of 5
Key words (4 of 4)
- Bubble sort
- Sorting by comparing neighbouring items and swapping them if they are in the wrong order, pass after pass.
- Validation
- An automatic check that input data is reasonable and follows set rules.
- Check digit
- An extra digit calculated from the other digits, used to catch typing errors.
8.2 Arrays · The explanation · step 1 of 13 · part 1 of 3
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.
8.2 Arrays · The explanation · step 1 of 13 · part 2 of 3
What an array is (continued)
| Pseudocode index | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Python index | 0 | 1 | 2 | 3 | 4 |
| Value | 56 | 71 | 48 | 90 | 65 |
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]
8.2 Arrays · The explanation · step 1 of 13 · part 3 of 3
What an array is (continued)
marks = [56, 71, 48, 90, 65] print(marks[3])
One name plus an index means a loop can visit every element. The same few lines work for 5 values or 5000.
Python 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.
8.2 Arrays · The explanation · step 2 of 13 · part 1 of 5
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.
8.2 Arrays · The explanation · step 2 of 13 · part 2 of 5
Using a variable as the index (continued)
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]
8.2 Arrays · The explanation · step 2 of 13 · part 3 of 5
Using a variable as the index (continued)
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: ", Highest8.2 Arrays · The explanation · step 2 of 13 · part 4 of 5
Using a variable as the index (continued)
marks = [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)8.2 Arrays · The explanation · step 2 of 13 · part 5 of 5
Using a variable as the index (continued)
Highest starts at the first element, not at 0. If every value were negative, starting at 0 would give the wrong answer.
8.2 Arrays · The explanation · step 3 of 13 · part 1 of 4
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.
8.2 Arrays · The explanation · step 3 of 13 · part 2 of 4
2D arrays: rows and columns (continued)
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.
| Scores | Column 1 | Column 2 | Column 3 | Column 4 |
|---|---|---|---|---|
| Row 1 (Ana) | 7 | 5 | 9 | 6 |
| Row 2 (Ben) | 8 | 6 | 4 | 9 |
| Row 3 (Carla) | 5 | 9 | 7 | 8 |
8.2 Arrays · The explanation · step 3 of 13 · part 3 of 4
2D arrays: rows and columns (continued)
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]
8.2 Arrays · The explanation · step 3 of 13 · part 4 of 4
2D arrays: rows and columns (continued)
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])Row 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.
The explanation · 10 more steps
More on the lesson page
The lesson page has the rest of the explanation, step by step:
- Nested loops to visit every element
- Initialising a 2D array
- Why programs store data in files
- Open, read or write, close
- Writing and reading a single item
- Writing and reading lines of text with a loop
- Be precise, and amend with care
- The 15-mark scenario question: plan first
- Model answer in pseudocode and Python
- How the 15 marks are awarded
Year 10 · Computer Science · Programming · Lesson 5 of 5
Quick quiz · 1 of 3
SUBSTRING("Computer", 1, 3) gives…
- A omp
- B Com
- C Computer
B. Com
Start at position 1, take 3 characters. Cambridge strings start at 1.
Year 10 · Computer Science · Programming · Lesson 5 of 5
Quick quiz · 2 of 3
FOR i ← 1 TO 10 … NEXT i runs the loop…
- A 10 times
- B 11 times
- C 9 times
A. 10 times
1 to 10 inclusive. Python's range(1, 11) does the same.
Year 10 · Computer Science · Programming · Lesson 5 of 5
Quick quiz · 3 of 3
A REPEAT … UNTIL loop…
- A May run zero times
- B Runs for ever
- C Always runs at least once
C. Always runs at least once
The test is at the end, so the body runs first. WHILE tests first and may run zero times.
Year 10 · Computer Science · Programming · Lesson 5 of 5
Exit ticket
Answer on paper before you go. Then check.
17 MOD 5 is…
A 3.4B 2C 3
B. 2 MOD is the remainder: 17 = 3 × 5 + 2. DIV would give 3.
A function differs from a procedure because it…
A Returns a valueB Can have parametersC Has a name
A. Returns a value Both have names and parameters. Only a function gives a value back.
Year 10 · Computer Science · Programming · Lesson 5 of 5
Homework and the challenge
Homework
The CS 8 quiz.
End-of-unit challenge
End-of-unit test: a scenario programming question (about 15 marks), plus short questions on topic 8.