Grade 11 · One-dimensional arrays · Lesson and coding task · about 12 minutes
Open this task in the Delphi workspace →
An array is a row of variables sharing one name, numbered from a low to a high index. In SA schools we almost always start at 1:
var
arrMarks : array[1..5] of Integer;
begin
arrMarks[1] := 78; // first box
arrMarks[5] := 67; // last box
Arrays and FOR loops are inseparable — the loop variable becomes the index:
for i := 1 to 5 do
memOutput.Lines.Add(IntToStr(arrMarks[i]));
Parallel arrays hold related data at matching positions: arrNames[3] and arrMarks[3] describe the same learner. Exam papers give you data as constant arrays exactly as in this exercise's starter code.
Go outside the range (like index 6 in a 1..5 array) and your program crashes — try it and read the error we give you.
What you will do
- Use one FOR loop over the two parallel constant arrays
- Each memo line pairs a name and mark: Thabo: 78
Hints
- for i := 1 to 5 do — the same i indexes both arrays.
- The line is arrNames[i] + ': ' + IntToStr(arrMarks[i])
Model solution
const
arrNames : array[1..5] of String = ('Thabo', 'Lerato', 'Anele', 'Sipho', 'Zanele');
arrMarks : array[1..5] of Integer = (78, 45, 90, 33, 67);
procedure TfrmClass.btnListClick(Sender: TObject);
var
i : Integer;
begin
for i := 1 to 5 do
memOutput.Lines.Add(arrNames[i] + ': ' + IntToStr(arrMarks[i]));
end;