A pencil-and-paper workbook: think it through first, learn the Delphi, then prove you can do it.
Good programmers do not start typing. They plan on paper first. In this unit you will learn the planning tools: IPO tables, flowcharts, pseudocode and trace tables.
| Problem | Input | Process | Output |
|---|---|---|---|
| Total money from a taxi | passengers, fare | total = passengers × fare | total |
Fill in the empty cells. Write the Process as a formula.
| Problem | Input | Process | Output |
|---|---|---|---|
| Area of a netball court | |||
| Cost of loaves at R17 each | |||
| Average of three test marks |
START INPUT fare, people SET share TO fare / people OUTPUT share END
Pseudocode rules: key words in CAPITALS, indent inside IF, one action per line, begin with START and end with END.
A learner buys some pies. Draw the flowchart for this pseudocode. Use the correct shapes and arrows.
START INPUT price, number, paid SET total TO price * number SET change TO paid - total OUTPUT change END
The school raises money with tickets at R35 each. The expenses are R400. Write pseudocode that reads the number of tickets sold and outputs the profit.
A shop gives free delivery on orders of R200 or more. Otherwise delivery costs R25. Complete the trace table for each test value.
INPUT total IF total >= 200 THEN SET fee TO 0 ELSE SET fee TO 25 ENDIF SET pay TO total + fee OUTPUT pay
| Test value (total) | total >= 200 ? | fee | pay (output) |
|---|---|---|---|
| 150 | |||
| 200 | |||
| 320 | |||
| 199 |
All four algorithms try to find the average of three marks. Read them, then answer.
INPUT m1, m2, m3 SET average TO total / 3 SET total TO m1 + m2 + m3 OUTPUT average
INPUT m1, m2, m3 SET total TO m1 + m2 + m3 SET average TO total / 3 OUTPUT average
INPUT m1, m2, m3 SET total TO m1 + m2 + m3 SET total2 TO m1 + m2 + m3 SET average TO total / 3 OUTPUT average
Get the marks Add them up and do the sums Show the answer
| 1 | The steps are in the wrong order. | P Q R S |
| 2 | A step is too vague for a computer to follow. | P Q R S |
| 3 | Correct, with no wasted steps. | P Q R S |
| 4 | Repeats a calculation it has already done. | P Q R S |
| 5 | Which two give the right answer? | P+Q Q+R R+S |
Delphi lets you design a window by dragging components onto a form and then write code that brings it to life. First, learn your way around the workshop.
Press F12 to flip between the form and the code editor. Press F9 to compile and run. Save each project in its own folder (unit as _u.pas, project as _p.dpr).
Write the name of each part. The circled letters are on the picture.
| Letter | Name of the part |
|---|---|
| A | |
| B | |
| C | |
| D | |
| E |
Circle the part you would use.
| 1 | Drag a new button onto the form. | Tool Palette Object Inspector Menu bar |
| 2 | Change the Caption of a label. | Tool Palette Object Inspector Menu bar |
| 3 | See a list of every component on the form. | Structure panel Menu bar Tool Palette |
| 4 | Save All, or run the program. | Object Inspector Menu bar Form Designer |
procedure TForm1.btnGreetClick(Sender: TObject); begin lblWelcome.Caption := 'Welcome to the spaza shop'; // Component.Property := Value; btnGreet.Caption := 'Thanks'; lblWelcome.Font.Color := clGreen; // spelt Color, not Colour edtName.Clear; // a method memOut.Hide; // another method end;
Write the prefix, then a good name (prefix + meaning) for the component described.
| Component | Prefix | A good name for… |
|---|---|---|
| TButton | ||
| TLabel | ||
| TEdit | ||
| TMemo |
Each line has one error. Write the correct line.
lblResult.Caption = 'Total'; 'Welcome' := lblMsg.Caption; btnOk.Caption := Click me; edtName.Text := 'Thabo' lblMsg.Font.Colour := clBlue;
When btnReset is clicked it must: set lblTotal to R0.00; clear edtQty; change its own caption to Cleared; and hide memReceipt.
procedure TForm1.btnResetClick(Sender: TObject);
A TOE chart lists each Task, the Object that does it and the Event that starts it. A memo that only displays results has the event “None”. Complete the chart for a till program.
| Task | Object | Event |
|---|---|---|
| Work out the total | ||
| Clear the screen for the next customer | ||
| Show the receipt lines |
A program must remember things while it runs. It keeps them in variables: labelled boxes in memory. In this unit you learn to declare variables, choose the right type, and move data in and out.
| Type | Holds | Example value | Prefix |
|---|---|---|---|
| Integer | whole numbers | 12, -5, 1250 | iCount |
| Real | numbers with decimals | 12.50, 3.14 | rPrice |
| String | text, in single quotes | 'Thandi' | sName |
| Char | one character | 'B' | cGrade |
| Boolean | True or False | True | bPaid |
var iCount : Integer; sName : string; begin iCount := 12; sName := 'Thandi'; iCount := iCount + 1; // now 13: the old 12 is replaced end;
Choose the best data type for each item and write a variable name with the correct prefix.
| Item to store | Data type | Variable name |
|---|---|---|
| Number of learners in a class | ||
| Price of a loaf: R12.50 | ||
| A learner’s surname | ||
| The class letter, for example ‘C’ | ||
| Has the fee been paid? (yes or no) |
Write the value held by each variable after each line runs. Write ? if the variable has no value yet.
iApples := 5; iOranges := 3; iFruit := iApples + iOranges; iApples := iApples - 2; iFruit := iFruit * 2;
| After line | iApples | iOranges | iFruit |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 |
Start with iA = 8 and iB = 15. Show the values after each line.
iTemp := iA; iA := iB; iB := iTemp;
| After line | iA | iB | iTemp |
|---|---|---|---|
| 1 | |||
| 2 | |||
| 3 |
| Function | Converts | Example |
|---|---|---|
| StrToInt | text → Integer | iAge := StrToInt(edtAge.Text); |
| StrToFloat | text → Real | rPrice := StrToFloat(edtPrice.Text); |
| IntToStr | Integer → text | lblOut.Caption := IntToStr(iTotal); |
| FloatToStr | Real → text | lblOut.Caption := FloatToStr(rAvg); |
var
iFirst, iSecond, iSum : Integer;
sName : string;
begin
sName := InputBox('Name', 'Your name?', '');
iFirst := StrToInt(edtFirst.Text);
iSecond := StrToInt(InputBox('Second', 'Second number:', ''));
iSum := iFirst + iSecond;
lblAnswer.Caption := sName + ', the sum is ' + IntToStr(iSum);
ShowMessage('Done!');
end;Circle the function that completes each statement.
| 1 | iQty := ______(edtQty.Text); | StrToInt IntToStr StrToFloat |
| 2 | lblTotal.Caption := ______(iTotal); | StrToInt IntToStr FloatToStr |
| 3 | rPrice := ______(InputBox('Price', 'Price in rand:', '')); | StrToInt StrToFloat FloatToStr |
| 4 | lblAvg.Caption := ______(rAverage); | IntToStr StrToFloat FloatToStr |
Find four errors in this event handler and write the corrections.
procedure TForm1.btnTotalClick(Sender: TObject);
var
iQty : Integer;
rPrice, rTotal : Real;
begin
iQty := edtQty.Text;
rPrice := 12.50;
rTotal := iQty * rPrice
lblTotal.Caption := rTotal;
ShowMessage('Total: ' + rTotal);
end;Write the code for btnTotalClick. The number of items is typed in edtQty. The price (with cents) is asked with an InputBox. Calculate the total and show it in lblTotal with an R in front and two decimals. Include the variable declarations.
Computers are fast calculators, but they follow strict rules about the order of work and about whole numbers and decimals. Learn the operators, then three useful functions: Random, Round and Sqrt.
| Operator | Meaning | Example | Result |
|---|---|---|---|
| + | add | 12 + 5 | 17 |
| - | subtract | 12 - 5 | 7 |
| * | multiply | 12 * 5 | 60 |
| / | divide (a Real answer) | 17 / 5 | 3.4 |
| DIV | whole-number division | 17 DIV 5 | 3 |
| MOD | remainder | 17 MOD 5 | 2 |
Work out each answer. Show your thinking in the margin if you need to.
| Calculation | DIV | MOD |
|---|---|---|
| 29 ÷ 4 | ||
| 100 ÷ 7 | ||
| 45 ÷ 9 | ||
| 8 ÷ 12 | ||
| 1234 ÷ 10 |
Work out each answer. Remember that / gives a Real.
| Expression | Answer |
|---|---|
| 6 + 4 * 3 | |
| (6 + 4) * 3 | |
| 20 - 12 DIV 4 | |
| 17 MOD 5 + 2 * 3 | |
| 100 DIV (3 + 2) * 4 | |
| 10 / 4 |
Can the answer be stored in an Integer variable, or is a Real variable needed?
| 1 | 17 DIV 5 | Integer Real |
| 2 | 17 / 5 | Integer Real |
| 3 | 17 MOD 5 | Integer Real |
| 4 | 12 * 3 | Integer Real |
| 5 | 20 / 4 (the answer looks whole) | Integer Real |
| 6 | Sqrt(49) | Integer Real |
Randomize; // once, at the start iDie := Random(6) + 1; // 1, 2, 3, 4, 5 or 6 iNear := Round(12.678); // 13 rRoot := Sqrt(49); // 7.0 (a Real) lblSide.Caption := IntToStr(Round(Sqrt(30))); // 5
Complete the table for the code.
iA := 50; iB := 8; iQuot := iA div iB; iRem := iA mod iB; rDiv := iA / iB; iCheck := iQuot * iB + iRem; iRound := Round(rDiv);
| Variable | Value stored |
|---|---|
| iQuot | |
| iRem | |
| rDiv | |
| iCheck | |
| iRound |
The number of cupcakes is typed in edtCakes. Each box holds 12. Show the number of full boxes in lblBoxes and the number left over in lblLeft. Include variable declarations.
Write code that rolls two dice and shows their total in lblTotal.
Find three errors. Each statement should give the result in its comment.
iAverage := iM1 + iM2 + iM3 / 3; // average of three marks (iAverage is an Integer) iDie := Random(6); // a die: 1 to 6
Programs make decisions all the time: pass or fail, free delivery or not, a bonus or no bonus. You will learn to ask a True/False question in code and act on the answer.
if iMark >= 50 then lblResult.Caption := 'Pass'; // one-way IF if iStock = 0 then lblNote.Caption := 'Sold out' // no semicolon before else else lblNote.Caption := 'In stock'; if iMark >= 50 then begin // two statements need begin...end lblResult.Caption := 'Pass'; btnRetry.Visible := False; end else lblResult.Caption := 'Fail';
iAge := StrToInt(edtAge.Text); if iAge >= 18 then lblMsg.Caption := 'May vote' else lblMsg.Caption := 'Too young';
Write each sentence as a Delphi condition.
| In words | Condition |
|---|---|
| The mark is at least 50 | |
| The learner is younger than 14 | |
| The number is even | |
| The name is Zinhle | |
| The price is not R10 |
Trace the code for each age. The standard fare is R20.
rFare := 20; if iAge < 13 then rFare := rFare / 2; if iAge >= 60 then rFare := rFare - 5;
| iAge | iAge < 13 ? | iAge >= 60 ? | rFare at the end |
|---|---|---|---|
| 7 | |||
| 13 | |||
| 60 |
Find four errors and write the corrections.
if iMark => 50 then
lblResult.Caption := 'Pass';
else
lblResult.Caption := 'Fail';
btnRetry.Visible := True;
if sName = Thabo then
ShowMessage('Hi Thabo');Draw the flowchart for this code.
iSpeed := StrToInt(edtSpeed.Text); if iSpeed > 60 then rFine := 10 * (iSpeed - 60) else rFine := 0; lblFine.Caption := FloatToStr(rFine);
The order total (with cents) is typed in edtTotal. Delivery is R0 for orders of R200 or more, otherwise R25. Show the amount to pay in lblPay.
A number is typed in an InputBox. Show Even or Odd in lblKind.
A shop gives a discount when the quantity is 10 or more. Write three test values for the quantity that test the rule well, and say what you expect for each.
Everything inside a computer is stored as 1s and 0s. Here you work with binary, decimal and hexadecimal by hand, see how letters become numbers, and measure how much space data needs.
| 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|
| 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 |
Convert. Write decimal answers as ordinary numbers and binary answers as 8 bits.
| Start with | Convert to | Answer |
|---|---|---|
| 1011 (binary) | decimal | |
| 10010 (binary) | decimal | |
| 00101101 (binary) | decimal | |
| 10000001 (binary) | decimal | |
| 6 | 8-bit binary | |
| 19 | 8-bit binary | |
| 37 | 8-bit binary | |
| 200 | 8-bit binary |
| Binary | 0101 | 1001 | 1010 | 1100 | 1111 |
|---|---|---|---|---|---|
| Hex | 5 | 9 | A | C | F |
Convert.
| Start with | Convert to | Answer |
|---|---|---|
| 10110111 (binary) | hex | |
| 7E (hex) | 8-bit binary | |
| 2F (hex) | decimal | |
| FF (hex) | decimal | |
| 100 | hex | |
| 255 | hex |
| A | B | Z | a | z | 0 | 9 | space |
|---|---|---|---|---|---|---|---|
| 65 | 66 | 90 | 97 | 122 | 48 | 57 | 32 |
Count along from the codes in the table above to find each ASCII code (decimal), then write its 8-bit binary code.
| Character | ASCII code | 8-bit binary |
|---|---|---|
| C | ||
| H | ||
| d | ||
| 7 |
Each number is the ASCII code of a capital letter. What word is hidden in 83 65 70 69?
| Unit | Size | Example |
|---|---|---|
| 1 byte (B) | 8 bits | one letter |
| 1 kilobyte (KB) | 1 024 bytes | half a page of text |
| 1 megabyte (MB) | 1 024 KB | a photo, a song |
| 1 gigabyte (GB) | 1 024 MB | a film, a game |
| 1 terabyte (TB) | 1 024 GB | a laptop hard drive |
Convert. Use 1 024 each time.
| Question | Answer |
|---|---|
| 2 KB = ? bytes | |
| 3 MB = ? KB | |
| 5 GB = ? MB | |
| 4096 KB = ? MB | |
| How many 4 MB songs fit on a 2 GB flash drive? |
Data is raw facts with no meaning on their own. Information is data that has been processed so that it means something.
| 1 | 12, 7, 15, 9, 20 | Data Information |
| 2 | Grade 10A has the highest average: 68% | Data Information |
| 3 | Thabo 54, Aisha 71, Pieter 49 | Data Information |
| 4 | A graph showing which taxi rank is busiest on Fridays | Data Information |
| 5 | 45.00, 12.50, 99.99 from till slips | Data Information |
| 6 | Total sales this week: R2 340 | Data Information |
No notes, no computer. Show all your working. Write Delphi code neatly and use correct indentation.
Complete the table for the code.
iA := 23; iB := 5; iC := iA div iB; iD := iA mod iB; iE := iC * 3 + iD; rF := iA / iB; iA := iA - iE;
| Variable | Value stored |
|---|---|
| iC | |
| iD | |
| iE | |
| rF | |
| iA (at the end) |
Write exactly what is displayed.
iPrice := 80;
iQty := 3;
iTotal := iPrice * iQty;
if iTotal > 200 then
iTotal := iTotal - 20
else
iTotal := iTotal + 15;
ShowMessage('Total: R' + IntToStr(iTotal));(a) What is the value of iNext? (b) What are the smallest and largest values iRoll can have?
rNum := Sqrt(81); iNext := Round(rNum) + 5 mod 3; iRoll := Random(5) + 2;
Write an IPO table (Input, Process, Output) for this problem: the cost of loaves at R15 each plus a R5 delivery fee. Write the Process as a formula.
Trace the algorithm for each test value.
INPUT qty SET price TO qty * 12 IF qty >= 10 THEN SET price TO price - 10 ENDIF OUTPUT price
| qty | qty >= 10 ? | price (output) |
|---|---|---|
| 4 | ||
| 10 | ||
| 15 |
Draw the flowchart for this algorithm.
START
INPUT mark
IF mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
ENDFind three errors in this code and write the corrections.
procedure TForm1.btnCalcClick(Sender: TObject); var iPrice, iQty : Integer; rTotal : Real; begin iPrice := StrToInt(edtPrice.Text); iQty := edtQty.Text; rTotal := iPrice * iQty * 1.15 lblTotal.Caption := rTotal; end;
An InputBox asks for the number of minutes of load shedding (a whole number). Display it in lblTime as hours and minutes, for example 3 h 10 min for 190 minutes. Include declarations.
A game rolls one die. If the number is a 6, display Bonus! in lblMsg. Otherwise display You rolled a followed by the number.
Convert.
| Start with | Convert to | Answer |
|---|---|---|
| 10110 (binary) | decimal | |
| 1101001 (binary) | decimal | |
| 37 | 8-bit binary | |
| 10101111 (binary) | hex | |
| 2C (hex) | decimal |
| Learner | Grade | Letter handed in? | Choir? | Athletics team? | Rule 1 | Rule 2 |
|---|---|---|---|---|---|---|
| Thandi | 10 | Yes | No | Yes | ||
| Aisha | 10 | No | Yes | No | ||
| Pieter | 9 | Yes | No | No |