Grade 11 — Programming Recap

1D arrays · searching & sorting · text files · methods · dates · databases in Delphi.

Delphi · Paper 1

1D Arrays

Declare & use

Fixed-size array
var arrMarks : array[1..30] of Integer;
Assign / read one element
arrMarks[3] := 75; iX := arrMarks[3];
Display all elements
for i := 1 to iCount do redOut.Lines.Add(IntToStr(arrMarks[i]));
Sum & average
iSum := 0; for i := 1 to iCount do iSum := iSum + arrMarks[i]; rAvg := iSum / iCount;
Find the maximum
iMax := arrMarks[1]; for i := 2 to iCount do if arrMarks[i] > iMax then iMax := arrMarks[i];
Index from 1 when you declare [1..n]. Keep a separate iCount for how many slots are really used.

Dynamic arrays & facts

SetLength(arr,n)Set / change size to n
Length(arr)Number of elements
Low(arr)First index (0 for dynamic)
High(arr)Last index (Length − 1)
Dynamic array — size set at runtime
var arrNum : array of Integer; begin SetLength(arrNum, 5); // index 0..4 for i := Low(arrNum) to High(arrNum) do arrNum[i] := i * 2;
Watch the base: [1..n] starts at 1, but a dynamic array always starts at 0.

Linear search (any array)

bFound := False; for i := 1 to iCount do if arrNames[i] = sTarget then begin bFound := True; Break; end;
Use when the array is unsorted. Checks every item until found.

Binary search (sorted array)

iLow := 1; iHigh := iCount; bFound := False; while (iLow <= iHigh) and not bFound do begin iMid := (iLow + iHigh) div 2; if arr[iMid] = iTarget then bFound := True else if arr[iMid] < iTarget then iLow := iMid+1 else iHigh := iMid-1; end;
Array must be sorted first. Halves the range each step — much faster.

Sorting — bubble & selection

Bubble sort (ascending)
for i := iCount-1 downto 1 do for j := 1 to i do if arr[j] > arr[j+1] then begin iTemp := arr[j]; arr[j] := arr[j+1]; arr[j+1] := iTemp; end;
Descending: change > to <.
Selection sort (ascending)
for i := 1 to iCount-1 do for j := i+1 to iCount do if arr[j] < arr[i] then begin iTemp := arr[i]; arr[i] := arr[j]; arr[j] := iTemp; end;
One pass of [5, 3, 8, 1] → bubble
3
5
1
8
Sorted
1
3
5
8
Parallel arrays: when you swap arrNames[i] you must swap arrMarks[i] at the same time so the two stay in sync.

Text Files

The commands

AssignFile(F,'d.txt')Link variable F to the file on disk
Reset(F)Open existing file for reading
Rewrite(F)Create / overwrite for writing
Append(F)Open & add to the end
ReadLn(F,s)Read one line into s
WriteLn(F,s)Write s as one line
EOF(F)True at end of file
CloseFile(F)Save & close — never forget!
FileExists('d.txt')True if the file exists
Rewrite warning: it wipes the old contents. Use Append to keep them.

Read · write · append

Read every line
if FileExists('Data.txt') then begin AssignFile(F, 'Data.txt'); Reset(F); while not EOF(F) do begin ReadLn(F, sLine); redOut.Lines.Add(sLine); end; CloseFile(F); end;
Write (new) / append (add)
AssignFile(F, 'Out.txt'); Rewrite(F); // or Append(F); WriteLn(F, sName + ',' + IntToStr(iAge)); CloseFile(F);

Procedures & Functions

Procedure vs function

ProcedureDoes a task — returns nothing
FunctionCalculates & returns one value
Call procedureWrite its name as a statement
Call functionUse it in an assignment / expression
Result is the special variable a function returns. Always set it before the function ends.

Examples

Procedure (no return)
procedure ShowGreeting(sName: String); begin ShowMessage('Hi ' + sName); end;
Function (returns Real)
function CalcVAT(rPrice: Real): Real; begin Result := rPrice * 0.15; end;
Calling them
ShowGreeting('Sam'); rVAT := CalcVAT(rPrice);

Parameters & scope

By value — a copy (original safe)
procedure Demo(iNum: Integer);
By reference — original changes
procedure Demo(var iNum: Integer);
LocalDeclared inside — visible there only
GlobalDeclared above — visible everywhere
ParameterPlaceholder in the header
ArgumentActual value you pass in

Date & Time Functions

Functions

NowCurrent date & time
DateToday's date
DateToStr(d)Date → text
StrToDate(s)Text → date
YearOf(d) / MonthOf / DayOfPull out the part you need
IsLeapYear(y)True / False

Examples

lblToday.Caption := DateToStr(Now); // 2026/06/02 iAge := YearOf(Now) - YearOf(dtpBirth.Date); if IsLeapYear(2024) then ShowMessage('Leap year');
Units: YearOf, MonthOf and IsLeapYear need DateUtils in the uses clause.

Databases in Delphi No SQL — that's Grade 12

The components

TADOConnectionLinks the project to the .mdb file
TADOTableOne table — needs the connection
TDataSourceThe pipe between data & controls
TDBGridShows records on the form
TDBNavigatorFirst/prev/next/last/add/delete
Chain: ADOConnection → ADOTable → DataSource → DBGrid.

Navigate & read fields

Move through records
tblStud.First; tblStud.Next; tblStud.Prior; tblStud.Last;
Read a field of the current record
sName := tblStud['Name']; iMark := tblStud['Mark'];
Loop through ALL records
tblStud.First; while not tblStud.Eof do begin redOut.Lines.Add(tblStud['Name']); tblStud.Next; // never forget this! end;

Add · edit · delete a record

Insert a new record
tblStud.Insert; // 1. blank record tblStud['Name'] := edtName.Text; tblStud['Mark'] := sedMark.Value; tblStud.Post; // 2. save it
Edit the current record
tblStud.Edit; tblStud['Mark'] := 90; tblStud.Post;
Delete · sort
tblStud.Delete; tblStud.Sort := 'Surname ASC';
Edit/Insert → Post to save, or Cancel to undo. Always confirm before Delete — there is no undo.