← All workbooksPrint double-sided, A4, black and white
GRADE 10 · TERM 1

Think, Plan, Code & Count in Binary

A pencil-and-paper workbook: think it through first, learn the Delphi, then prove you can do it.

Name:
Class: Date:

How to use this workbook

  1. Work with a pencil. Mistakes are how we learn: rub out and try again.
  2. Start each unit with Think First. Try it on your own before you read the explanation.
  3. Read Learn It, then do the activities. Trace tables and code are written by hand.
  4. At the end of each unit, tick the “I can” boxes you are sure of. Write down anything you still want to ask.
  5. The checkpoint test is done under test conditions, with no notes.

In this workbook

  1. Plan before you code
  2. Meet Delphi
  3. Boxes with labels
  4. Calculating
  5. Making a choice
  6. Bits and bytes on paper
  7. Term checkpoint test
Matches the Annual Teaching Plan: Term 1 — algorithms and problem solving; the Delphi IDE; variables, data types, input and output; operators, Random, Round and Sqrt; IF and IF…ELSE; number systems, ASCII and storage units.

UNIT 1Plan before you code

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.

By the end of this unit I can: (tick each box when you are sure)
One thing I want to ask my teacher:
THINK FIRST · no computer needed

1.1Tea for a robot

A robot has arrived at your house. It does exactly what you tell it, nothing more, and it has never seen a kettle. Write numbered steps so that it makes one cup of rooibos tea with milk.
Now swap with a partner. They play the robot and follow your steps literally. Circle every step where the robot got stuck or did something silly.
LEARN IT

Algorithms and the problem-solving cycle

An algorithm is a clear, step-by-step list of instructions that solves a problem. A recipe is an algorithm. A good algorithm is clear (each step has only one meaning), in the right order, finite (it ends), correct and efficient (no wasted steps).
Programmers follow a cycle: 1 Understand the problem → 2 Plan (IPO table, flowchart, pseudocode) → 3 Write the code → 4 Test (trace table) → 5 Fix the errors and test again.
LEARN IT

IPO tables

Before you plan the steps, list what the problem needs. An IPO table has three columns: Input (the data you are given), Process (what you do with it, written as a formula or a decision, not just “calculate”) and Output (what you show at the end).
ProblemInputProcessOutput
Total money from a taxipassengers, faretotal = passengers × faretotal

1.2Complete the IPO tables [6]

Fill in the empty cells. Write the Process as a formula.

ProblemInput
Process
Output
Area of a netball court
Cost of loaves at R17 each
Average of three test marks
LEARN IT

Flowcharts and pseudocode

A flowchart draws an algorithm and arrows show the direction. Pseudocode writes it in plain, structured English. Neither is real Delphi. Here is the same algorithm both ways.
STARTINPUT fare, peopleshare = fare / peopleOUTPUT shareEND
  • Oval: start or end
  • Parallelogram: input or output
  • Rectangle: a process (calculation)
  • Diamond: a yes/no decision
Pseudocode
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.

1.3Fill in the blanks [5]

The shape that marks the start and the end of a flowchart is an . A shows input or output, a rectangle shows a (a calculation) and a shows a decision. An table lists the Input, Process and Output.

1.4Draw the flowchart [6]

A learner buys some pies. Draw the flowchart for this pseudocode. Use the correct shapes and arrows.

Pseudocode
START
  INPUT price, number, paid
  SET total TO price * number
  SET change TO paid - total
  OUTPUT change
END

1.5Write the pseudocode [4]

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.

1.6Trace the algorithm [6]

A shop gives free delivery on orders of R200 or more. Otherwise delivery costs R25. Complete the trace table for each test value.

Pseudocode
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 ?feepay (output)
150
200
320
199

1.7Compare the algorithms [5]

All four algorithms try to find the average of three marks. Read them, then answer.

Algorithm P
INPUT m1, m2, m3
SET average TO total / 3
SET total TO m1 + m2 + m3
OUTPUT average
Algorithm Q
INPUT m1, m2, m3
SET total TO m1 + m2 + m3
SET average TO total / 3
OUTPUT average
Algorithm R
INPUT m1, m2, m3
SET total TO m1 + m2 + m3
SET total2 TO m1 + m2 + m3
SET average TO total / 3
OUTPUT average
Algorithm S
Get the marks
Add them up and do the sums
Show the answer

1The steps are in the wrong order.P  Q  R  S
2A step is too vague for a computer to follow.P  Q  R  S
3Correct, with no wasted steps.P  Q  R  S
4Repeats a calculation it has already done.P  Q  R  S
5Which two give the right answer?P+Q  Q+R  R+S

UNIT 2Meet Delphi

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.

By the end of this unit I can: (tick each box when you are sure)
One thing I want to ask my teacher:
THINK FIRST · no computer needed

2.1The vending machine

A vending machine sits there doing nothing until something happens. Describe two things a customer can do to it, and what the machine does in response. Then answer: what would the machine do if nobody touched it?
LEARN IT

The Delphi IDE

The IDE (Integrated Development Environment) is your workshop: an editor, a compiler and a debugger in one program.
File Edit Search View Project Run Tools HelpMENU BARSTRUCTUREForm1 lblTotal edtQty btnAdd memOutOBJECT INSPECTORCaption AddName btnAddEnabled TrueFont ...Properties | EventsFORM DESIGNERForm1lblTotaledtQtybtnAddmemOutTOOL PALETTETButtonTLabelTEditTMemoTPanelTSpinEditTComboBox

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).

2.2Label the IDE [5]

Write the name of each part. The circled letters are on the picture.
File Edit Search View Project Run Tools HelpABForm1 lblTotal edtQty btnAdd memOutCCaption AddName btnAddEnabled TrueFont ...Properties | EventsDForm1lblTotaledtQtybtnAddmemOutETButtonTLabelTEditTMemoTPanelTSpinEditTComboBox

LetterName of the part
A
B
C
D
E

2.3Which part of the IDE? [4]

Circle the part you would use.

1Drag a new button onto the form.Tool Palette  Object Inspector  Menu bar
2Change the Caption of a label.Tool Palette  Object Inspector  Menu bar
3See a list of every component on the form.Structure panel  Menu bar  Tool Palette
4Save All, or run the program.Object Inspector  Menu bar  Form Designer
LEARN IT

Classes, objects, components and events

A class is the plan, for example TButton. An object is a real thing built from the plan, for example btnPay. A component is an object you place on a form. Every component has properties (how it looks, such as Caption), methods (what it can do, such as Hide) and events (what can happen to it, such as OnClick).
Name components with a prefix that shows the type: btn button, lbl label, edt edit box, mem memo, sed spin edit, cmb combo box. A label shows its text in Caption; an edit box keeps its text in Text. Delphi code runs only inside an event handler, in this format:
Delphi
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;

2.4Prefixes and names [8]

Write the prefix, then a good name (prefix + meaning) for the component described.

ComponentPrefixA good name for…
TButton
TLabel
TEdit
TMemo

2.5Debug: component statements [5]

Each line has one error. Write the correct line.

Delphi
lblResult.Caption = 'Total';
'Welcome' := lblMsg.Caption;
btnOk.Caption := Click me;
edtName.Text := 'Thabo'
lblMsg.Font.Colour := clBlue;

2.6Write the event handler [4]

When btnReset is clicked it must: set lblTotal to R0.00; clear edtQty; change its own caption to Cleared; and hide memReceipt.

Delphi
procedure TForm1.btnResetClick(Sender: TObject);

2.7Plan with a TOE chart [5]

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.

TaskObjectEvent
Work out the total
Clear the screen for the next customer
Show the receipt lines

UNIT 3Boxes with labels

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.

By the end of this unit I can: (tick each box when you are sure)
One thing I want to ask my teacher:
THINK FIRST · no computer needed

3.1Swap the glasses

Glass A is full of orange juice and glass B is full of water. You must swap them so that A has water and B has juice. You have one extra empty glass and you may only pour from one glass into another. Write the steps.
LEARN IT

Variables and data types

A variable is a named place in memory that holds one value that can change. Its data type decides what may go in. Declare it in a var section: name : type; Names start with a letter, have no spaces, and begin with a prefix for the type. Put a value in with the assignment operator := (the value on the right goes into the box on the left).
TypeHoldsExample valuePrefix
Integerwhole numbers12, -5, 1250iCount
Realnumbers with decimals12.50, 3.14rPrice
Stringtext, in single quotes'Thandi'sName
Charone character'B'cGrade
BooleanTrue or FalseTruebPaid
Delphi
var
  iCount : Integer;
  sName : string;
begin
  iCount := 12;
  sName := 'Thandi';
  iCount := iCount + 1;   // now 13: the old 12 is replaced
end;

3.2Choose the type [10]

Choose the best data type for each item and write a variable name with the correct prefix.

Item to storeData typeVariable 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)

3.3Be the computer: assignment [5]

Write the value held by each variable after each line runs. Write ? if the variable has no value yet.

Delphi
iApples := 5;
iOranges := 3;
iFruit := iApples + iOranges;
iApples := iApples - 2;
iFruit := iFruit * 2;
After lineiApplesiOrangesiFruit
1
2
3
4
5

3.4Be the computer: swap [3]

Start with iA = 8 and iB = 15. Show the values after each line.

Delphi
iTemp := iA;
iA := iB;
iB := iTemp;
After lineiAiBiTemp
1
2
3
LEARN IT

Input and output in Delphi

An edit box keeps what the user typed as text, and so does InputBox (a pop-up that asks a question). You must convert text to a number before you calculate, and a number back to text before you display it. ShowMessage pops up a message, Caption sets a label, memOut.Lines.Add adds a line to a memo, and + joins text together.
FunctionConvertsExample
StrToInttext → IntegeriAge := StrToInt(edtAge.Text);
StrToFloattext → RealrPrice := StrToFloat(edtPrice.Text);
IntToStrInteger → textlblOut.Caption := IntToStr(iTotal);
FloatToStrReal → textlblOut.Caption := FloatToStr(rAvg);
For money, FloatToStrF(value, ffFixed, 8, 2) shows exactly two decimals.
Delphi
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;

3.5Pick the conversion [4]

Circle the function that completes each statement.

1iQty := ______(edtQty.Text);StrToInt  IntToStr  StrToFloat
2lblTotal.Caption := ______(iTotal);StrToInt  IntToStr  FloatToStr
3rPrice := ______(InputBox('Price', 'Price in rand:', ''));StrToInt  StrToFloat  FloatToStr
4lblAvg.Caption := ______(rAverage);IntToStr  StrToFloat  FloatToStr

3.6Debug: the till button [4]

Find four errors in this event handler and write the corrections.

Delphi
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;

3.7Write it: the till [8]

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.

UNIT 4Calculating

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.

By the end of this unit I can: (tick each box when you are sure)
One thing I want to ask my teacher:
THINK FIRST · no computer needed

4.1Boxes and leftovers

(a) The tuck shop has 95 samoosas to pack in boxes of 8. How many boxes are completely full? How many samoosas are left over? How many boxes are needed if every samoosa must be packed?
(b) A day of load shedding adds up to 245 minutes. How many hours and minutes is that?
LEARN IT

The operators

Delphi has six arithmetic operators. / always gives a Real, even when the answer looks whole. DIV gives the whole-number part of a division and MOD gives the remainder. Use n MOD 2 = 0 to test for an even number and n MOD 10 to get the last digit. Inc(x) adds 1 to x and Dec(x) subtracts 1.
OperatorMeaningExampleResult
+add12 + 517
-subtract12 - 57
*multiply12 * 560
/divide (a Real answer)17 / 53.4
DIVwhole-number division17 DIV 53
MODremainder17 MOD 52
LEARN IT

Order of operations

Delphi works in this order: 1 brackets ( ), 2 then * / DIV MOD, left to right, 3 then + and -, left to right. Use brackets to make your meaning clear. Example: 7 + 15 MOD 4 * 2. MOD and * come first, from the left: 15 MOD 4 = 3, then 3 * 2 = 6, then 7 + 6 = 13. To average three marks you must write (iMark1 + iMark2 + iMark3) / 3: without the brackets only iMark3 would be divided.

4.2DIV and MOD [10]

Work out each answer. Show your thinking in the margin if you need to.

CalculationDIVMOD
29 ÷ 4
100 ÷ 7
45 ÷ 9
8 ÷ 12
1234 ÷ 10

4.3Order of operations [6]

Work out each answer. Remember that / gives a Real.

ExpressionAnswer
6 + 4 * 3
(6 + 4) * 3
20 - 12 DIV 4
17 MOD 5 + 2 * 3
100 DIV (3 + 2) * 4
10 / 4

4.4Integer or Real? [6]

Can the answer be stored in an Integer variable, or is a Real variable needed?

117 DIV 5Integer  Real
217 / 5Integer  Real
317 MOD 5Integer  Real
412 * 3Integer  Real
520 / 4 (the answer looks whole)Integer  Real
6Sqrt(49)Integer  Real
LEARN IT

Random, Round and Sqrt

Random(n) gives a whole number from 0 to n-1, so add to shift it: Random(6) + 1 is a die (1 to 6). Call Randomize once when the program starts. Round(x) gives the nearest whole number. A value exactly halfway goes to the even neighbour, so Round(2.5) is 2 but Round(3.5) is 4. Sqrt(x) gives the square root as a Real. It cannot take a negative number.
Delphi
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

4.5Fill in the blanks [10]

Round(6.3) = .   Round(6.7) = .   Round(2.5) = .   Round(3.5) = .   Sqrt(64) = .   Round(Sqrt(30)) = .
To get a random number from 5 to 15 write Random() + .   Random(20) + 10 gives a number from to .

4.6Be the computer: DIV, MOD and Round [5]

Complete the table for the code.

Delphi
iA := 50;
iB := 8;
iQuot := iA div iB;
iRem := iA mod iB;
rDiv := iA / iB;
iCheck := iQuot * iB + iRem;
iRound := Round(rDiv);
VariableValue stored
iQuot
iRem
rDiv
iCheck
iRound

4.7Write it: packing [6]

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.

4.8Write it: two dice [4]

Write code that rolls two dice and shows their total in lblTotal.

4.9Debug: three errors [3]

Find three errors. Each statement should give the result in its comment.

Delphi
iAverage := iM1 + iM2 + iM3 / 3;   // average of three marks (iAverage is an Integer)
iDie := Random(6);                 // a die: 1 to 6

UNIT 5Making a choice

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.

By the end of this unit I can: (tick each box when you are sure)
One thing I want to ask my teacher:
THINK FIRST · no computer needed

5.1Rules we live by

Write each rule in the form IF … THEN … ELSE …
(a) A taxi driver charges children half price. (b) Your school does not open when there is a strike. (c) You study for the test only if you did badly in the last one.
LEARN IT

Conditions and IF

A condition is a question with the answer True or False. Compare with: = equal, <> not equal, >, <, >= and <=. IF…THEN runs a statement only when the condition is True. IF…THEN…ELSE chooses one of two paths. Three rules: put no semicolon before else; use begin…end when a branch has more than one statement; indent the branches.
Delphi
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';
LEARN IT

From flowchart to code

A decision diamond becomes an IF. The Yes path is the THEN part and the No path is the ELSE part.
STARTINPUT iAgeiAge >= 18 ?OUTPUT"May vote"OUTPUT"Too young"YesNoEND
Delphi
iAge := StrToInt(edtAge.Text);
if iAge >= 18 then
  lblMsg.Caption := 'May vote'
else
  lblMsg.Caption := 'Too young';

5.2Write the condition [5]

Write each sentence as a Delphi condition.

In wordsCondition
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

5.3Trace the code [6]

Trace the code for each age. The standard fare is R20.

Delphi
rFare := 20;
if iAge < 13 then
  rFare := rFare / 2;
if iAge >= 60 then
  rFare := rFare - 5;

iAgeiAge < 13 ?iAge >= 60 ?rFare at the end
7
13
60

5.4Debug: the IFs [4]

Find four errors and write the corrections.

Delphi
if iMark => 50 then
  lblResult.Caption := 'Pass';
else
  lblResult.Caption := 'Fail';
  btnRetry.Visible := True;
if sName = Thabo then
  ShowMessage('Hi Thabo');

5.5Code to flowchart [6]

Draw the flowchart for this code.

Delphi
iSpeed := StrToInt(edtSpeed.Text);
if iSpeed > 60 then
  rFine := 10 * (iSpeed - 60)
else
  rFine := 0;
lblFine.Caption := FloatToStr(rFine);

5.6Write it: delivery [6]

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.

5.7Write it: even or odd [4]

A number is typed in an InputBox. Show Even or Odd in lblKind.

5.8Choose the test values [4]

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.

UNIT 6Bits and bytes on paper

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.

By the end of this unit I can: (tick each box when you are sure)
One thing I want to ask my teacher:
THINK FIRST · no computer needed

6.1Light switches

A computer is built from billions of tiny switches, each ON or OFF. Think of a row of light switches. How many different patterns can you make?
1 switch gives patterns.   2 switches give .   3 switches give .   4 switches give .   8 switches give .
Spot the rule and explain it in one sentence.
LEARN IT

Binary place values

Decimal uses 10 digits and every column is worth 10 times the one on its right. Binary (base 2) uses only 0 and 1 and every column is worth 2 times the one on its right. A bit is one binary digit; a byte is 8 bits.
1286432168421
00101101
Binary to decimal: add the place values where there is a 1. 00101101 = 32 + 8 + 4 + 1 = 45.
Decimal to binary: go from the biggest place value down. Does it fit? Write 1 and subtract; if not, write 0. For 77: 64 fits (13 left); 32 no; 16 no; 8 fits (5 left); 4 fits (1 left); 2 no; 1 fits. So 77 = 01001101.

6.2Binary and decimal [8]

Convert. Write decimal answers as ordinary numbers and binary answers as 8 bits.

Start withConvert toAnswer
1011 (binary)decimal
10010 (binary)decimal
00101101 (binary)decimal
10000001 (binary)decimal
68-bit binary
198-bit binary
378-bit binary
2008-bit binary
LEARN IT

Hexadecimal

Long binary numbers are hard to read. Hexadecimal (base 16) has 16 digits: 0 to 9 and then A=10, B=11, C=12, D=13, E=14, F=15. One hex digit is exactly 4 bits, so you can convert in groups of four bits. Hex is used for colour codes (#FF0000 is red) and memory addresses.
Binary01011001101011001111
Hex59ACF
Binary to hex: split into groups of 4: 1011 0111 → B and 7 → B7.
Hex to decimal: each column is worth 16 times the one on its right: 2A = 2 × 16 + 10 = 42.
Decimal to hex: divide by 16: 200 ÷ 16 = 12 remainder 8, so 200 = C8.

6.3Hexadecimal conversions [6]

Convert.

Start withConvert toAnswer
10110111 (binary)hex
7E (hex)8-bit binary
2F (hex)decimal
FF (hex)decimal
100hex
255hex
LEARN IT

Text as numbers: ASCII and Unicode

A computer stores a letter as a number. ASCII gives each character a code that is stored as one byte: space = 32, digits 0 to 9 = 48 to 57, capital letters A to Z = 65 to 90, small letters a to z = 97 to 122. A small letter is always 32 more than its capital. ASCII covers only English. Unicode (for example UTF-8) covers more than 140 000 characters, including isiZulu and isiXhosa letters, Arabic, Chinese and emoji.
ABZaz09space
65669097122485732

6.4ASCII [8]

Count along from the codes in the table above to find each ASCII code (decimal), then write its 8-bit binary code.

CharacterASCII code8-bit binary
C
H
d
7

6.5Decode the message [2]

Each number is the ASCII code of a capital letter. What word is hidden in 83 65 70 69?

LEARN IT

Measuring storage

A bit is one 0 or 1. A byte is 8 bits and stores one ASCII character. Computers count in powers of two, so each unit below is 1 024 times the one before.
UnitSizeExample
1 byte (B)8 bitsone letter
1 kilobyte (KB)1 024 byteshalf a page of text
1 megabyte (MB)1 024 KBa photo, a song
1 gigabyte (GB)1 024 MBa film, a game
1 terabyte (TB)1 024 GBa laptop hard drive
To go to a smaller unit multiply by 1 024; to go to a bigger unit divide by 1 024.

6.6Storage conversions [5]

Convert. Use 1 024 each time.

QuestionAnswer
2 KB = ? bytes
3 MB = ? KB
5 GB = ? MB
4096 KB = ? MB
How many 4 MB songs fit on a 2 GB flash drive?

6.7Data or information? [6]

Data is raw facts with no meaning on their own. Information is data that has been processed so that it means something.

112, 7, 15, 9, 20Data  Information
2Grade 10A has the highest average: 68%Data  Information
3Thabo 54, Aisha 71, Pieter 49Data  Information
4A graph showing which taxi rank is busiest on FridaysData  Information
545.00, 12.50, 99.99 from till slipsData  Information
6Total sales this week: R2 340Data  Information

TERM CHECKPOINTAlgorithms, Delphi basics and number systems

Time: 60 minutes
Total: 49 marks
Mark: ______ / 49

No notes, no computer. Show all your working. Write Delphi code neatly and use correct indentation.

SECTION A — Trace and predict [12]

A1 [5]

Complete the table for the code.

Delphi
iA := 23;
iB := 5;
iC := iA div iB;
iD := iA mod iB;
iE := iC * 3 + iD;
rF := iA / iB;
iA := iA - iE;
VariableValue stored
iC
iD
iE
rF
iA (at the end)

A2 [3]

Write exactly what is displayed.

Delphi
iPrice := 80;
iQty := 3;
iTotal := iPrice * iQty;
if iTotal > 200 then
  iTotal := iTotal - 20
else
  iTotal := iTotal + 15;
ShowMessage('Total: R' + IntToStr(iTotal));

A3 [4]

(a) What is the value of iNext? (b) What are the smallest and largest values iRoll can have?

Delphi
rNum := Sqrt(81);
iNext := Round(rNum) + 5 mod 3;
iRoll := Random(5) + 2;

SECTION B — Algorithms and flowcharts [13]

B1 [3]

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.

B2 [5]

Trace the algorithm for each test value.

Pseudocode
INPUT qty
SET price TO qty * 12
IF qty >= 10 THEN
  SET price TO price - 10
ENDIF
OUTPUT price

qtyqty >= 10 ?price (output)
4
10
15

B3 [5]

Draw the flowchart for this algorithm.

Pseudocode
START
  INPUT mark
  IF mark >= 50 THEN
    OUTPUT "Pass"
  ELSE
    OUTPUT "Fail"
  ENDIF
END

SECTION C — Write the Delphi code [15]

C1 [3]

Find three errors in this code and write the corrections.

Delphi
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;

C2 [6]

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.

C3 [6]

A game rolls one die. If the number is a 6, display Bonus! in lblMsg. Otherwise display You rolled a followed by the number.

SECTION D — Number systems and data [9]

D1 [5]

Convert.

Start withConvert toAnswer
10110 (binary)decimal
1101001 (binary)decimal
378-bit binary
10101111 (binary)hex
2C (hex)decimal

D2 [4]

A byte is made of bits.   3 KB = bytes.   If A is 65 in ASCII, then C is .   4 GB = MB.
COMING UP NEXT TERM · warm-up

Two rules at once

Next term your programs will ask two questions at the same time. Try it first with ordinary school rules.

Rule 1: A learner may enter the 400 m race if she is in Grade 10 AND she has handed in her permission letter.
Rule 2: A learner gets free entry to the athletics evening if she is in the choir OR in the athletics team.
LearnerGradeLetter handed in?Choir?Athletics team?Rule 1Rule 2
Thandi10YesNoYes
Aisha10NoYesNo
Pieter9YesNoNo
In your own words: when is a rule with AND True, and when is a rule with OR True? Which one is harder to satisfy?