Grade 12 · Two-dimensional arrays · Lesson and coding task · about 16 minutes
Open this task in the Delphi workspace →
Processing a 2D array is 1D patterns aimed along a row or a column:
Row total — reset inside the outer loop, accumulate in the inner:
for iRow := 1 to 3 do
begin
iRowTot := 0; // reset PER ROW
for iCol := 1 to 4 do
iRowTot := iRowTot + arrRain[iRow, iCol];
// … use iRowTot …
end;
Where you reset the total decides what it measures: before the outer loop = grand total; inside it = row total. That placement question is worth marks nearly every year.
Biggest row — the highest-value pattern from Grade 11, applied to row totals: keep the best total and its row number so you can name the province.
Your task
Same rainfall grid. Report each province's total, the grand total, and crown the wettest province.
What you will do
- Lines 1-3: Gauteng: 37 mm (each province's row total)
- Line 4: Total: 105 mm (the grand total)
- Line 5: Wettest: Western Cape (46 mm)
Hints
- iGrand resets once before the outer loop; iRowTot resets at the top of every outer lap.
- After the inner loop you have the row total — memo it, add it to iGrand, and challenge the current wettest.
- Track iWet (best total) and iWetRow (which row) together, like top-learner in Grade 11.
Model solution
const
arrProv : array[1..3] of String = ('Gauteng', 'KwaZulu-Natal', 'Western Cape');
arrRain : array[1..3, 1..4] of Integer = ((12, 0, 5, 20), (3, 8, 0, 11), (25, 14, 7, 0));
procedure TfrmRainStats.btnStatsClick(Sender: TObject);
var
iRow, iCol, iRowTot, iGrand, iWet, iWetRow : Integer;
begin
iGrand := 0;
iWet := -1;
iWetRow := 1;
for iRow := 1 to 3 do
begin
iRowTot := 0;
for iCol := 1 to 4 do
iRowTot := iRowTot + arrRain[iRow, iCol];
memOutput.Lines.Add(arrProv[iRow] + ': ' + IntToStr(iRowTot) + ' mm');
iGrand := iGrand + iRowTot;
if iRowTot > iWet then
begin
iWet := iRowTot;
iWetRow := iRow;
end;
end;
memOutput.Lines.Add('Total: ' + IntToStr(iGrand) + ' mm');
memOutput.Lines.Add('Wettest: ' + arrProv[iWetRow] + ' (' + IntToStr(iWet) + ' mm)');
end;