ARRAYS – TEMPERATURES
A program must store the temperatures of 7 days.
Declare an array to store 7 real numbers.
Use a loop and InputBox to capture temperatures.
Display all temperatures.
Count how many days had temperatures above 30°C.
Delphi Code
var
temps: array[1..7] of real;
i, hotDays: integer;
begin
hotDays := 0;
for i := 1 to 7 do
temps[i] := StrToFloat(InputBox('Input', 'Enter
temperature:', ''));
for i := 1 to 7 do
begin
[Link](FloatToStr(temps[i]));
if temps[i] > 30 then
Inc(hotDays);
end;
ShowMessage('Hot days: ' + IntToStr(hotDays));
end;
ARRAYS AND PROCEDURES
PROCEDURE – SALES TOTAL
Write a procedure that:
accepts an array of 5 sales values
calculates the total
displays the result
Code
procedure CalculateTotal(arr: array of real);
var
i: integer;
total: real;
begin
total := 0;
for i := 0 to High(arr) do
total := total + arr[i];
ShowMessage('Total sales: ' + FloatToStr(total));
end;
Calling the procedure
var
sales: array[0..4] of real;
i: integer;
begin
for i := 0 to 4 do
sales[i] := StrToFloat(InputBox('Input','Enter sale:',
''));
CalculateTotal(sales);
end;
SEARCH FOR A PRODUCT
A program stores 5 product names in an array.
The user enters a product to search for.
Display:
"Product found" or
"Product not found"
Code
var
products: array[1..5] of string;
i: integer;
search: string;
found: boolean;
begin
found := False;
for i := 1 to 5 do
products[i] := InputBox('Input','Enter product:', '');
search := InputBox('Search','Enter product to find:', '');
for i := 1 to 5 do
if products[i] = search then
found := True;
if found then
ShowMessage('Product found')
else
ShowMessage('Product not found');
end;
IF STATEMENTS – AGE CATEGORY
A learner enters their age in an Edit box.
Age Output
≥ 18 Adult
13–17 Teen
< 13 Child
Code
var
age: integer;
begin
age := StrToInt([Link]);
if age >= 18 then
ShowMessage('Adult')
else if age >= 13 then
ShowMessage('Teen')
else
ShowMessage('Child');
end;
NESTED IF STATEMENTS
MARK EVALUATION
A program must:
1. Input assignment and test marks
2. Calculate the average
3. Display performance
Average Output
≥ 80 Excellent
≥ 60 Good
Average Output
≥ 50 Pass
< 50 Fail
Code
var
assignMark, testMark: integer;
avg: real;
begin
assignMark := StrToInt(InputBox('Input','Enter assignment
mark:', ''));
testMark := StrToInt(InputBox('Input','Enter test mark:',
''));
avg := (assignMark + testMark) / 2;
if avg >= 80 then
ShowMessage('Excellent')
else
if avg >= 60 then
ShowMessage('Good')
else
if avg >= 50 then
ShowMessage('Pass')
else
ShowMessage('Fail');
end;
FUNCTION – LOWEST VALUE
Write a function that returns the lowest number in an array.
Code
function GetLowest(arr: array of integer): integer;
var
i, min: integer;
begin
min := arr[0];
for i := 1 to High(arr) do
if arr[i] < min then
min := arr[i];
Result := min;
end;
Calling the function
var
nums: array[0..4] of integer;
i: integer;
begin
for i := 0 to 4 do
nums[i] := StrToInt(InputBox('Input','Enter number:',
''));
ShowMessage('Lowest value: ' + IntToStr(GetLowest(nums)));
end;