Information Technology Topic
One-Dimensional Arrays
- Read notes
- Flashcards
- Quiz
- Confident
Study focus - 3 things
- An array holds several values of the same data type under one name, and each value, called an element, is picked out by its index: marks[3] is the element at position 3
- In Pascal-style code array[1..5] of integer holds 5 elements, because both bounds are included, and the programmer chooses the bounds; they do not have to start at 0
- A for loop whose counter is the index traverses the array, and an index outside the declared range is an error (a run-time error when range checking is on)
Section 1One name, many values
A variable holds one value. If a teacher wants to store 40 test marks, 40 separate variables would need 40 different names and 40 separate lines every time you wanted to use them. An array solves this. It holds several values of the same data type under one name. Each value is an element, and a number called the index says which element you mean. Picture a row of numbered boxes. The row is called marks, the boxes are numbered 1 to 5, and box 1 holds 62, box 2 holds 75, and so on. The element at position 3 is written marks[3]. Here's the trick: the index is the box number and the element is what is inside the box. They are different things.
Section 2Declaring an array in Pascal
In Pascal-style code you declare an array in the var section, giving the lowest and highest index and the type of every element: var marks : array[1..5] of integer; Both bounds are included, so array[1..5] has 5 elements. The size is high - low + 1. The programmer chooses the bounds, so array[0..4] and array[10..14] also hold 5 elements. The size is fixed before the program runs. An array of 5 cannot grow to hold a sixth value. Every element has the same type. An array of integer holds whole numbers only. Watch out: do not assume an array starts at 0. In Pascal the declaration decides, and in this topic the arrays start at 1 unless a question says otherwise.
Section 3Writing to and reading from an element
Writing puts a value into an element, so the element goes on the left of :=. Reading uses the value in an element, so it goes in an expression or in writeln. Here is a complete Pascal-style program: program FirstElements; var marks : array[1..5] of integer; begin marks[1] := 62; marks[2] := 75; marks[3] := marks[1] + marks[2]; writeln(marks[3]) end. The first two lines write 62 and 75. The third line reads both, works out 62 + 75 = 137 and writes 137 into marks[3]. The last line displays 137. The index can also be a variable or a calculation, such as marks[index] or marks[index + 1]. That is what makes loops so useful with arrays. An element can be filled from the keyboard too: readln(marks[2]) stores the typed value in the element at position 2.
Section 4Filling an array with a loop
Typing five readln lines is slow. In Pascal-style code, a for loop whose counter is the index does the job: for index := 1 to 5 do readln(marks[index]); If the values typed are 62, 75, 48, 90 and 55, then pass 1 stores 62 in marks[1], pass 2 stores 75 in marks[2], and so on up to marks[5] holding 55. The loop limits match the declaration: 1 to 5 for array[1..5]. Easy to mix up: the loop counter is not the value, it is the position. Index 3 holds whatever was typed third.
Section 5Traversing an array
To traverse an array is to visit every element in turn. The usual tool is a for loop with the index as its counter. To add up the five marks, in Pascal-style code: total := 0; for index := 1 to 5 do total := total + marks[index]; writeln(total) With the marks 62, 75, 48, 90 and 55 this displays 330, because 62 + 75 + 48 + 90 + 55 = 330. Each pass reads one element and adds it to total. To show the elements in reverse order, count down: for index := 5 downto 1 do writeln(marks[index]) This displays 55, 90, 48, 75 and 62, one per line.
Section 6An index outside the range is an error
An array declared as array[1..5] has no marks[6] and no marks[0]. If a program tries to use one, there is no such box. If the index is a variable, the mistake does not show when the program is compiled. It shows when that line runs, so it is a run-time error. Free Pascal reports it as error 201, a range check error, when range checking is switched on. With the check off, the program may carry on using whatever memory sits beyond the array, which is worse. A constant index such as marks[6] is different: the compiler can see the mistake and refuses to compile the program. index := 6; writeln(marks[index]) This Pascal-style piece asks for the sixth element of a five-element array, so (with range checking on) it stops with a run-time error. The usual cause is a loop that goes one step too far: for index := 1 to 6 do readln(marks[index]) works for five passes and fails on the sixth. Here's the trick: whenever you write a loop over an array, say the declaration bounds and the loop bounds together out loud. They must match.
Section 7Changing elements and swapping two of them
A loop can change elements as well as read them. The line marks[index] := marks[index] + 10 reads the element, adds 10 and writes the new value back into the same element. Swapping two elements needs a spare variable, temp, exactly like swapping two ordinary variables. With marks[1] = 4, marks[2] = 6 and marks[3] = 2, this Pascal-style piece swaps the first and third elements: temp := marks[1]; marks[1] := marks[3]; marks[3] := temp After it, marks[1] is 2, marks[2] is still 6 and marks[3] is 4. The first line saves the 4 so it is not lost when marks[1] is overwritten.
Examples
A teacher in Kingston types in five test marks and wants them displayed with their positions and added up. The marks typed are 62, 75, 48, 90 and 55. program MarkTotal; var marks : array[1..5] of integer; index : integer; total : integer; begin total := 0; for index := 1 to 5 do begin readln(marks[index]); total := total + marks[index] end; for index := 1 to 5 do writeln('Mark ', index, ': ', marks[index]); writeln('Total: ', total) end. (a) State how many elements the array holds and which indexes are valid. (b) State the value held in marks[3] after the first loop. (c) State the lines displayed. (d) Both loops were changed to run to 6. State what happens.
(a) The array holds 5 elements, because 5 - 1 + 1 = 5. The valid indexes are 1, 2, 3, 4 and 5. (b) marks[3] holds 48, the third value typed. (c) Mark 1: 62, Mark 2: 75, Mark 3: 48, Mark 4: 90, Mark 5: 55 and then Total: 330, each on its own line. (d) On the sixth pass of the first loop the program tries to read into marks[6], which does not exist. With range checking on, that is a run-time error (a range check error), the program stops, and nothing is displayed because the display comes later.
A shop in Montego Bay stores four prices in an array. It adds 50 dollars to each price and finds the new total. program PriceRise; var prices : array[1..4] of integer; item : integer; total : integer; begin prices[1] := 200; prices[2] := 350; prices[3] := 150; prices[4] := 400; total := 0; for item := 1 to 4 do begin prices[item] := prices[item] + 50; total := total + prices[item] end; writeln('New total: ', total) end. (a) Show item, the new price and total after each pass. (b) State the line displayed and the value of prices[3] at the end. (c) Explain what is read and what is written in the line prices[item] := prices[item] + 50. (d) The loop heading is changed to for item := 1 to 5 do. State what happens and when.
(a) Pass 1: item 1, new price 250, total 250. Pass 2: item 2, new price 400, total 650. Pass 3: item 3, new price 200, total 850. Pass 4: item 4, new price 450, total 1300. (b) New total: 1300. prices[3] ends as 200, because 150 + 50 = 200. (c) The right side reads the old value of the element, and 50 is added to it. The left side writes the answer back into the same element, replacing the old value. (d) The first four passes work as before, with the total reaching 1300. On the fifth pass the program uses prices[5], which is outside 1..4, so (with range checking on) it stops with a run-time error and the final line is never displayed.
Now let's water it
Session runs notes to flashcards to quiz back-to-back.
Where this comes from
- CSEC Information Technology Syllabus (CXC 30/G/SYLL 08, revised 2000, 2008, 2013) · Caribbean Examinations Council (CXC) · Section 3, Specific Objective 9 · Syllabus scope
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 3, Static arrays (3.3.1; array[low..high] of type, both bounds included, size fixed at compile time, index in square brackets), checked against (https://www.freepascal.org/docs-html/ref/refsu14.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter 13, Assignments (13.1.1; the right-hand side is evaluated and stored in the left-hand side), checked against (https://www.freepascal.org/docs-html/ref/refsu52.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Page ReadLn, and WriteLn, in the run-time library reference (read values into variables; write values and start a new line), checked against (https://www.freepascal.org/docs-html/rtl/system/readln.html) and (https://www.freepascal.org/docs-html/rtl/system/writeln.html) · Facts
- Free Pascal Reference guide v3.2.2 (16.1 Programs, 16.5 Blocks and the language pages in the facts sheet), RTL entries Write, Writeln, ReadLn, and User's guide 3.2 and Appendix D · Free Pascal team · Chapter D, Run-time errors (Appendix D of the User guide; error 201 is a range check error), checked against (https://www.freepascal.org/docs-html/user/userap4.html) · Facts