GoalRecall how sub-routines shorten an algorithm and how a table of records is built, searched and judged.
New words
sub-routine · a named set of steps that does one job and can be called many timesrecord · one row of a table, holding everything about one thingfield · one column of a table, holding one kind of datakeyword · a word or part of a word that we search for
Explanation
A sub-routine is a named set of steps that does one job. We write it once and call it wherever the job is needed. The main algorithm becomes shorter, and a mistake is fixed in only one place. We can judge a program with criteria, such as function and ease of use, cost, training and support, and technical requirements. A database table stores data in records and fields. Each field has a data type, which is text, number or date. A code field makes every record different. A keyword search finds the cells that contain the keyword anywhere inside them. A phrase narrows the search, and we always read the matches to check them.
A
B
C
D
1
Code
Name
Age
Joined
2
B-01
Aziza
11
3 Sep
3
B-02
Timur
12
14 Sep
4
B-03
Dilya
10
2 Oct
5
B-04
Sanjar
12
9 Oct
6
B-05
Madina
11
30 Oct
Worked examples
Main algorithm
Sub-routine: lay one place
Repeat 4 times: lay one place, move to the next seat
1 Put down a plate
Lay one place
2 Put down a fork
Stop
3 Put down a cup
Setting five places at a table is the same job five times. The sub-routine is written once. Done in full, the job takes 5 × 3 = 15 laying steps and 4 moves. With the sub-routine, the three laying steps appear only once.
Search
Cells found
Why
Sep
D2, D3
two members joined in September
12
C3, C5
two members are 12 years old
Oct
D4, D5, D6
three members joined in October
9 Oct
D5
the phrase finds only one date
ad
B6
only Madina has the letters ad
Here are five searches on the club table in the first picture. Ages are in column C and dates are in column D. Each search looks at every cell. The phrase 9 Oct is narrower than the keyword Oct.
Question
Program X
Program Y
Does it do the whole job?
yes
yes
Is it free?
yes
no
Is help available?
no
no
Does it run on our laptop?
yes
yes
The club must choose one of two programs for its table. Count the yes answers. Both programs do the whole job and run on the club’s laptop. Program X is free but has no help, and program Y costs money and has no help either. X has 3 yes answers and Y has 2, so X is the better choice.
Class activity
Paper database (no computer): choose a topic, such as a class reading club. Draw a table with a code field and three more fields. Write the data type of each field. Fill in five records. Swap tables with a partner. The partner searches for a keyword and then a phrase, and writes the cells that match, in the style A3 or C5. Then the partner adds a new record, changes one value and deletes one record. Last, write the steps for ‘add a new record’ as a sub-routine, and say where the main algorithm would call it.
Practice
1
How many fields and how many records does the table have? Give the best data type for each field.
A
B
C
D
1
Code
Fruit
Price
Stock
2
F-1
Apple
6
40
3
F-2
Pear
8
25
4
F-3
Grapes
14
18
5
F-4
Pomegranate
12
30
6
F-5
Apricot
10
22
There are 4 fields and 5 records. Code is text, because it is a label. Fruit is text. Price is a number. Stock is a number.
2
Using the table in question 1, which cells match the keyword ap?
B2 (Apple), B4 (Grapes) and B6 (Apricot) contain the letters ap. Pear and Pomegranate do not.
3
A main algorithm calls ‘wrap one gift’ six times. The sub-routine has 4 steps. How many steps are carried out altogether, and how many lines of the sub-routine are written?
6 × 4 = 24 steps are carried out. The 4 lines of the sub-routine are written only once.
4
Which program should be chosen, and why?
Question
Program P
Program Q
Does it do the whole job?
yes
no
Is it free?
no
yes
Is help available?
yes
yes
Does it run on our laptops?
yes
yes
Program P. Q is free and has help, but it does not do the whole job, and a program that cannot do the job is no use. P has 3 yes answers and it does the whole job.