Data Representation
Data Representation
Theme: 4
4.1. Data representation
Convert binary to denary and vice versa using positive values up to 16 bits.
i. 2796
_______________________________________________________________
ii. 45005
______________________________________________________________
iii. 9336
_______________________________________________________________
Convert the following hexadecimal numbers to denary
i. FFC2
____________________________________________________________
ii. AE
______________________________________________________________
Explain a byte and how the byte and its function in the measurement of memory
The smallest measurement of memory is called a bit. Bit stands for binary digit and a bit
can only be either a 1or 0. Each character in the computer is represented by 8 bits which is
equivalent to 1 byte.
Explain what a register is and how binary is used for a variety of applications, e.g.
storage, robotics, digital instruments, control systems and programming
Activity
Define the term register.
________________________________________________________________________
Convert positive denary values to hexadecimal and vice versa up to 4 hexadecimal
digits (integers only)
Activity
a. 5 628
b. 269
c. 2 095
COMPUTER STUDIES GRADE 10 – 11Page 140 of 256
Activity
d. 75
e. 258
f. 1 056
rather than:
COMPUTER STUDIES GRADE 10 – 11Page 141 of 256
Convert binary to hexadecimal and vice versa using positive integers up to 16 bits
of binary.
To convert from binary to hexadecimal, you need to first of all convert from binary to
denary then from denary to hexadecimal.
In the binary number system, the weight of each digit increases by a factor of 2 from right
to left as shown in the figure below. The first digit has a weight of 1 ( 20 ), the second digit
has a weight of 2 ( 21 ), the third a weight of 4 ( 22 ), the fourth a weight of 8 ( 23 ) and so
on.
Add together ALL the denary number values from right to left at the positions that are
represented by a “1” gives us: (256) + (64) + (32) + (4) + (1) = 357 or three hundred and
fifty seven as a denary number.
COMPUTER STUDIES GRADE 10 – 11Page 142 of 256
After converting from binary to denary, you then follow the procedure of converting from
denary to hexadecimal explained earlier.
Activity
a. 101011100
b. 011011101
c. 111000111
After converting from binary to denary, you then follow the procedure of converting from
denary to hexadecimal explained earlier.
To convert a hexadecimal to a denary, you must start by multiplying the hex number by
16 raised to a power of 0 and increase that power by 1 each time starting from the right of
the hexadecimal number and go to the left.
99 = 9 ∗ 160
C = 12 ∗ 161 = 192
After converting to denary, follow the procedure to convert the denary number to binary.
Activity
a. AAA
b. ABC
c. EFA
Explain how hexadecimal numbers are used in defining colours in HTML (Hypertext
Markup Language)
HTML code is often used to represent colours of text on the computer screen. The
values change to represent different colours. The different intensity of the three
primary colours (red, green and blue) is determined by its hexadecimal value.
For example:
• # FF 00 FF represents fuchsia
• # FF 80 00 represents orange
• # B1 89 04 represents tan
and so on producing almost any colour the user wants. There are many websites available
that allow a user to find the HTML code for the colour needed.
COMPUTER STUDIES GRADE 10 – 11Page 144 of 256
Explain how hexadecimal numbers are used in Media Access Control (MAC)
addresses
A MAC address is usually made up of 48 bits which are shown as six groups of
hexadecimal digits
NN – NN – NN – DD – DD – DD or NN:NN:NN:DD:DD:DD
where the first half (NN – NN – NN) is the identity number of the manufacturer of the
device and the second half (DD – DD – DD) is the serial number of the device.
Explain how hexadecimal numbers are used in - assembly languages and machine
code
Machine code and assembly code are examples of low-level programming languages and
are used by software developers when producing, software such as computer games.
Using hexadecimal makes it much easier, faster and less error prone to write code
compared to binary. Using true machine code (which uses binary) is very cumbersome and
it takes a long time to type the values. It is also very easy to mistype the digits when there
are a lot of 1s and 0s.
COMPUTER STUDIES GRADE 10 – 11Page 145 of 256
For example:
1010 0101 1110 0100 1111 1111 1010 0100 (machine code using binary)
Hexadecimal is often used when tracing errors in programs. A programmer can look at
each of the hexadecimal codes and determine where the error lies. This is much more
manageable using hexadecimal rather than using binary.
It’s a very powerful error-tracing tool, but requires good knowledge of computer
architecture in order to understand the results.
Explain how data flows through computers, e.g. a storage device; data to a printer;
listen to music from the Internet
Memory is where instructions and data are stored on a computer system during the
running of programs. Memory consists of a number of cells, and each cell can hold a single
instruction. Each memory cell is identified by a number which is called the address. A
program cannot run before it is loaded in memory.
Buffering
A buffer is a temporary storage location which stores data that has already been
processed by the CPU but is waiting to be used by peripheral devices. Buffers are found
within the slow device e.g. a printer buffer. Buffering allows the CPU to be freed so that it
can deal with other tasks.
Spooling
Printers are slow and it will be waste of time for the CPU to wait for the slow printer before
it can continue with other tasks. Spooling is used to avoid the monopolization of the CPU
by slow devices such as printers.
Cache memory
Cache memory is memory that is integrated directly within the CPU chip and a processor
can access it faster than it can access normal RAM. The use of cache memory is to store
COMPUTER STUDIES GRADE 10 – 11Page 147 of 256
program instructions that are frequently used and this allows fast access to these
instructions and thereby increasing the overall speed of running the program.
1) Stop the execution of the current instruction and save it in the system stack.
2) Fetch interrupt instruction from interrupt register.
3) Decode it and execute it.
4) Continue with the execution of the suspended instruction, which is the system
Stack.
6. Execute the instruction in the current Instruction Register. To execute is also to carry
out/ perform / obey)
7. Store results of the processing in the accumulator then later they will be transferred to
main memory.
COMPUTER STUDIES GRADE 10 – 11Page 148 of 256
Identify the following registers and explain their roles in the fetch-execute cycle
This is the unit that carries out all the arithmetic calculations and logic operations.
= equal to, <> not equal to, <= less than or equal to, >= greater than or equal to, < less
than, >greater than
The CU controls the activities of all the other parts of the CPU, to make sure that an
instruction is executed in the correct manner. It fetches instructions and decodes them.
The MDR uses the address in the MAR to copy instructions and data from memory.
The IR is used to hold a copy of the instruction which the CPU is executing at that
particular time. That means all instructions are executed in the IR.
The MDR acts as a buffer to hold instructions and data as they are waiting to be executed
by the CPU.
Accumulator
The ACU is used to temporarily store the results of the execution before they are
transferred to the main memory.
The PC contains the address of the next instruction to be fetched from memory.
After the next instruction is fetched from memory, the value of the PC must be incremented
by 1.
Activity
1. Explain the purpose of each of the following special registers in a processor.
i. Program Counter (Sequence Control Register). [2]
___________________________________________________________________
___________________________________________________________________
ii. Current Instruction Register. [2]
___________________________________________________________________
___________________________________________________________________
iii. Memory Address Register. [2]
___________________________________________________________________
___________________________________________________________________
iv. Memory Data Register. [2]
___________________________________________________________________
___________________________________________________________________
2. A processor is to carry out an instruction. Describe the steps of the fetch-decode-
COMPUTER STUDIES GRADE 10 – 11Page 150 of 256
execute-reset cycle, stating the effect on the registers in the processor, when carrying out
this instruction. [6]
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
3. The Central Processing Unit comprises three main parts. They are registers and two
other parts.
a. Name these two parts and briefly outline their function. [4]
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
d. Describe how the processor handles an interrupt during the fetch-decode-
execute-reset cycle. [4]
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
Identify a variety of formats for the storage of audio, video, graphics and alpha-
numeric data
There are various formants used to store various data such as sound (music), pictures,
video, text and numbers.
MIDI data contains a list of events or messages that tell an electronic device (musical
instrument, computer sound card, cell phone, et cetera) how to generate a certain sound.
Here are a few examples of typical MIDI messages:
a MID file can explain what notes are played, when they're played, and how long or loud
each note should be.
JPEG
This is a storage format for storing compressed images. JPEG images are greatly reduced
in size but maintain the quality of the images. Compressing is done to allow the easy
sharing of the files over the internet and other platforms.
COMPUTER STUDIES GRADE 10 – 11Page 152 of 256
Bitmap
This is a format for storing digital image composed of a grid of pixels and each pixels
contains color information
This is a format for storing images that are compressed to reduce transfer time. It supports
animation of the images.
These are storage formats for audio and videos. MP3s are compressed audio files.
Portable Document Format (PDF; files with an extension pdf. Text and formatting are both
stored in reasonably complex encoding. Although the details of the encoding are freely
available, the formats are owned by Adobe and can be changed by them at any-time and
for any reason. The files can be viewed and printed on all major platforms through free
software usually provided by Adobe. PDF files are not readily editable.
This is a format for storing audio files. It has some compression limitations and therefore
files are generally large.
This the format for storing raw and uncompressed audio files. The files are usually large
and difficult to share.
COMPUTER STUDIES GRADE 10 – 11Page 153 of 256
This is a format for storing audio files and video files. WMA can compress audio files
without loss of quality.
Plain text; files with the extension txt. These files are portable, which means they can be
used on any computer. There are no formats specified like font size, colour, or italic and no
paragraph or page formats are also specified. The characters are stored in bytes using
ASCLL encoding and stored in sequence.
Rich Text Format (RTF); is a text alignment options. However, it does not offer all the
features such as bold, italic, underline, bullets, various fonts, and text alignment options.
However, it does not offer all the features found in a full word processor. An example of
RTF editor is Microsoft Windows WordPad.
HTML
Hyper Text Mark-up Language (HTML); files with the extension html. Text and simple
formats are stored in simple encoding. It is freely available but controlled by public interest
standards body. Any web Brower is used to view the file, while it can be edited using a text
editor, using HTML.
COMPUTER STUDIES GRADE 10 – 11Page 154 of 256
ZIP
ZIP is an archive file format that support lossless data compression. A zip file may contain
one or more files or directions that may have been compressed. File are zipped mostly
when they are to be e-mailed or sent over the internet.
Discuss the verification and correction of data integrity after data transfer processes
- Parity checks
- check sums
- check digits
Data on a storage device can be changed due to viruses or physical damage of the device.
In that case, error occurs. An error is a condition where the output information does not
match the input information. Therefore, whenever we store data on disk or tape, we need
assurance when we retrieve it in future, to find if it will be the same or unaltered. Accurate
data is absolutely essential for computations, record keeping, transaction processing and
online commerce. There are various methods to both detect and correct errors.
Parity checking
Parity checking is an error detection method that uses an extra bit as a check digit called
parity bit. Parity bit is a bit added to a group of source data bits.
Even parity
With even parity, the number of 1s must be even. This means if the source data contains
an odd number of 1s, then the parity bit must be a 1, So that the number of 1s becomes
even. If the source data contains an even number of 1s, then the parity bit must be a 0, so
COMPUTER STUDIES GRADE 10 – 11Page 155 of 256
that the number of 1s is even. If the source data is retrieved with an odd number of 1s, it
means that an error occurred.
Odd parity
With odd parity, the number of 1s must be odd. If the source data contains an odd number
of 1s, then the parity bit must be a 0 to maintain an odd number of 1s. If the source data
contains an even number of 1s, then the parity bit must be a 1 to make an odd number of
1s. If the source data is retrieved with even number of 1s, then it implies that an error
occurred.
Mark parity,
Space parity
The sender and receiver must agree on which parity type they are using. However, this
may not be guaranteed that the received data is correct because if the two agreed to use
odd parity for example, and the sender sends 0101011 and receiver receives 10100010
which is still odd parity but looking at it, it is not correct. In that case, the transmission will
be regarded successful, yet it is not. Such errors are detected by other error detection
methods, for example check digits.
COMPUTER STUDIES GRADE 10 – 11Page 156 of 256
Check digits
Check digits are numbers appended to an identification number that allow the accuracy of
information stored to be checked by an algorithm. It is the final digit in a code, calculated
using other digits of the code. Check digits are used for International Standard Book
Numbers (ISBN), Driver’s License Numbers, University Product Codes (UPC), Bank
Account Numbers, Personal Checks and Social Security Numbers, for example. A check
digit is used to detect errors such as mistyped digits in data entry of a series of digits.
Types of common errors that are addressed by check digits algorithms include:
Check digit algorithms are generally intended to capture human transcription errors.
position 1 2 3 4 5 6 7 8 9 10 11 12 13
ISBN 9 7 8 9 9 9 4 5 7 3 8 5 9
Check digits can be calculated using different calculation methods, mostly a method used
is not an issue as long as the system has correct instructions on what to do.
Checksums
If it is assumed that the checksum of a block of data is 1 byte. Then its maximum value is
255, thus 111111111. If the sum of the data block bytes is 255 or less, then that sum is the
checksum. If the sum is greater than 255, then the calculation is as follows:
1. Divide the sum of the bytes of the block of data (v) by 256 = (x)
2. Round down x to the nearest integer to get (y)
3. Multiply y by 256 to get z
4. Checksum = v –z
2446
For example, say v = 2446, then 𝑥 = = 9.55
256
Y = int (9.55) = 9
Z = 9*256 = 2304
We also have methods of correcting errors such as Automatic Repeat requests (ARQ).
COMPUTER STUDIES GRADE 10 – 11Page 158 of 256
Timeout is a predetermined time interval after the sending device sends the packet; if the
sending device does not receive an acknowledgement before the timeout, it will re-transmit
the packet after the timeout. The re-transmission is repeated until the sending device
receives an acknowledgement or exceeds a predetermined number of re-transmissions.
COMPUTER STUDIES GRADE 10 – 11Page 159 of 256
Activity
Identify and describe 2 error checking methods that can be used for detecting and
correcting errors during data transmission.
Method 1 _____________________________________________________________________[1]
Description______________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_____________________________________________________________________________[3]
Method 2 _____________________________________________________________________[1]
Description______________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_______________________________________________________________________________
_____________________________________________________________________________[3]
Explain how data in different formats (audio, video, graphics and text) can be compressed
using lossy or lossless compression
The smaller our files are, the more files we can store. We use compression algorithms to
reduce the amount of space needed to represent a file. Compression can be lossy or
lossless.
Lossy Compression
Lossy compression is a compression technique that reduces file size by discarding the
less important information. The data that have been compressed using lossy techniques
cannot be recovered or reconstructed to its exact original format but in most cases this is
not a problem. Lossy file compression results in lost data and quality from the original
version. Lossy compression is usually image, audio and video files.
COMPUTER STUDIES GRADE 10 – 11Page 160 of 256
Lossless Compression
Lossless compression is a compression technique in which every single bit of data that
was originally in the file remains after the file is uncompressed. All of the information is
completely restored. This is generally the technique of choice for text files, where losing
words could pose a problem.
Activity
Files can be compressed using lossy or lossless compression techniques. For each of the
following file types, identify which compression technique would be appropriate.
a. Text files
_________________________________________________________________
b. Audio files
________________________________________________________________
c. Video files
________________________________________________________________
COMPUTER STUDIES GRADE 10 – 11Page 161 of 256
Recognize the symbols and describe the use of AND, OR, NOT, NAND and NOR and
XOR gates
AND OR
NOR
NAND
NOT
XOR
COMPUTER STUDIES GRADE 10 – 11Page 162 of 256
It has two inputs (A and B) and the output is True only if A and B are both True. The
AND gate can be represented by the following two symbols.
AND Gate
Output
A B (A.B)
0 0
1 0
0 1
The OR Gate 1 1
It has two inputs (A and B) and the output is True if either A or B is True, which means
any one of A or B must be true for the output to be true.
OR Gate
A B Output (A+B)
0 0
1 0
0 1
1 1
COMPUTER STUDIES GRADE 10 – 11Page 163 of 256
This gate has only one input and it inverts the input value, meaning that it gives the
opposite of the input value as the output. The output is True if input is False and the
output is False if input is True
NOT Gate
A Output (A)
0
1
This means NOT-AND which is equal to an AND gate followed by a NOT gate. The
output is False only if A and B are both True.
The symbol is an AND gate with a small circle on the output. The small circle
represents inversion.
NAND Gate
A B Output (A.B)
0 0
1 0
0 1
1 1
COMPUTER STUDIES GRADE 10 – 11Page 164 of 256
This is NOT-OR which is equal to an OR gate followed by a NOT gate. The output is
True only if A and B are both False.
The symbol is an OR gate with a small circle on the output. The small circle represents
inversion.
Produce truth tables for given logic circuits (maximum of 2 inputs and 4 gates)
A B K L M N X
1 1
0 0
1 0
0 1
COMPUTER STUDIES GRADE 10 – 11Page 165 of 256
Produce a simple logic network from a given statement; e.g. if X AND Y are on then
the light will be on.
A traffic signal system will only operate if it receives an output signal (D = 1). This can only
occur if:
or (b) signal A is green (i.e. A = 1) and signals Band C are both red (i.e. B and C are both
0).
Design a logic network and draw a truth table for the above system.
Algorithms
process
input/output
start / stop
direction of flow
Decision
Decision Making
This is when conditions are given for the computer to decide whether it is true or false.
Conditions
When AND is used, both conditions must be true for the decision to be true.
When OR is used, any one of the conditions must be true for the decision to be true.
Examples
Assume: x = 10 y = 0 z = -5
1. x > y * 2 ___True__
2. z^2 = x _________________________
3. x > 3 OR y < z ____________________
4. y >=0 __________________________
5. x <> 10 _________________________
6. y <= 5 AND z >=0 _________________
COMPUTER STUDIES GRADE 10 – 11Page 168 of 256
There are 2 types of decision making structures which are IF … then …Else structure and
the CASE structure.
Solution
Enter mark
If mark <= 50 then
Output “passed”
Else
Output “failed”
End if
Activity
1. Ask the user for a number. Check whether it’s negative or positive and display a
suitable message.
2. Ask the user for a number. Check whether it’s odd or even and display a suitable
message.
Example
1. Students at a university are graded according to the following scale:
80-100 Distinction
70-79 Merit
0-69 Fail
COMPUTER STUDIES GRADE 10 – 11Page 169 of 256
Write and algorithm to enter a mark and display a suitable message given that
students are:
Solution
Enter mark
If mark >=80 and mark <= 100 then
Print “Distinction”
Else
If mark >= 70 and mark <=79 then
Print “Merit”
If mark >= 0 and mark <=69 then
Print “Fail”
End if
End if
End if
Activity
𝒘𝒆𝒊𝒈𝒉𝒕 𝒊𝒏 𝒌𝒊𝒍𝒐𝒈𝒓𝒂𝒎𝒔
𝑩𝑴𝑰 =
(𝒉𝒆𝒊𝒈𝒉𝒕 𝒊𝒏 𝒎𝒆𝒕𝒓𝒆𝒔)𝒙 (𝒉𝒆𝒊𝒈𝒉𝒕 𝒊𝒏 𝒎𝒆𝒕𝒓𝒆𝒔)
Using pseudocode or otherwise, write an algorithm that will input the ID, weight (kg)
and height (m) of 30 students, calculate their body mass index (BMI) and output their
ID,
A BMI greater than 25 will get the comment ‘OVER WEIGHT’, a BMI between 25 and
19 (inclusive) will get ‘NORMAL’ and a BMI less than 19 will get ‘UNDER WEIGHT’.
COMPUTER STUDIES GRADE 10 – 11Page 170 of 256
Case structure
A case structure is a decision structure that is used when there are more than two options
to choose from. It can be used in place of the nested if structure to make the algorithm
shorter.
Example
If Sunday is day one of the week and Saturday is day seven of the week, write an
algorithm which asks a user to enter a number and display the day of the week as a word.
Solution
Enter number
Case number of
1: output “Sunday”
2: output “Monday”
3: output “Tuesday”
4: output “Wednesday”
5: output “Thursday”
6: output “Friday”
7: output “Saturday”
Else
Output “Not a valid number”
End case
COMPUTER STUDIES GRADE 10 – 11Page 171 of 256
Activity
The computer studies teacher has given a test to her learners. Write an algorithm which allows
the teacher to enter a mark and display a suitable message given that learners are graded
according to the following scale:
80-100 A
70-79 B
60-69 C
50-59 D
40-49 E
30-39 F
0-29 U
Some variables need to be initialized before they are used. Variables that increase or
decrease in value need to be initialised. But variables
COMPUTER STUDIES GRADE 10 – 11Page 172 of 256
Initialise Variables
Most repetition problems come with a need to initialise variables for several reasons.
To initialize a variable is to give a starting value to a variable. We initialize variables when
Totaling
Counting
Finding the highest value
Finding the lowest value
For .. do loop
A for .. do loop is used when we know how many times some statements have to be
repeated.
General syntax
Example:
Mrs. Kapisi bought 50 items from a grocery store. Write an algorithm to enter the prices of
all the prices and get the total price.
Solution
Approach: The problem demands the use of a For..Do loop because it is asking for input
of 50 prices. The problem is also asking for a total price there we need to initialize the
variable total price to zero, then we will increase it inside the loop each time a price is
entered. The for do loop always has to be ended i.e end for. Output will only be displayed
at the end of the algorithm when all prices have been entered and the total has been
obtained.
COMPUTER STUDIES GRADE 10 – 11Page 173 of 256
TotalPrice = 0
For i = 1 to 50 do
Enter price
TotalPrice = TotalPrice + price
End for
Output “The total price is “, TotalPrice
Activity
Enter weights and ages for 11 babies and find the average weight and average age.
The repeat … until loop is used when we do not know how many times to repeat. It uses a
condition at the end of the loop and the loop is terminated when the condition becomes
true.
General syntax
Repeat
Statements
Until condition True
Example
Mrs. Kapisi gave a test to her grade 11 learners. Write an algorithm to enter the marks of
all the learners and get the total mark. Assume input is terminated by entering a -1 for the
mark.
Solution
We do not know how many learners are in the class but we are sure that they are more
than 1. In this case we can use a repeat … until loop which will be terminated by entering a
COMPUTER STUDIES GRADE 10 – 11Page 174 of 256
-1. The situation is also asking us to get the total mark, therefore we should initialize the
variable TotalMark.
TotalMark = 0
Repeat
Input mark
TotalMark = TotalMark + mark
Until mark = -1
Output “The total mark is “,TotalMark
Activity
Enter weights and ages for an unknown number of babies and find the average
weightand average age. Assume input is terminated by entering a -1 for the weight.
While …… D0 Loop
This is looping structure that is also used when we do not know how many times to repeat
some statements. A condition is used at the beginning of the loop and the loop is
terminated when the condition becomes false.
General syntax
NB: Algorithms that are written using a repeat loop can also be written using a while .. loop
COMPUTER STUDIES GRADE 10 – 11Page 175 of 256
1. Write an algorithm to enter marks for 100 leaners and find the highest and the lowest.
2. Write an algorithm to enter ages for an unknown number of people and find the age of
the youngest and oldest people. Input is terminated by entering a 0.
3. Write an algorithm to enter prices of some items. Find the most expensive price and
the cheapest. Input will be terminated by a -1 for the price.
1. Write an algorithm to enter marks for the leaners in the grade 10 geography class
and find how many passed given that for a learner to pass, they must get 50 or
more.
2. A survey was done at church to find out how many adults attended the church
service. Write an algorithm to enter ages for 500 church members and find out how
many adults were there given that adulthood starts at 18 years.
3. Write an algorithm to enter prices of a number of items. Find the most expensive
price and the cheapest price. Input will be terminated by a -1 for the price.
COMPUTER STUDIES GRADE 10 – 11Page 176 of 256
MOD
When MOD is used, you divide and give the remainder as your answer.
Activity 1
a. 85 MOD 6 = _______________
b. 15 MOD 3 = _______________
c. 25 MOD 6 = _______________
DIV
When DIV is used, you divide and take the whole number as your answer. Discard or
ignore the remainder.
i.e 10 DIV 3 = 3
55 DIV 10 = 5
16 DIV 6 = 2
COMPUTER STUDIES GRADE 10 – 11Page 177 of 256
Activity 2
a. 95 DIV 6 = _______________
b. 25 DIV 3 = _______________
c. 15 DIV 6 = _______________
Activity 3
1. Write an algorithm to enter 200 numbers and find out how whether they are positive or
negative. Print a suitable message.
2. Write an algorithm to determine how many times a 20 litre bucket can be used to
empty a 100 litre tank full of water.
Design algorithms which relate clearly to the requirements of the system where the
problem has been observed
Activity 4
inputs 50 numbers
3. Write an algorithm to read in ten numbers from the keyboard. Determine the
highest and lowest of the numbers entered, as well as the average of the positive
numbers. The output should must be as follows:
DISTINCTION 70 – 100
MERIT 55 – 69
PASS 40 – 54
FAIL 0 – 39
salary is more than N$2000, deduct (subtract) 10% tax. If the gross salary is less
than N$2000, deduct 5% tax. Print the name, gross salary, deduction and net salary
of each worker.
Use a trace table to dry run an algorithm containing one loop/iteration and at least
one decision structure
Keywords
Input / read / enter – this means enter a value from the values given under the mentioned
variable. NB: you cannot use the same value more than once.
Output / print / write / display – this means write the value of the variable under output.
FOR i = 1 to 9
OUTPUT i
End for
2. Construct a trace table to dry run the algorithm with the data
READ Number
FOR count = 1 TO Number
READ Mark
IF Mark < 50 THEN
OUTPUT Count, “Fail”
ELSE
IF mark > 80 THEN
OUTPUT Count, “Merit”
ELSE
OUTPUT Count, “pass”
Endif
Endif
NEXT count
COMPUTER STUDIES GRADE 10 – 11Page 181 of 256
3. Trace the following algorithm using the data: 18, 11, 14, 19, 17, 11, 8
Total = 0
FOR counter = 1 TO 7 DO
ENTER number
total = total + number
END FOR
average = total / counter
print average
4. Construct a trace table of the following pseudo code using the data values.
[2, 3, 7, 5, 1, 6,]
L = 0
read X
For i = 1 to 5 do
IF X > L THEN
L = X
ENDIF
read X
Next i
PRINT L
5. Use the following test data to dry run the following algorithm
Test data: 2, 3, 4, 1, 7, 8
For i = 5 to 10 do
Input N
if N MOD 2 = 0 then
N=N/2
else
N=3*N + 1
endif
output N
end for
COMPUTER STUDIES GRADE 10 – 11Page 182 of 256
REPEAT
Read A
Read B
IF A>B THEN
PRINT ‘First’
ELSE
IF A<B THEN
PRINT ‘Second’
ELSE
PRINT ‘Equal’
ENDIF
ENDIF
Until A>0 AND B>0
7. Study the following algorithm. What will be the output of the algorithm if the following
values are entered in sequence?
4 6 2 0 0 8 3 5.
READ A
READ B
REPEAT
C = (A+B)/2
PRINT C
READ A
READ B
UNTIL A = 0
COMPUTER STUDIES GRADE 10 – 11Page 183 of 256
8. Use the following test data to dry run the following algorithm
Test data: 2, 3, 4, 1, 7, 8
Input N
while N<>1 Do
if N MOD 2 = 0 then
N = N/2
else
N = 3*N + 1
end if
output N
Input N
Endwhile
Price = 0
Total = 0
while Price >= 0
input price
Total = Total + Price
End while
Total > 100?
Output Price – 10/100*Price
COMPUTER STUDIES GRADE 10 – 11Page 184 of 256
Trace the following flowchart and complete the trace table using the data
5, 9, 7, 7, 5, 2, 1, 2, 3
COMPUTER STUDIES GRADE 10 – 11Page 185 of 256
A b c DECISION OUTPUT
COMPUTER STUDIES GRADE 10 – 11Page 186 of 256
Algorithm Study
1. The following algorithm contains an error. Trace the algorithm and explain what the
error is.
1. SET X = 1
2. REPEAT
3. X = X + 2
4. Print X
5. UNTIL X = 10
b. FOR i = 1 to 9 step 2
OUTPUT i
Next i
Select appropriate test data and compare actual test results to expected results
Example of selecting appropriate test data.
If mark < 0 and mark > 0 then
Print “ERROR”
Endif
COMPUTER STUDIES GRADE 10 – 11Page 187 of 256
The following table shows how the above validation rule will be tested for normal, abnormal
and extreme data.
Activity
A programmer has been asked to write a routine which will calculate a person’s age in
years from the date of birth supplied as a parameter to the routine and today’s date, taken
from the system. The date of birth is supplied in the format dd/mm/yyyy and has already
been checked by the system to be a valid date before it is passed to the routine.
No person is expected to be over 120 years old.
Suggest suitable values for each of the types of test data, normal, extreme and abnormal,
and explain why you have chosen these values.
i. two values of normal data
ii. two values for extreme data
two values of abnormal data
- program indentation
COMPUTER STUDIES GRADE 10 – 11Page 188 of 256
Programs should be easy for other programmers to understand and should be easy to
maintain. Some of the rules include the following:
- Make use of meaningful variable names and identifiers, for example mark instead of
M or total instead of T. This allows other programmers to understand what the
variables stand for.
- Use comments to explain what a piece of code is doing. All comments are ignored
by the computer, they are not executed.
- Use indentation to group code in the same structure together which makes the
program easier to read and follow.
- distinguish between variables and constants
Activity
One of the programming standards expected of all programmers is that their programs
should be easy to understand. Mention 3 ways that programmers can use to ensure that
their programs are easy to understand.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
___
A large problem can be broken down into smaller sub-problems which are further broken
down and so on and so on until simpler modules which cannot be further broken down are
obtained. This is done so that the problem becomes easier to understand and solve. The
process of breaking down is called top down design.
COMPUTER STUDIES GRADE 10 – 11Page 189 of 256
Functions
A function is a block of code (a group of statements) which is called by its name from any
part of the program to perform a specific task and it always returns a value.
Procedures
A procedure is a block of code (a group of statements) which is called by its name from
any part of the program to perform a specific task and it may or may not return a value.
Function Procedure
Built-in functions
These are functions which are already written, fully tried and tested and are stored so that
they can be used over and over again to solve similar problems. There will be no need to
re-write the same type of module again but it can just be called from the library.
Activity
- runtime errors
- logic errors
PROGRAMMING ERRORS
An error is a mistake in the program. It is also called a bug. Locating and removing an error
from a program is called debugging.
TYPES OF ERROR
Syntax error
Logic error
This is an error in which the program is running but the output is wrong.
Runtime error
A runtime error is a program error that occurs while the program is running.
Examples of runtime errors are:
Activity
Explain each of the following types of program errors and give an example for each.
a. Syntax error
________________________________________________________________________
________________________________________________________________________
__
b. Runtime error
________________________________________________________________________
________________________________________________________________________
__
c. Logic error
________________________________________________________________________
________________________________________________________________________