KEMBAR78
Data Representation | PDF | Data Compression | Central Processing Unit
0% found this document useful (0 votes)
12 views55 pages

Data Representation

Uploaded by

kopar40872
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views55 pages

Data Representation

Uploaded by

kopar40872
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

COMPUTER STUDIES GRADE 10 – 11Page 138 of 256

Theme: 4
4.1. Data representation

Explain why binary numbers are used in computer systems.


Computers are digital devices, which means that they only recognize two states which are
0 and 1. 0 represents OFF and 1 represents ON. This is because computers are powered
by electricity which also has only two states ON and OFF. 0 means the absence of
electricity and 1 means the presence of an electric charge

Convert binary to denary and vice versa using positive values up to 16 bits.

Convert the following denary numbers to hexadecimal

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.

i.e 8 bits = 1 byte


COMPUTER STUDIES GRADE 10 – 11Page 139 of 256

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

A register is a special storage location used for temporary storage of addresses,


instructions or data during the fetch-decode-execute cycle.

Activity
Define the term register.
________________________________________________________________________
Convert positive denary values to hexadecimal and vice versa up to 4 hexadecimal
digits (integers only)

To convert a denary number to a hexadecimal number

 write down the denary number


 Continually divide-by-16 (sixteen) to give a result and a remainder. Please note that
the remainder ranges from 0 to 15. Please also note that digits 10 to 15 are
represented by letters A to F respectively. The division has to be done until the final
result equals zero.

Example: Convert the denary number 3 479 to hexadecimal.

Activity

Convert the following denary numbers to hexadecimal.

a. 5 628
b. 269
c. 2 095
COMPUTER STUDIES GRADE 10 – 11Page 140 of 256

To convert a denary number to a binary number

 write down the denary number


 continually divide-by-2 (two) to give a result and a remainder of either a “1” or a “0”
until the final result equals zero

Example: convert the denary number 25 to binary

Activity

Convert the following denary numbers to binary.

d. 75
e. 258
f. 1 056

Explain why hexadecimal notation may be the preferred number system

Programmers use the hexadecimal number system to represent binary addresses in a


human-readable format when writing code. This is because two digits of hexadecimal can
represent a whole byte (eight digits) in binary.

Since it is much easier to work with: B 5 A 4 1 A F C

rather than:
COMPUTER STUDIES GRADE 10 – 11Page 141 of 256

1011 10011010 0100 0001 1010 11111100

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.

Convert binary to denary

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.

So for example, converting a Binary to Denary number would be:

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

Convert the following binary numbers to hexadecimal numbers.

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 from hexadecimal to binary, first convert from hexadecimal to denary


then from denary to binary.

Convert hexadecimal to denary

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.

Example: convert to denary

99 = 9 ∗ 160

C = 12 ∗ 161 = 192

Then, we add the results.

192 + 9 = 20110 decimal


COMPUTER STUDIES GRADE 10 – 11Page 143 of 256

After converting to denary, follow the procedure to convert the denary number to binary.

Activity

Convert the following hexadecimal numbers to binary.

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 00 represents primary colour red

• # 00 FF 00 represents primary colour green

• # 00 00 FF represents primary colour blue

• # 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 MEDIA ACCESS CONTROL (MAC) ADDRESS refers to a number which uniquely


identifies a device on the internet. The MAC address is rarely changed so that a particular
device can always be identified no matter where it is.

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.

For example: 00 – 1C – B3 – 4F – 25 – FE is the MAC address of a device produced by


the Apple Corporation (code: 001CB3) with a serial number of 4F25FE.

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:

STO FFA4 (assembly code)

A5E4 FFA4 (machine code using hexadecimal values)

1010 0101 1110 0100 1111 1111 1010 0100 (machine code using binary)

Explain how hexadecimal numbers are used in debugging

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

The microprocessor is connected to the rest of the computer components through a


motherboard. A chipset co-ordinates the data flow between the system’s hardware
components through the use of buses. A chipset is a group of interconnected chips on the
motherboard that controls the flow of information between the microprocessor and other
system components connected to the motherboard. The input-output interface provides a
method for transferring information between internal storage and external I/O devices.
Peripherals connected to a computer need special communication links for interfacing
them with the central processing unit. Therefore, Peripheral Component Interconnect
(PCL) is required. It connects the peripherals to the CPU through the PCL slots on the
motherboard. The PCL bus transports the data between the CPU and peripherals.
COMPUTER STUDIES GRADE 10 – 11Page 146 of 256

Explain the role of memory in the data path

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.

Explain the advantages of buffering, spooling and cache 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

Spooling is a technique used to speed up communication between devices which operate


at different speeds e.g. CPU and printer.

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.

Explain the role of interrupts

During the fetch-decode-executed-reset cycle, there is chance that an instruction of higher


priority than the current instruction can occur. This is called an interrupt because it disturbs
the whole cycle. Interrupts are used to ensure that instructions of higher priority are not left
unattended.

Whenever an interrupt occurs, a series of events takes place, as follows.

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.

Describe the flow of data in the fetch-decode-execute cycle


1. Copy address in the Program Counter to Memory Address Register
2. Increment Program Counter 1
3. Copy instruction from memory to Memory Data Register using the address stored in
the Memory Address register.

4. Copy instruction from Memory Data Register to Current Instruction Register.

5. Decode the instruction in the Current Instruction Register. To decode is to translate


an instruction into a language which the computer can understand (binary digits or
machine code). This is because instructions from programmers are not understood by
the computer unless they are translated. Decoding is done in the CIR

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

Arithmetic and Logic Unit (ALU)

This is the unit that carries out all the arithmetic calculations and logic operations.

Arithmetic calculations are multiplication, division, addition, subtraction and exponentiation.

These are comparisons. The following symbols used

= equal to, <> not equal to, <= less than or equal to, >= greater than or equal to, < less
than, >greater than

Control Unit (CU)

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.

Memory Buffer Register (MBR)

The MDR uses the address in the MAR to copy instructions and data from memory.

Memory Address Register (MAR)

The MAR contains the data that is currently being fetched.

Instruction Register (IR)

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 IR copies the instructions from the MDR


COMPUTER STUDIES GRADE 10 – 11Page 149 of 256

Memory Data Register (MDR)

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.

I/O Address Register (I/O AR)

Program Counter (PC)

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]
___________________________________________________________________
___________________________________________________________________

b. At a particular point in a program, the program counter (PC) contains the


value 200.
i. State the expected value contained in the PC after the instruction held
at location 200 has been fetched. Explain your answer. [2]
___________________________________________________________________
ii. After the instruction has been processed, the value in the PC is 180.
Explain why the value has changed to 180. [2]
___________________________________________________________________
___________________________________________________________________
c. Apart from the PC, name four other registers used during the fetch-decode-
execute cycle and state the purpose of each. [8]
___________________________________________________________________
___________________________________________________________________
COMPUTER STUDIES GRADE 10 – 11Page 151 of 256

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

Musical Instrument Digital Interface (MIDI)

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

GIF (graphics interchange format)

This is a format for storing images that are compressed to reduce transfer time. It supports
animation of the images.

MP3 and MP4

These are storage formats for audio and videos. MP3s are compressed audio files.

PDF (Portable Document Format)

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.

AVI (Audio Video Interleave)

This is a format for storing audio files. It has some compression limitations and therefore
files are generally large.

WAV (Waveform Audio)

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

WMA and WMV (Windows Media Audio and Video)

This is a format for storing audio files and video files. WMA can compress audio files
without loss of quality.

txt (plain text)

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.

RTF (Rich Text Format)

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

- Automatic Repeat reQuests (ARQ)

Methods of error detection and correction

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,

The parity, bit is always a 1,

Space parity

The parity bit is always a 0.

The table below, figure 4-14, summerises parity checking.

Source data Even Odd parity Mark parity Space


bits parity bit bit bit parity bit
1100101 0 1 1 0
0110111 1 0 1 0
1111110 0 1 1 0

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:

 Single digit error 21456 21486


 Transposition of adjacent digits error 21456 21546
 Jump transposition error 21456 21654
 Twin error 21455 21466
 Phonetic error speech; the computer may take as you have
spoken a certain number, for example, thirteen as eighteen;
21413 21418
 Jump twin error 21156 21456
 Omitting or typing extra digits 21675 2167 or 216757

Check digit algorithms are generally intended to capture human transcription errors.

An Example of check digit algorithm is the ISBN -13.

Here are the steps using 978999457385.

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

1. Sum all the digits at odd positions, which is 9+8+9+4+7+8= 45


2. Sum all the digits at even position and multiply by 3: 7+9+9+5+3+5 =38*3=114
3. Add the answers (45+114) = 159
4. get its mod by 10 to get the remainder: 159 mod 10 =9
5. Therefore, the check digit is 9.
COMPUTER STUDIES GRADE 10 – 11Page 157 of 256

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

A checksum is an error detection method created by calculating the binary values in a


packet or other block of data using some algorithm. The calculated checksum is stored
together with the data and sent along with the data. So, for each block of data sent,
checksum is calculated and compared with the existing checksum. Non-matching
checksums indicate an error could have occurred. If an error has occurred, a request is
send for retransmission. If the received checksum value matches the sent value, the
transmission is considered to be successful and error-free and an acknowledgement is
sent. A checksum may also be known as a hash sum.

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

Checksum = 2446-2304 = 142

Checksum is a better way of detecting errors than the parity checking.

We also have methods of correcting errors such as Automatic Repeat requests (ARQ).
COMPUTER STUDIES GRADE 10 – 11Page 158 of 256

Automatic Repeat Requests (ARQ)

Automatic Repeat Request (ARQ) is an error-control mechanism for data transmission


which uses acknowledgements, negative acknowledgements and timeouts to achieve
reliable data transmission over an unreliable communication service. When the sending
device fails to receive an acknowledgement to confirm the data has been received, it
usually retransmits the data after a predefined timeout and repeats the process a
predetermined number of times until the transmitting device receives the
acknowledgement.

An Acknowledgement (ACK) is a short message sent by the receiver to the transmitter to


indicate that it has correctly received a data packet.

A Negative Acknowledgement (NACK) is a short message sent by the receiver to the


transmitter to indicate that it has incorrectly received a data packet.

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

Interpret simple logic circuits using a truth table

The AND Gate

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.

Symbol and truth table for the AND gate

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.

Symbol and truth table for the OR gate

OR Gate
A B Output (A+B)
0 0
1 0
0 1
1 1
COMPUTER STUDIES GRADE 10 – 11Page 163 of 256

The NOT Gate

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

Symbol and truth table for the NOT gate

NOT Gate
A Output (A)
0
1

The NAND Gate

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.

Symbol and truth table for the NAND gate

NAND Gate
A B Output (A.B)
0 0
1 0
0 1
1 1
COMPUTER STUDIES GRADE 10 – 11Page 164 of 256

The NOR Gate

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.

Symbol and truth table for the NOR gate


NOR Gate
A B Output (A+B)
0 0
1 0
0 1
1 1

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:

either (a) signal A is red (i.e. A = 0)

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.

Develop algorithms using algorithm tools such as pseudocode and flowcharts.

Algorithms

An algorithm is a set of steps which must be followed in order to solve a problem. It


is a step by step solution to a problem. There are two types of algorithms which are
pseudocode and flowcharts.
COMPUTER STUDIES GRADE 10 – 11Page 166 of 256

Pseudocode is an algorithm in the form of words and sentences while flowchart is


an algorithm in the form of symbols.

Symbols used in flowcharts

process
input/output

start / stop

direction of flow

Decision

Solve problems in a structured way, using logic and reason.

Steps when writing algorithms

1. What are my inputs?


2. What processing needs to be done?
3. What is the output?
COMPUTER STUDIES GRADE 10 – 11Page 167 of 256

Decision Making
This is when conditions are given for the computer to decide whether it is true or false.

Conditions

A condition is an expression that can be evaluated by either true or false

Symbols used in conditions are

> greater than

< less than

>= greater than or equal to

<= less than or equal to

<> Not equal to

Combining conditions with AND or OR

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

Give decisions (True or False) for the following conditions:

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

Decision making structures

There are 2 types of decision making structures which are IF … then …Else structure and
the CASE structure.

IF … then … else Structure – this is used when there is a decision to be made.


Example
A learner can only pass if they get a minimum mark of 50 in an examination. Write an
algorithm which accepts a mark for a learner and display a suitable message.

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.

Nested IF … then … else structure

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

The body mass index (BMI) of a person is calculated as:

𝒘𝒆𝒊𝒈𝒉𝒕 𝒊𝒏 𝒌𝒊𝒍𝒐𝒈𝒓𝒂𝒎𝒔
𝑩𝑴𝑰 =
(𝒉𝒆𝒊𝒈𝒉𝒕 𝒊𝒏 𝒎𝒆𝒕𝒓𝒆𝒔)𝒙 (𝒉𝒆𝒊𝒈𝒉𝒕 𝒊𝒏 𝒎𝒆𝒕𝒓𝒆𝒔)
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,

BMI and a comment as follows:

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

Looping or Iteration structures

Looping or iteration means repetition of one or more statements in an algorithm. For


example you may need to enter marks for 20 learners. In this case it means that the
entering of marks has to be repeated 20 times.

There are three repetition structures, which are


 For .. do loop
 While … do loop
 Repeat … Until loop

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

For i = start_value to end_value do


Statements
End for

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.

Repeat …… Until Loop

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

While condition true do


Statements
End while

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

Problems that need getting the highest value


 Initialize the highest to the lowest possible value,
 Enter each value and compare it with the highest
 If value is higher than the highest, then reassign highest to this value.

Problems that need getting the lowest value


 Initialize the lowest to the highest possible value,
 Enter each value and compare it with the lowest
 If value is lower than the lowest, then reassign lowest to this value.
Activity

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.

Problems that need counting

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

Appropriately apply operators +, -, *, /, MOD and DIV to solve problems

MOD and DIV


These are special division operators.

MOD
When MOD is used, you divide and give the remainder as your answer.

i.e 7 MOD 2 = 1 - the remainder is 1 when we divide 7 by 2.


50 MOD 4 = 2 - the remainder is 2 when we divide 50 by 4.
8 MOD 2 = 0 - the remainder is 0 when we divide 8 by 2.

Activity 1

Write the answers for the following problems.

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

Write the answers for the following problems.

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

1. Write an algorithm to enter 300 numbers and output the following

 The average number

 How many were positive

 How many were negative

 How many were odd

 How many were even


COMPUTER STUDIES GRADE 10 – 11Page 178 of 256

2. Write an algorithm, using pseudocode or otherwise, which;

 inputs 50 numbers

 checks whether each number is in the range 1000 to 9999

 Prints how many of the numbers were out of range.

 outputs the percentage of input numbers which were out of range.

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:

The highest number is


The lowest number is
The average of the positive numbers is

4. Write a program or give an algorithm (flowchart or pseudo code) to solve the


following problem:

Pupils are graded in a test, based on the following marks:

DISTINCTION 70 – 100
MERIT 55 – 69
PASS 40 – 54
FAIL 0 – 39

Any other mark displays an error message.


Write an algorithm to input the marks for grade 10 learners and for each learner
output either a grade or an error message. Assume that input is terminated by a -1.

5. Give an algorithm (flowchart or pseudo code) to solve the following problem:


You need to enter the names of the workers and their gross salary. If the gross
COMPUTER STUDIES GRADE 10 – 11Page 179 of 256

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.

6. You need to pack some books in cardboard boxes. Design an algorithm(pseudo


code or flowchart) which measures the volume of boxes by accepting as input
W (width), H (height) and B (breadth). The only boxes permissible are boxes with a
volume of 100 to 600. Any box outside this range is rejected. The books you need
to pack amount to 6 000 volume units in total.
Display a message for each box to be accepted or rejected.

7. Customers can withdraw cash from an Automatic Teller Machine (ATM).

• withdrawal is refused if amount entered > current balance


• withdrawal is refused if amount entered > daily limit
• if current balance < $100, then a charge of 2% is made
• if current balance $100, no charge is made

Write an algorithm which inputs a request for a sum of money, decides if a


withdrawal can be made and calculates any charges. Appropriate output messages
should be included.
COMPUTER STUDIES GRADE 10 – 11Page 180 of 256

Use a trace table to dry run an algorithm containing one loop/iteration and at least
one decision structure

Trace tables (Dry running algorithms)


Algorithms can be dry run by drawing Trace tables. A trace table is a table drawn to test
algorithms by tracking the values of variables as they change throughout the algorithm. A
trace table helps to locate logic errors that occur while the algorithm is running.

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.

 is the same as = and we read it as “assigned”

1. Trace the following algorithm and give the output.

FOR i = 1 to 9
OUTPUT i
End for
2. Construct a trace table to dry run the algorithm with the data

4, 40, 90, 60, 50, 70

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

6. What will be the output produced by the following algorithm if A = 1 and B = 2?

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

9. Work through the following algorithm and construct a trace table

Test data: 75.00, 30.00, 20.00, 0

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

Identify where there is an error.


Suggest ways of removing the errors.

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

2. Write down the output of the following algorithms


a. FOR i = 1 to 9
OUTPUT i
Next i

b. FOR i = 1 to 9 step 2
OUTPUT i
Next i

c. Re-write each of the above algorithms using:


i. a REPEAT loop
ii. a WHILE loop

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.

Example of test data Type of test data Expected result


10, 30, 99 Normal data Values accepted as
normal
-1, 120, -80 Abnormal Error. Values not
accepted because they
are abnormal.
0 and 100 Extreme Value accepted

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

Describe and apply the following to facilitate the maintenance of programs:


- use of comments in program codes

- distinguish between variables and constants

- meaningful identifier names (variables, constants, programs, functions, etc.)

- 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.
________________________________________________________________________
________________________________________________________________________
________________________________________________________________________
___

Define the scope of separate modules

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 and procedures

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.

The differences between a function and a procedure

Function Procedure

 Always returns a value  May or may not return a value


 Always returns a single value  May returns more than one value or
no value at all

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.

Examples of standard functions are

 RANDOM – This function generates random numbers,


 SORT – This function sorts values in either ascending or descending order,
 INT, - this function gives integer values.
COMPUTER STUDIES GRADE 10 – 11Page 190 of 256

Activity

1. A program can be written using separate modules.


a. Explain the meaning of the term module.
________________________________________________________________________
________________________________________________________________________
b. Give one advantage of writing a program using separate modules.
________________________________________________________________________
________________________________________________________________________

Describe and give examples of the following types of programming errors:

- syntax errors (compilation errors)

- 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

This is an error caused by wrongly using a programming language. It is like a grammatical


error in a language. A program with a syntax error will not run at all so the error must be
corrected before running the program. The compiler or interpreter will indicate to the
programmer that there is a syntax error in the program.
COMPUTER STUDIES GRADE 10 – 11Page 191 of 256

Examples of syntax errors are

- Misspelling a keyword eg wile instead of while or plint instead of print


- If without Endif
- Omitting a bracket or a quote.
Syntax errors can be detected by the translator when it tries to translate and run the
program. This is called translator diagnosis. A debbuger can also be used to detect
syntax errors.

Logic error

This is an error in which the program is running but the output is wrong.

Examples of logic errors are

- B+C instead of B-C


- Unending loop due to wrong sentinel value
Logic errors can be difficult to detect because a program with logic errors will run. They
can be detected by tracking variables and going line by line and this is called desk
checking. Desk checking involves drawing a trace table. A debugger can also be used to
track values of variables

Runtime error
A runtime error is a program error that occurs while the program is running.
Examples of runtime errors are:

 running out of memory


 division by zero
COMPUTER STUDIES GRADE 10 – 11Page 192 of 256

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
________________________________________________________________________
________________________________________________________________________

You might also like