Course 8.3Lesson 1 of 3
One variable holds one value, but a class has thirty names. This lesson declares one-dimensional arrays in Cambridge pseudocode, writes values into them and reads them back with loops, and compares them with Python lists.
Try the code and the simulations beside the lesson.
A teacher keeps thirty names. Thirty variables, Name1 to Name30, would be thirty lines to input and thirty lines to output. An array keeps them all under one identifier. Each value is an element, and a whole number, the index, says which one.
In the syllabus an array has a fixed length, and every element has the same data type. You met the Python form, the list, in the Python Foundations chapter "Lists: Many Values in One Variable". A Python list can grow and can mix types; a pseudocode array cannot.
DECLARE StudentNames : ARRAY[1:30] OF STRING
DECLARE NoughtsAndCrosses : ARRAY[1:3, 1:3] OF CHARARRAY[1:30] gives the lower bound 1 and the upper bound 30, so there are 30 elements. It is good practice to state the lower bound, because some systems start at 0 and some at 1. The syllabus usually uses 1. Python lists always start at 0.
![An array named Rainfall drawn as a row of seven boxes holding 4, 0, 12, 7, 0, 3 and 9. Above the boxes are the pseudocode indexes [1] to [7], below them the Python indexes 0 to 6. The third box, holding 12, is shaded, with the label Rainfall[3] = 12.](https://tuygolavdykfydmasiqc.supabase.co/storage/v1/object/public/discussion-images/57b80fd5-0d05-40b0-bb7c-fdadd190c00d/figures/ch08-programming-in-pseudocode-and-python/ics-g3-array.png)
The picture shows an array of seven rainfall readings. Rainfall[3] is 12. In Python the same element is rainfall[2], one less.
An element is used just like a variable, with its index in square brackets: StudentNames[1] ← "Ali". The index can be a variable or an expression: StudentNames[n+1] ← StudentNames[n]. That is what makes arrays powerful: a loop counter can be the index.
A FOR loop visits every element, one after the other. The syllabus example sets every name to the empty string:
FOR Index ← 1 TO 30
StudentNames[Index] ← ""
NEXT IndexHere is a full program. The first loop inputs a week of rainfall into the array. The second loop reads the elements back to find the total and the wettest day.
DECLARE Rainfall : ARRAY[1:7] OF INTEGER
DECLARE Day : INTEGER
DECLARE Total : INTEGER
DECLARE Wettest : INTEGER
DECLARE WettestDay : INTEGER
FOR Day ← 1 TO 7
OUTPUT "Rainfall in mm for day ", Day
INPUT Rainfall[Day]
NEXT Day
Total ← 0
Wettest ← Rainfall[1]
WettestDay ← 1
FOR Day ← 1 TO 7
Total ← Total + Rainfall[Day]
IF Rainfall[Day] > Wettest
THEN
Wettest ← Rainfall[Day]
WettestDay ← Day
ENDIF
NEXT Day
OUTPUT "Total ", Total, " mm"
OUTPUT "Wettest day ", WettestDayFor the inputs 4, 0, 12, 7, 0, 3 and 9 millimetres, the output is Total 35 mm and then Wettest day 3. Look at the start of the second loop: Wettest begins as the first element, not as 0. Then a reading only replaces it when it is larger.
A linear search checks the elements one by one until it finds the value or runs out of elements. You will meet it as a standard method in the chapter on algorithms. Here it is on an array:
DECLARE Names : ARRAY[1:5] OF STRING
DECLARE Index : INTEGER
DECLARE Wanted : STRING
DECLARE Found : BOOLEAN
Names[1] ← "Ali"
Names[2] ← "Bea"
Names[3] ← "Cem"
Names[4] ← "Dana"
Names[5] ← "Eli"
INPUT Wanted
Found ← FALSE
Index ← 1
WHILE Found = FALSE AND Index <= 5 DO
IF Names[Index] = Wanted
THEN
Found ← TRUE
ELSE
Index ← Index + 1
ENDIF
ENDWHILE
IF Found
THEN
OUTPUT Wanted, " is at position ", Index
ELSE
OUTPUT Wanted, " is not in the list"
ENDIFWith the input Dana, the loop stops when Index is 4, and the output is Dana is at position 4. With Fay, Index goes past 5, and the output is Fay is not in the list. The test Index <= 5 stops the loop before it reads an element that does not exist.
The program is the rainfall example with a Python list. The readings are typed into the program, so it runs at once.
Rainfall[8] does not exist in ARRAY[1:7].Total ← Total + Rainfall has no index. Write Rainfall[Day].DECLARE Name : ARRAY[Lower:Upper] OF TYPE.Name[Index], and the index may be a variable or an expression.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
ARRAY[0:9] OF REAL have?Rainfall.Index <= 5?Rainfall[1]?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place in the course.