Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
18 views

Processing and Logic

The document discusses different number systems including decimal (base-10), binary (base-2), octal (base-8), and hexadecimal (base-16). It explains how each system works by representing numbers as place values based on its numerical base. For example, in binary each digit represents powers of 2, in octal powers of 8, and so on. The document also provides examples of converting numbers between decimal and other bases using the successive division method, which repeatedly divides the number by the new base and records the remainders as digits in the new system.

Uploaded by

jonathan otshudi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Processing and Logic

The document discusses different number systems including decimal (base-10), binary (base-2), octal (base-8), and hexadecimal (base-16). It explains how each system works by representing numbers as place values based on its numerical base. For example, in binary each digit represents powers of 2, in octal powers of 8, and so on. The document also provides examples of converting numbers between decimal and other bases using the successive division method, which repeatedly divides the number by the new base and records the remainders as digits in the new system.

Uploaded by

jonathan otshudi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 84

Processing and Logic Concepts

UNIT 1 - Number Systems


1.1 Introduction to number systems
A number system is a way to represent numbers. We should already be used to the decimal
number system that is base-10. Other common number systems are hexadecimal (base-16), octal
(base-8) and binary (base-2). How do number systems work? Let's take a closer look at what we
have been doing all these years.

1.1.1 Base-10
Base-10 is known as Decimal. When we count as follows 1, 2, 3, 4, 5, 6, 7, 8, 9 we have no more
numbers to represent the next value once we reach the number 9. We need to set it back to 0
and add 1 to the tens digit, giving us 10. This process continues until we get 99. There is no way
to represent a larger number than 99 with two digits, so we add another digit, giving us 100.

Let’s look at what is going on mathematically, using 2347 as an example.

• There are 2 groups of a thousand. 1000 = 10*10*10 which can also be written as 103.

• There are 3 groups of a hundred. 100 = 10*10 or 102.

• There are 4 groups of ten. 10 = 101.

• Finally, there are 7 groups of one, and 1 = 100. (Any number to the power of 0 equals 1.)

© PIHE PLC 1|Page


This is essentially the definition of base-10. To get a value of a number in base-10, we follow this
pattern.

So, the number 2347 can be written as 2*103 + 3*102 + 4*101 + 7*100

Here are a few more examples:

892 = 8*102+9*101+2*100

1147 = 1*103+1*102+4*101+7*100

53 = 5*101+3*100

Any base has the same number of digits as its base. So base-5 has five digits (0-4), base-7 has
seven digits (0-6), etc. We always use base-10, and it comes naturally to us. If we understand the
patterns behind base-10, we can understand the other bases better.

1.1.2 Base-2
Base-2 is known as binary. Binary is made up of 1’s and 0’s limiting us to two digits. It is important
to know that mathematically binary is no different to any other number system. Like the base-10
number system after we get to 1, we run out of numbers for any higher value. We then need to
change the 1 to 0 and add another digit, leaving us with 10. This can become confusing as 10 in
base-10 is not the same as 10 in base-2. We use subscripts to represent the different number
systems.

1010 = 1010

102 = 210 (10 in binary is equal to the number two in decimal)

1.1.3 Base-8
Base-8 is known as Octal. Octal limits us to only 8 digits. 0, 1, 2, 3, 4, 5, 6, 7. There is no such
thing as 8 or 9 in this number system. Like the base-10 number system, after we get to 7, we run
out of numbers to represent any higher values. We then need to change the 7 to 0 and add
another digit. 8 in base-8 is 10 or 108. This can become confusing when read. When working in
number systems other than decimals we tend to read these numbers differently. 10 will be
pronounced “one-zero”. This should help you differentiate 10 (“one-zero”) in decimal being ten
and 10 (“one-zero”) in Octal being 8.

810 = 108

© PIHE PLC 2|Page


1.1.4 Base-16
Base-16 is known as Hexadecimal. It is commonly used in computer programming and is very
important to understand. Since we are working with base-16, we have 16 digits. So, we have 0,
1, 2, 3, 4, 5, 6, 7, 8, 9, ... We’ve run out of numbers, but we still need six more. Instead, we use
letters of the alphabet, starting with A and continuing to F. Here’s a table with all the digits of base-
16:

Base 16 Digit Value

0 0

1 1

2 2

3 3

4 4

5 5

6 6

7 7

8 8

9 9

A 10

B 11

C 12

D 13

E 14

F 15

© PIHE PLC 3|Page


1.2 Converting from base 10
The successive division method is used to convert any decimal value to any other base. There
are four steps in successive division.

1.2.1 Decimal to binary


Let’s look at what is going on mathematically, converting 26 to binary as an example.

• Step 1:

Divide the decimal number to be converted by the value of the new base. (To convert to binary,
divide by two)

Base Number Remainder Answer

2 26

13

• Step 2:

Record the remainder from Step 1 as the right most digit of the new base number.

Base Number Remainder Answer

2 26 0

13 0

© PIHE PLC 4|Page


• Step 3:

Divide the answer of the previous division by the new base.

Base Number Remainder Answer

2 26 0

13 0

6 1

• Step 4:

Record the remainder from Step 3 as the next digit (to the left of the number recorded in step 2)

Base Number Remainder Answer

2 26 10

13 0

6 1

Steps 3 and 4 must be repeated, until you get an answer of zero in Step 3.

Base Number Remainder Answer

2 26 010

13 0

6 1

3 0

© PIHE PLC 5|Page


Base Number Remainder Answer

2 26 1010

13 0

6 1

3 0

2 1

Base Number Remainder Answer

2 26 11010

13 0

6 1

3 0

1 1

0 1

Therefore 2610=110102

© PIHE PLC 6|Page


1.2.2 Decimal to Octal
Let’s look at what is going on mathematically, converting 416 to octal as an example.

• Step 1:

Divide the decimal number to be converted by the value of the new base. (To convert to octal,
divide by eight)

Base Number Remainder Answer

8 416

52

• Step 2:

Record the remainder from Step 1 as the right most digit of the new base number.

Base Number Remainder Answer

8 416 0

52 0

• Step 3:

Divide the answer of the previous division by the new base.

Base Number Remainder Answer

8 416 0

52 0

6 4

© PIHE PLC 7|Page


• Step 4:

Record the remainder from Step 3 as the next digit (to the left of the number recorded in step 2)

Base Number Remainder Answer

8 416 40

52 0

6 4

Steps 3 and 4 must be repeated, until you get an answer of zero in Step 3.

Base Number Remainder Answer

8 416 640

52 0

6 4

0 6

Therefore 41610 = 6408

© PIHE PLC 8|Page


1.2.3 Decimal to Hexadecimal
Let’s look at what is going on mathematically, converting 941 to hexadecimal as an example.

• Step 1:

Divide the decimal number to be converted by the value of the new base. (To convert to
hexadecimal, divide by 16.) Convert the remainder to its hexadecimal value.

Base Number Remainder Answer

16 941

58 13 = D

• Step 2:

Record the remainder from Step 1 as the right most digit of the new base number.

Base Number Remainder Answer

16 941 D

58 13 = D

• Step 3:

Divide the answer of the previous division by the new base.

Base Number Remainder Answer

16 941 D

58 13 = D

3 10 = A

© PIHE PLC 9|Page


• Step 4:

Record the remainder from Step 3 as the next digit (to the left of the number recorded in step 2)

Base Number Remainder Answer

16 941 AD

58 13 = D

3 10 = A

Steps 3 and 4 must be repeated, until you get an answer of zero in Step 3.

Base Number Remainder Answer

16 941 3AD

58 13 = D

3 10 = A

0 3

94110 = 3AD16

© PIHE PLC 10 | P a g e
1.3 Converting to base 10
Next, we will look at how we can convert from the other number systems to the Decimal number
system.

1.3.1 Binary to Decimal


Let’s look at what is going on mathematically, converting 101102 to base-10 as an example.

• Step 1:

Split the number up into columns with one digit in each column. Label columns from right to left
starting at 0.

4 3 2 1 0

1 0 1 1 0

• Step 2:

Use the label of each column and set it to the power of the base the number is in. The Binary
base is used in the example below.

24 23 22 21 20

1 0 1 1 0

• Step 3:

Multiply the value in the column to the base and its power calculated in Step 2

24 23 22 21 20

1 0 1 1 0

24 * 1 = 16 23 * 0 = 0 22 * 1 = 4 21 * 1 = 2 20 * 0 = 0

© PIHE PLC 11 | P a g e
• Step 4:

Add the values in each column together calculated in Step 4

16 + 0 + 4 + 2 + 0 = 22

Therefore 101102 = 2210

1.3.2 Octal to Decimal


Let’s look at what is going on mathematically, converting 2578 to base-10 as an example.

• Step 1:

Split the number up into columns with one digit in each column. Label columns from right to left
starting at 0.

2 1 0

2 5 7

• Step 2:

Use the label of each column and set it to the power of the base the number is in. The Octal base
is used in the example below.

82 81 80

2 5 7

• Step 3:

Multiply the value in the column to the base and its power calculated in Step 2

82 81 80

2 5 7

82 * 2 = 128 81 * 5 = 40 80 * 7 = 7

© PIHE PLC 12 | P a g e
• Step 4:

Add the values in each column together calculated in Step 3

128 + 40 + 7 = 175

Therefore 2578 = 17510

1.3.3 Hexadecimal to Decimal


Let’s look at what is going on mathematically, converting 2B3C to base-10 as an example.

• Step 1

Split the number up into columns with one digit in each column. Label columns from right to left
starting at 0.

3 2 1 0

2 B 3 C

• Step 2

Convert the Hexadecimal value, A – F to its decimal value 10 - 15

3 2 1 0

2 B = 11 3 C = 12

• Step 3

Use the label of each column and set it to the power of the base the number is in. The
Hexadecimal base is used in the example below.

163 162 161 160

2 B = 11 3 C = 12

• Step 4:

Multiply the value in the column to the base and its power calculated in Step 3

© PIHE PLC 13 | P a g e
163 162 161 160

2 B = 11 3 C = 12

163 * 2 = 8192 162 * 11 = 2816 161 * 3 = 48 160 * 12 = 12

• Step 5:

Add the values in each column together calculated in Step 4

8192 + 2816 + 48 + 12 = 11068

Therefore 2B3C16 = 1106810

1.4 Shortcut notation

1.4.1 Octal Notation


Computers store instructions and data, by using transistors, each of which can be in one of two
states: off or on, represented with a 0 and 1 (binary). Each binary digit (0 or 1) is called a bit.
Every instruction we give a computer and every number, character, and object is ultimately
represented in binary notation.

It is very useful to have a shortcut notation for binary numbers when printing out the contents of
memory. If the contents of memory were to be printed in binary, several pages of zeros and ones
would be required. This form is very difficult to use for a memory dump, because of the length of
the output and the narrow choice of characters.

To reduce the volume and print time of the memory dump and to show memory content with a
greater variety of symbols, either the octal or hexadecimal number system is used as a shortcut
notation. The selection of the shortcut notation is determined by the memory organization of the
machine, any computer will always use either octal or hexadecimal, but not both.

In octal notation we learn how to convert from binary to octal and from octal to binary.

Binary to Octal

Let’s look at what is going on mathematically, converting 1100112 to octal as an example.

• Step 1:

© PIHE PLC 14 | P a g e
Divide the binary digits into groups of 3 starting from right to left. You can add zeros to the front
should your binary number not be able to split into groups of 3

110 011

• Step 2:

Convert each group into 1 digit of octal (Refer back to the binary to decimal conversion section if
below is confusing)

110 011

4+2+0 0+2+1

6 3

Therefore 1100112 = 638

Octal to Binary

Let’s look at what is going on mathematically, converting 2468 to binary as an example.

• Step 1:

Convert each octal digit to 3 binary digits

2 4 6

0+2+0 4+0+0 4+2+0

010 100 110

• Step 2:

Write down the binary numbers from right to left.010100110

© PIHE PLC 15 | P a g e
Therefore 2468 = 0101001102

1.4.2 Hexadecimal notation


In hexadecimal notation we learn how to convert from binary to hexadecimal and from
hexadecimal to binary.

Binary to Hexadecimal

Let’s look at what is going on mathematically, converting 110101112 to hexadecimal as an


example.

• Step 1:

Divide the binary digits into groups of 4 starting from right to left. You can add zeros to the front
should your binary number not be able to split into groups of 4

1101 0111

Step 2:

Convert each group into 1 digit of hexadecimal. Remember to write your answers of 10 – 15 as A
– F.

1101 0111

8+4+0+1 0+4+2+1

13 = D 7

Therefore 110101112 = D716

Hexadecimal to Binary

Let’s look at what is going on mathematically, converting 2A916 to binary as an example.

• Step 1:

Convert each hexadecimal digit to 4 binary digits

© PIHE PLC 16 | P a g e
2 A = 10 9

0+0+2+0 8+0+2+0 8+0+0+1

0010 1010 1001

• Step 2:

Write down the binary numbers from right to left

001010101001

Therefore 2A916 = 0010101010012

1.5 Arithmetic
An arithmetic logic unit (ALU) represents the fundamental building block of the central processing
unit of a computer. An ALU is a digital circuit used to perform arithmetic and logic operations.
Examples of arithmetic operations are addition, subtraction, multiplication, and division.
Remember that all information in a computer is stored and manipulated in the form of binary
numbers. These numbers can be converted to any other base.

1.5.1 Addition
The steps required to add two numbers are the same in all number systems. Addition always
begins by adding the rightmost column. If the column total is equal to or greater than the value of
the base, a 'carry-over' will occur. Whenever a 1 is carried to the next column, the value being
carried is equal to the value of the base. If the column total from Step 1 is equal to or greater than
the base, subtract the value of the base from the column total and carry a 1 to the next column. If
the column total is still equal to or greater than the base, repeat Step 2.

© PIHE PLC 17 | P a g e
Example adding the decimal numbers 937 to 129:

Carry 1 1

9 3 7

+ 1 2 9

Answer 1 0 6 6

Adding Binary

Example adding the binary numbers 1010 to 0011:

Carry 1

1 0 1 0

+ 0 0 1 1

Answer 1 1 0 1

Adding Octal

Example adding the octal numbers 167 to 765:

Carry 1 1

1 6 7

+ 7 6 5

Answer 1 1 5 4

© PIHE PLC 18 | P a g e
Adding Hexadecimal

Example adding the hexadecimal numbers 8A5C to F39A:

Carry 1 1

8 A = 10 5 C = 12

+ F = 15 3 9 A = 10

Answer 1 7 D F 6

Remember to write 10 – 15 as A – F.

1.5.2 Subtraction
The principles of subtraction using decimal numbers can be applied to subtraction of other bases
as well. Subtraction always begins by subtracting the rightmost column. If the number, we are
subtracting from is less than the number it is being subtracted by it is necessary to borrow from
the column to the left. The value borrowed is always equal to the base of the number system
being subtracted. The second step is to subtract the lower value from the top value.

Subtracting decimal

Example subtracting the decimal numbers 937 from 129:

9 32 7 + 10

- 1 2 9

Answer 8 0 8

Subtracting Binary

© PIHE PLC 19 | P a g e
Example subtracting the binary number 1101 from 0011:

1 10 0+2 1

- 0 0 1 1

Answer 1 0 1 0

Subtracting Octal

Example subtracting the octal number 456 from 173:

43 5+8 6

- 1 7 3

Answer 2 6 3

Subtracting Hexadecimal

Example subtracting the hexadecimal number B78 from 8AB:

BA 7 6 + 16 8 +16

- 8 A B

Answer 2 C D

Remember to write 10 – 15 as A – F.

© PIHE PLC 20 | P a g e
1.6 Complement arithmetic
Most computer manufacturers distinguish between positive and negative numbers by designating
the leftmost bit in a binary number as an indicator of whether a number is positive or negative. If
this sign bit is a zero, the number is positive, and the remaining bits represent the value of the
number. If the leftmost sign bit is a one, the number is considered negative and the remaining bits
represent the value of the negative number.

Example: 001011 (binary) = +11 (decimal)

Conversely: 101011 (binary) = -11 (decimal)

Computer hardware was built to recognize positive and negative numbers. This method meant
installing complex hardware circuits. This method was soon abandoned in favour of what is called
complement arithmetic.

In complements arithmetic, the negative numbers are represented as the mathematical


complement of the positive numbers. There are two ways to complement binary numbers. This is
called One's Complement and Two’s Complement.

Complements will not be tested in the exam.

1.6.1 One’s Complement


1’s complement of a binary number is the binary number obtained by inverting each bit in it.

Examples:

1's complement of 7 (0111) is 8 (1000)

1's complement of 12 (1100) is 3 (0011)

1.6.2 Two’s Complement


2’s complement of a binary number is 1 added to the 1’s complement of the binary number.

Examples:

2's complement of 7 (0111) is 9 (1001) (add 1 to the result of 1’s complement)

2's complement of 12 (1100) is 4 (0100) (add 1 to the result of 1’s complement)

© PIHE PLC 21 | P a g e
1.7 Overflow
Overflow occurs when the number of bits resulting from an arithmetic operation exceeds the
maximum number of bits that the computer can handle.

Let’s look at what is going on mathematically, when adding 27 and 16 in a computer that can only
handle a maximum of 6 bits.

Two positive numbers have been added and produced a negative binary number. It is negative
because the leftmost sign bit is 1. This is problematic because adding two positive numbers
cannot have a negative result. When two positive binary numbers are added to give a negative
binary number, it is called a size error.

Overflow’s will not be tested in the exam.

© PIHE PLC 22 | P a g e
Processing and Logic Concepts
UNIT 2 Computer Logic
2.1 Introduction to computer logic concepts

Computer logic involves operations on data that works according to mathematical logic rules.
Basic computers developed ways to deal with numbers and logical states. These computers used
electronic gates, vacuum tubes and transistors, that were able to apply specific operations that
lead to precise results. Binary logic forms the basis of all computer operations. This logic has
evolved by adapting the principles of reasoning (where a statement can either be true or false) to
the nature of the electronic components that make up computers.

A bistable device can be in one of two possible states – never in both at the same time. For
example, a lamp is either on or off. Electrical current either flows or there is no current. Similarly,
computers use a binary system of logic in which variables can have only one of two possible
values, 0 or 1. A variable is an element or a factor that is subject to change. The bistable nature
of electronic components is at the heart of binary logic, which effectively models a computer's
operations. In computers, logic operations are incorporated into electronic circuits in the form of
logic gates. These operations will be discussed further in this section.

© PIHE PLC 1|Page


2.2 Logic operators and diagrams

The concept of logical operators is simple. Logic operators allows a program to make a decision,
based on conditions. Each variable is considered a condition that can be evaluated to a true/on/1
or false/off/0 value. The value of the conditions is used to determine the overall value.

Logic diagrams exist in many formats. Logic gates, truth tables and Venn diagrams are all
examples of logic diagrams. Logic diagrams are used to visually represent logic reasoning. Binary
logic uses three basic logic operations, AND, OR and NOT. The table below illustrates the
difference between these operations and is visually represented using Logic diagrams.

Functio
Operator Logic gate Truth Table Venn Diagram
n

AND A B A.B=C

The result
0 0 0
C= A.B
is true only
if both or 0 1 0
variables
C = AB
1 0 0
are true.

1 1 1

A B A+B=
C
OR
The result
0 0 0
is true if
C=A+B
either 0 1 1
variable is
1 0 1
true.

1 1 1

© PIHE PLC 2|Page


NOT

The state
indicator A A'
changes
A = A' 0 1
true to
false and 1 0

false to
true.

2.3 Converting logic gates

2.3.1 Converting logic gates to Boolean functions


Let's look at how we can draw the Boolean equation XY + ST in the following logic diagram.
Let’s start with input signals and develop terms for each gate until the output is reached.

1. X and Y feed AND gate 1, the output is: XY.

2. S and T feed AND gate 2, the output is: ST.

3. The input to OR gate 3 is the result of gate 1 (XY) and gate 2 (ST), giving the output
expression:

XY + ST.

© PIHE PLC 3|Page


2.3.2 Converting Boolean equations to logic gates
Let's look at the equation (A + B) (C + D) = X.

1. It states that one term (A + B) AND another term (C + D) is equal to X.

2. A + B is a binary OR gate that supplies the overall AND gate.

3. C + D is also a binary OR gate and supplies the overall AND gate.

2.4 Truth Tables

The aim of a truth table is to prove that the left-hand side (LHS) of an equation is equal to the
right-hand side (RHS), for each combination of variables. For example:

A+B=B+A

A(B+C) + B’C = AB + AC + B’C

To prove that the expressions above is in fact equal, we draw a truth table. How do we do this?
Well let’s go through it together.

Before we start, we must remember that every character in the expression is a binary variable
and it represents the number 1 or 0. For example in the expression A + B both A OR B can be
either 1 or 0.

When starting with the construction of our table we first need to look at what makes up the
columns. Below is a list of rules you can use to help you identify the column headings:

• Each variable in the expression will be a new column e.g. A + B will have two columns.
One for A and the second one B.

• Each inverted variable will be a new column e.g. A’ + B will have three columns. One for
A, one for B and one for A’. If you are wondering why we are including A even though it

© PIHE PLC 4|Page


was not in our expression, it is because we need to know the value of A before we can
determine what A is not.

• Each unique grouping of terms in brackets e.g. (A + B)C will have four columns. One for
A, one for B, one for C and one for the brackets (A + B)

• Each unique grouping of terms ANDed together e.g. A + BC will have four columns. One
for A, one for B, one for C and one for BC

• The last two columns will always be the LHS of the expression and the RHS of the
expression.

Now that we have an idea of how to identify our columns, we should look at what determines the
number of rows in our table.

The number of rows can easily be determined from the number of unique variables in our given
expression. The expression A + B has two unique variables. To determine the number of rows
we will need to consider how much numbers the variable can represent. Because we are working
in binary, the variable can only represent one of two values, being 1 or 0. We then use the
calculation 2n = c with n being the number of variables in the expression and c being the maximum
number of combinations. For example: The expression A + B + C has three unique variables. The
calculation to determine the maximum amount of combinations will be 23 = 8.

Now that we know how to determine our table headings and how to calculate all the possible
combinations, we can start constructing our truth table.

Example 1

Construct a truth table for the following expression:

A+B=B+A

First let’s start by determining the number of columns. There are two unique variables A and B.
There are no inverted variables, no brackets and no terms ANDed together. We therefore have a
total of four columns. Two for our unique variables and two indicating our LHS and RHS.

Next, let’s determine the number of rows. Since we have two unique variables, we will use the
formula 22 = 4.

Now that we have identified our columns and rows we can go ahead and construct our first truth
table.

© PIHE PLC 5|Page


LHS RHS

A B A+B B+A

1 1 1 1

1 0 1 1

0 1 1 1

0 0 0 0

We need to ensure that we always write down our conclusion which is LHS = RHS.

Should you find that your LHS does not match your RHS you made a mistake somewhere and
you will need to check each 0 and 1 for that row to identify the mistake.

Example 2

Next, we will look at drawing a Truth table for a bigger expression which will allow us to practice
identifying more columns and rows.

A(B+C) + B’C = AB + AC + B’C

Again, we will start by determining the number of columns. There are three unique variables A,
B and C. There is one inverted variable B’, one unique bracket (B+C) and four unique terms
ANDed together B’C, AB, AC and A(B + C). We therefore have a total of eleven columns. The
nine we just mentioned and the two indicating our LHS and RHS.

Now let’s determine the number of rows. Since we have three unique variables, we will use the
formula 23 = 8.

Now that we have identified our columns and rows we can go ahead and construct our second
Truth table.

© PIHE PLC 6|Page


LHS RHS

A B C B’ (B + B’C AB AC A(B+ A(B+C) + A.B + AC +


C) C) B’C B’C

1 1 1 0 1 0 1 1 1 1 1

1 1 0 0 1 0 1 0 1 1 1

1 0 1 1 1 1 0 1 1 1 1

1 0 0 1 0 0 0 0 0 0 0

0 1 1 0 1 0 0 0 0 0 0

0 1 0 0 1 0 0 0 0 0 0

0 0 1 1 1 1 0 0 0 1 1

0 0 0 1 0 0 0 0 0 0 0

Therefore: LHS = RHS

© PIHE PLC 7|Page


Processing and Logic Concepts

UNIT 3 - Boolean Algebra and De Morgan’s Theorem

3.1 Introduction to Boolean Algebra


In mathematics, Boolean algebra is the branch of algebra, that is taught in high school.
Whereas arithmetic of algebra deals with specified numbers, Boolean algebra introduces
quantities without fixed values, known as variables. In Boolean algebra, the values of the
variables are either true or false, and is denoted by 1 and 0. Instead of algebra where the values
of the variables are numbers, and the operations are addition, subtraction, division and
multiplication, the main operations of Boolean algebra are the conjunction (AND), the disjunction
(OR), and the negation (NOT). Boolean algebra has been fundamental in the development of
modern digital electronics.

3.2 Boolean algebra: laws and theorems

Although Boolean algebra has its own set of laws and theorems, it is also governed by the laws
of arithmetic algebra.

© PIHE PLC 1|Page


Order of operations
BODMAS is an acronym and it stands for Bracket, Of, Division, Multiplication, Addition and
Subtraction. It explains the order of operations to solve an expression. According to the rule of
BODMAS, if an expression contains brackets ((), {}, []) we must first solve or simplify the bracket
followed by of (powers/exponents), then division, multiplication, addition and subtraction from left
to right. Solving the problem in the wrong order will result in an incorrect answer.

Using algebra
Before you evaluate an algebraic equation, you need to simplify it. Here are the basic steps to
follow when simplifying an algebraic expression:

1. Remove parentheses by multiplying factors

2. Combine like terms by adding coefficients

3. Combine the constants

Let's work through an example:

5(2 + x) + 3(5x + 4)

When simplifying an expression, the first thing to look for is whether you can clear any
parentheses. Often, you can use the distributive property to clear parentheses, by multiplying the
factors with the terms inside the parentheses. In this expression, we can use the distributive
property to get rid of the first two sets of parentheses.

= 10 + 5x + 15x + 12

The next step in simplifying is to look for like terms and combine them. The terms 5x and 15x are
like terms, because they have the same variable. We can combine these two terms to get 20x.

= 10 + 20x + 12

Finally, we look for any constants that we can combine. Here, we have the constants 10 and 12.
We can combine them to get 22.

= 22 + 20x

© PIHE PLC 2|Page


Boolean Laws and theorems
The following laws and theorems are essential to solving Boolean algebra problems. It is
important for you to know them and be able to apply them where necessary. There are eight
groups of laws and one important theorem (De Morgan’s theorem).

Rule Examples Variable or expression (…)

Identity A+0=A (…) OR 0 is equal to itself

A.1 = A (…) AND 1 is equal to itself

A+1=1 (…) OR 1 is equal to 1

A.0 = 0 (…) AND 0 is equal to 0

Idempotence A+A=A (…) OR itself is equal to itself

A.A = A (…) AND with itself is equal to itself

Complement A + A' = 1 (…) OR its complement is equal to 1

A.A' = 0 (…) AND with its complement is equal to


0

Involution (A')' = A

Commutative A+B=B+A

A.B = B.A

Associative (A + B) + C = A + (B + C)

A.(B.C) = (A.B).C

Distributive A.(B + C) = A.B + A.C

A + B.C = (A + B).( A + C)

Absorption A + A.B = A

© PIHE PLC 3|Page


A.(A + B) = A

Simplification A + A'B' = A + B'


Theorem
A' + AB = A' + B

De Morgan's (A + B + C …)' = A'.B'.C'…


theorem

(A.B.C …)' = A' + B' + C'…

3.2.3.1 AND

The law of AND:

AND LAWS

x.0 = 0

x.1 = x

x.x = x

x.x’ = 0

xy = yx

(xy)z = x(yz)

x + yz = (x + y)(x + z)

x(x + y) = x

(xy)’ = x’ + y’

© PIHE PLC 4|Page


3.2.3.2 OR

The law of OR:

OR LAWS

x+0=x

x+1=1

x+x=x

x + x’ = 1

x+y=y+x

(x + y) + z = x + (y + z)

x (y + z) = xy + xz

x + xy = x

(x + y)’ = x’.y’

3.2.3.3 NOT

In some instances, a condition may be negated more than once. It may have a double negation,
a triple negation, or more. If there is an even number of vincula over the variable, it can be reduced
to equal the variable itself. (No vincula). If there is an odd number of vincula over the variable the
value is equal to the inverse of the variable (1 vinculum)

Above can be written as either OR Ā.

© PIHE PLC 5|Page


3.2.3.4 De Morgan's theorem

De Morgan's theorem may be thought of as breaking 'the bar'. When the bar is broken, the
operation directly underneath changes from addition (OR) to multiplication (AND), or multiplication
(AND) to addition (OR), and the remaining bar piece stays over the individual variables.

The following illustrates this concept.

In short, “Break the line, change the sign”

De Morgan's theorem may be illustrated using the following truth tables:

Using Boolean Algebra:


Simplify A'B'(A'+ B')(B'+ B)

F = A'B'(A' + B)(1)

A'A'B' + A'BB'

A'B' + A'(0)

A'B' + 0

A'B'

Expression: Rule(s) used:

F= xzy + xzy' + x'z Distributive


= xz(y + y') + x'z Uniqueness
= xz(1) + x'z Identity
= xz + x'z Distributive
= z(x + x') Uniqueness
= z(1) Identity
=z

© PIHE PLC 6|Page


Prove A + B + CD + (AB)' = 1

Expression: Rule(s) used:

A + B + CD + (AB)'
= A + B + CD + A' + B' De Morgan’s
= A + A' + B + B' + CD Commutative
= 1 + 1 + CD Uniqueness
=1

© PIHE PLC 7|Page


Processing and Logic Concepts

UNIT 4 - Decision Tables


4.1 Introduction to Decision Tables

A decision table is a tool that systems analyst can give to a programmer to help them understand
the system they need to develop better. This tool allows for a more effective form of
communication than a written or verbal explanation. Look at an example of a decision table below
and see if you can understand it before reading the explanation.

Title: Cash back rewards system

Table size: 3C, 3A, 8R, 2I

1 2 3 4 5 6 7 8

C Bill >= R500 Y Y Y Y N N N N


1

C Bill >= Y Y N N Y Y N N
2 R1000

C Rewards Y N Y N Y N Y N
3 card

© PIHE PLC 1|Page


A 2% cash X
1 back

A 5% cash X
2 back

A No rewards X X X X
3

Above is an example of a cash back rewards system that rewards a customer with a small amount
of money back based on the total of their bill. On checkout the system will check whether the
customer qualified for a reward.

• Should they have a card and their total is equal to or more than R500 but less than R1
000 the person will get 2% cash back.

• If they have a card and their total is equal to or more than R1 000 the person will get 5%
cash back.

• Should the person not have a card, or their total is less than R500 they do not qualify for
a reward.

• There are also two columns in our table (shown in red) which we say is impossible as it
indicates that the bill is both less than or equal to R500 and more than R1 000 at the same
time. If you draw your decision table on paper, you can draw a vertical line through the
column.

I hope we agree that the diagram presents the needs of the system clearer to the programmer.
The systems analyst will draw up these diagrams after an information gathering session with the
client. Your goal in this unit will be to learn how to draw and read a decision table.

Now let us break down the various elements of a decision table so we can better understand what
each part represents.

• Title (an identifying description of the system/table as a whole). In our first example, the
title of the table is 'Cash back rewards System'.

© PIHE PLC 2|Page


• Table size (the number of conditions, actions, rules and impossibilities of the table). In our
example we have three conditions, three actions, eight rules and two impossibilities.

• Conditions (factors the computer must test to make a decision). A condition statement is
a choice that our system needs to make based on a particular scenario. A possible
outcome based on the combination of the conditions is indicated with either Y (yes) or N
(no). In our decision tables we will only cover three and four conditions but know that this
number can be much higher depending on the complexity of the system you are
developing.

• Actions (actions to be taken as a result of existing conditions). An action can either take
place (represented by X) or not take place (blank square). In our first example we used
the X to indicate what type of reward (if any) the customer qualified for.

• Rules. The number of rules for our decision table is determined by the unique number of
conditions we have. Since we are working with two outcomes ‘Yes’ or ‘No’ for each
condition, we can use the same formula (2n = c) as we did when determining our number
of combinations in the Truth Table section. n represents the number of unique conditions
and c the maximum number of rules.

• Three conditions - 23 = 8 rules

• Four conditions - 24 = 16 rules

• Impossibilities. These are rules where the combination of ‘Y’ and ‘N’ do not make logical
sense or has been indicated by the client as something that the system should not allow.
These rules can be marked as impossible by either using the colour red or drawing a line
through the rule if you are drawing it out on paper. Here are a few examples:

• Numeric values can’t be higher and lower than the exact same number at the same
time e.g. you can’t be younger than 21 but also older than 35 at the same time

• If a bank indicates that anyone who is unemployed cannot qualify for a loan, then
all rules indicating that this is possible should be marked as impossible.

© PIHE PLC 3|Page


4.2 Constructing decision tables

Now that we have a better understanding of what a decision table is, it is time we learn how to
draw our own.

Example

Rainbow nation bank would like an instant loan application system that will provide loans to a
client at an ATM machine based on their profile. We have been provided with the following rules
the system must adhere to:

• Should the person not be credit worthy the system should not indicate the loan option

• If the person earns up to R10 000 they will qualify for a loan of up to R5 000

• If the person earns up to R15 000 they will qualify for a loan of up to R7 500

• If the person earns more than R20 000 the system should in addition to the first two
amounts also provide a call me option to request a consultant to call the client regarding
a loan

Let’s look at a step by step approach we can follow to construct our Decision table:

Step 1:

Collect all information needed on the problem to be solved by the table. In this case, it’s the
above information. Identify a title for your system e.g. Loan System

Step 2:

Identify all the conditions (factors the computer must test to make a decision) to be included in
the table.

Conditions for the scenario above is as follow:

(C1) Is the person credit worthy?

(C2) Does the person earn over R10 000?

(C3) Does the person earn over R15 000?

(C4) Does the person earn over R20 000?

© PIHE PLC 4|Page


Step 3:

In the third step, actions are identified. These actions are to be taken as a result of the existing
conditions.

Actions for the scenario above is as follow:

(A1) No Loan option

(A2) Get a loan of R5 000

(A3) Get a loan of R7 500

(A3) Request a call option

Step 4:

Determine the number of rules to be included in your table. The number of rules equals two to
the power of the number of conditions. Since our scenario have four conditions, we will have 24
= 16 Rules e.g.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

C1 Credit
worthy?

C2 Earn >
10000?

C3 Earn >
15000?

C4 Earn >
20000?

© PIHE PLC 5|Page


Step 5:

Fill in the Y (yes) and N (no) for your conditions. We can use the following pattern to make it
easier:

• Condition one: 8 Yes – 8 No

• Condition two: 4 Yes – 4 No (Repeat)

• Condition three: 2 Yes – 2 No (Repeat)

• Condition four: 1 Yes – 1 No (Repeat)

Next, identify any impossible columns based on the conditions. See below for example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

C1 Credit Y Y Y Y Y Y Y Y N N N N N N N N
worthy?

C2 Earn > Y Y Y Y N N N N Y Y Y Y N N N N
10000?

C3 Earn > Y Y N N Y Y N N Y Y N N Y Y N N
15000?

C4 Earn > Y N Y N Y N Y N Y N Y N Y N Y N
20000?

Rule 3, 5, 6, 7, 11, 13, 14 and 15 is impossible as the salary can’t be lower than a low amount
and higher than a high amount at the same time e.g. client earns less than 10 000 but more than
20 000 at the same time.

© PIHE PLC 6|Page


Step 6:

Action entries must be filled in. An action can either take place (represented by an X) or not take
place (blank square)

Title: Loan System

Table size: 4C, 4A, 16R, 8I

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

C1 Credit Y Y Y Y Y Y Y Y N N N N N N N N
worthy?

C2 Earn > Y Y Y Y N N N N Y Y Y Y N N N N
10000?

C3 Earn > Y Y N N Y Y N N Y Y N N Y Y N N
15000?

C4 Earn > Y N Y N Y N Y N Y N Y N Y N Y N
20000?

A1 No Loan X X X X X
option

A2 Qualify for X X X
5000

A3 Qualify for X X
7500

© PIHE PLC 7|Page


A4 Request a X
call option

Step 7:

The Table size indicator must be added. For the above scenario we used 4C, 3A, 16R, 8I (four
conditions, four actions, sixteen rules and eight impossible columns).

In conclusion

The steps in constructing a decision table are:

1. Collect the information. Identify a title for your Decision Table


2. Identify and list the conditions.
3. Identify and list the actions.
4. Determine the number of rules to be included.
5. Fill in the condition entries and impossibilities.
6. Fill in the action entries.
7. Add table size indicator.

© PIHE PLC 8|Page


Processing and Logic Concepts
UNIT 5 System Flowcharts

5.1 Introduction to System Flowcharts


A system flowchart illustrates how computer programs fit together in an information system. It is
prepared by a systems analyst and shows the flow of control through an information system,
specifying all programs, inputs and outputs.

Each program on a system flowchart is referenced by a name such as Program 1. The steps in
the computer programs and the logic required for each is not shown on the system flowchart.
These appear on the program flowchart. (See Unit 6 -Program Flowcharts)

A system flowchart is a convenient way to design the logic for systems. It is less costly to
determine, arrange and rearrange complex logic on a flowchart than to correct logic errors in an
implemented system. The flowchart becomes an integral part of the system documentation. A
system flowchart is vital if the system is to be maintained easily and efficiently or if modifications
are necessary.

© PIHE PLC 1|Page


Let’s look at the symbols that make up a system flowchart to better understand what it is.

5.2 System flowchart symbols

The information technology industry has developed an international set of standardised system
flowchart symbols and symbol definitions. These allow system flowcharts to be easily understood
to anyone working in the industry anywhere in the world. The table below shows the most common
system flowchart symbols and their function.

Symbol Example and Function

Terminator

This is how we indicate the start of all


flowcharts

Flowline The solid line indicates the flow of information


between the various symbols.

The dashed line indicates when a


symbol(device) is taking the place of the
other symbol(device) when the flow repeats.

Processing / Program This symbol is used to indicate a process or


function that is taking place. Usually
represents a software program. Examples of
this could be updating, printing copying,
backing up, sorting data etc.

Disk Storage

© PIHE PLC 2|Page


Represents storing or reading data from a
disk (hard drive)

The use of a double-sided arrow indicates


that the program is updating the disk

Document

Represents reports, invoices, or any paper


documents that may be input/output in the
information system. The example above
shows the process/program printing the salary
slip.

Output Display

Displays a message on the screen. In the


example above it display “Update complete”
to the screen after the disk was updated.

© PIHE PLC 3|Page


Manual Operation

This is a manual process that does not


involve the computer. In the example above
the results on disk is manually checked. Two
paths are created. One for the correct results
and one for the incorrect results.

Manual Input

Represent data entered manually via the


keyboard during processing. In the example
above the date is entered into the program.

Merge

Represents the combining (merging/collating)


of more than one symbols information into
one.

External/Offline storage

Represents the external and offline storage of


data.

© PIHE PLC 4|Page


Connector

Represents a connection in the flow. Used


when the steps to be joined are located on
the same page.

Now that we know what symbols we can use to draw a system flowchart, let us look at an
example of one.

A school would like to store their Grade 12 results and compare it with the results of the
previous year. The school starts by entering the current Grade 12 results into a program. This
information is then stored on a disk and a copy of the disk is made for storage. Once the copy is

© PIHE PLC 5|Page


complete a message is displayed informing the user of this. Last year's results together with this
year's results on disk is used to produce a document that compares the two years with each
other. The copy of this year's results will become the input to last year's results for the following
year.

5.3 System flowchart rules


Before we start drawing our own system flowcharts, we need to take note of the following rules.

• The flow of information will be as follows:

• We will not be writing the logic of the process/program symbol. We only need to label the
process with the action it must perform e.g. make copy, sort date etc. We do not have to
write the logic of how the copy is made or sorting takes place.

• No disk-to-disk, document-to-disk and disk-to-document is allowed. There must always be


a process/program in-between.

• A document is the product of a process. It cannot be produced directly from a storage


medium.

• Anything input at the keyboard, using the manual input symbol, is entered into a program.

• A double-sided arrow is used to show that a disk is updated.

• When making a backup or copy of a file on disk, input the file into a backup or copy
program and output a new file on disk (called Backup file or Copy file)

© PIHE PLC 6|Page


• The processing symbol is the most important, as without it the flow of the diagram will be
missing and will not make sense. All actions or steps take place here. If the processing
symbol is left out any decisions made will not be followed by an action.

5.4 Drawing System flowcharts


It is time for us to draw our first system flowchart. Once you are done you can view your answer
on the next page. It is important that you do not skip ahead as it is your opportunity to learn.

The system begins with three source documents, which are loaded onto one disk called
INVOICE DISK.

The INVOICE DISK is manually checked, and any incorrect records are processed again and
rechecked.

Once the records on the INVOICE DISK are all correct, the INVOICE DISK are merged onto
one DISK called COLLATED INVOICES. The COLLATED INVOICES file is used as input to
three programs.

PROGRAM 1 produces a report called REPORT 1.

PROGRAM 2 makes a backup of the disk, which is then stored.

PROGRAM 3 updates the INCOME DISK.

PROGRAM 4 uses the INCOME DISK to produce REPORT 2. The date is entered at the
keyboard for REPORT 2.

© PIHE PLC 7|Page


© PIHE PLC 8|Page
Processing and Logic Concepts
UNIT 6 - Program Flowcharts
6.1 Introduction to Program Flowcharts

A Program Flowchart is a tool we use to illustrate the logic of our program in an easily understood
format. Developers will use the flowchart to develop the system in a program language suitable
for developing the software. It is less costly to create a program flowchart to find a solution to a
complex logic problem than to solve it during program development. A program flowchart is vital
if the program is to be maintained easily and efficiently or if modifications are necessary.

6.2 Program flowchart symbols

Program flowcharts are constructed with a set of standardised symbols that make it easier for
persons other than the original programmer to read and maintain the program. A flowchart allows
developers who code in different programming languages and communicate in different
languages to understand the same program logic. Below are the symbols we use to construct a
program flowchart.

© PIHE PLC 1|Page


Symbol Example and function

Terminator

Indicates the beginning and end of a


program. Every program must have a start
and stop terminator.

Input / Output

Represent any input and output in the


program. The keyword input is used in most
of our examples. The keyword read is used
when the information is read from a file (EOF)

Process

Represents all calculations, initialisation,


assignments. Shows all program instruction
that process data.

Decision

© PIHE PLC 2|Page


Indicates a condition (refer to decision tables
section). Represents a point in the program
where branching based on a decision occurs.
Every decision must have a ‘Y’ and ‘N’ to
indicate the direction of the path.

Connector

Represents a connection in the flow. Used


when the steps to be joined are located on
the same page.

Off-page connector

Used instead of the connector symbol when


the flow is continued on another page.

6.3 Variables

Data manipulated by a computer program is represented by variables. A variable is really a name


or placeholder that represents a number, character, or word. The value of a specific variable is
subject to change as the computer program processes. There are several guidelines that should
be followed when choosing a name for a variable.

• A variable name should be short, simple, descriptive, and meaningful and the same name
should be used throughout the program. The variable names num, number, nums will all
represent a different variable name to the program.

• Some computer languages are case-sensitive and so the variable name should be in the
same case throughout the program. The variable Num and num will therefore be treated
as two different variables.

© PIHE PLC 3|Page


• Do not use an underscore to join two words. In fact, do not use any symbols in variable
names e.g. num<five, greaterThan50% etc will all be incorrect as the program will attempt
to perform a calculation when it finds a symbol.

• Variable names should begin with a small letter. When joining two words, the first letter of
the second word should be capitalised. For example, totStud could be used to represent
the total number of students. The standard is to be used when drawing program
flowcharts.

6.4 Calculations

The result of a calculation must always be recorded in a variable. If we do not record this value,
we are unable to use the result in another calculation or output the result to the user.

• An asterisk (*) is used to indicate multiplication.


For example: grossPay = rate * hours

• A forward slash (/) is used to indicate division.

For example: classAverage = totalMarks/totLearners

• Percentages must be converted to decimal before you can work with them.
For example, if the tax is 10% of the gross salary the calculation would be worked out as
follows:
tax = grossPay * 0.1

Please take note of the following important calculations that must be understood for the examples
in this course:

• Gross pay - This is your salary before any deductions.

• gPay = hours * rate

• Net pay – This is your salary after any deductions.

• nPay = gPay - deductions

• Percentage – Calculation showing your result for an exam result out of 100.

• percMark = (mark/maxMark) * 100

© PIHE PLC 4|Page


• Average – Calculation usually performed on two totals. Assume you wrote 6 exams and
you have your mark out of 100 for all of them. If you were to add all 6 exam results together
and divide this number by 6 you will get your average mark for all your exams.

• avgMark = totalMark / 6

• Modulus (MOD) - Returns the remainder of two numbers that have been divided.

• e.g. 8 MOD 6 = 2. The number 6 goes into 8 once. 8 - (6*1) = 2 which is our answer.

• e.g. 16 MOD 5 = 1. The number 5 goes into 16 three times. 16 - (5*3) = 1 which is
our answer.

• e.g. 10 MOD 2 = 0. The number 2 goes into 10 five times. 10 - (2*5) = 0 which is
our answer.

6.5 Simple flowcharts

Let’s have a look at our first program flowchart. Below is a flowchart for a program that will do the
following:

Input the number of kilometres on a car odometer when the car is filled with petrol, the number of
kilometres on the odometer when the car was previously filled, and the number of litres of petrol
used to fill the car's tank.

Calculate the kilometres per litre by subtracting the previous odometer reading from the current
odometer reading and then dividing by the number of litres used to fill the car.

Print the kilometres per litre.

Use the following variable names:

Current kilometres = cKms


Previous kilometres = pKms
Kilometres per litre = kpl
Litres = ltrs

© PIHE PLC 5|Page


You will not be required to show the notes on the right side of the flowchart when you draw the
flowchart.
6.7 Flowchart loops

Flowchart loops are used when a process may be performed one or more times. Used, for
example, when more than one record from a file is to be processed by the computer program. In
this case, the computer will read in the next record after processing the current record. However,
the program must be able to stop when all the records have been processed. To make this
possible, an end-of-file (EOF) check is used. Every computer input device uses a special record
called an end-of-file, or EOF record. The EOF record is the last record in a file.

When using loops in your programs, an EOF check should be used, unless specific instructions
are given, such as, process until the number zero is input. Use a decision symbol to represent an
EOF check.

Let us look at some examples of a loop.

© PIHE PLC 6|Page


6.5.1 Flowchart loop example
Below are two examples. Both process one or more numbers and print each number to the screen
before stopping. The flowchart on the left use the EOF condition to terminate the program. This
means the program will stop once the last number has been processed. As we are reading from
a file, we are using the read keyword. The flowchart on the right will stop when the user enters
the number 0 (zero). As the number is being entered by the user, we use the keyword input.

Study the following example of a program that performs a loop.

© PIHE PLC 7|Page


• The above program reads a record from a file containing the following fields: name, rate,
hours, and deductions.

• An EOF check determines whether the record is the last record in the file. If the EOF
condition is true (‘Y’), the program stops. Otherwise (‘N’), the program will continue to
process the records.

• The gross pay is calculated by multiplying the hours by the rate. The net pay is calculated
by subtracting the deductions from the gross pay. A payslip is then printed and the next
record on the file is read and processed.

© PIHE PLC 8|Page


6.6 Logical decisions

When supplying the information to be included in a program, it is vital to remember two important
facts about computers:

1. Computers can determine and act on only 'Yes' and 'No' decisions.

2. You must supply every decision the computer is to make and every resulting action for
each decision (refer to Decision Table section for more information on this)

These factors require the programmer to pay a great deal of attention to the logic of the program.

6.6.1 Determining logical decisions


The first step in determining the logical decisions to be made in a program is to identify the factors
upon which the decisions are to be based. You must then determine the actions to be taken as a
result of the decisions.

6.6.2 Illustrating logical decisions

The following special symbols are frequently used in flowcharts to indicate relationships:
> Greater than
< Less than
≥ Greater than or equal to
≤ Less than or equal to
= Equal
<> or != Not equal

Below is an example where decisions are being used to determine the message the person
receives depending on the mark entered. If the mark is less than 60 the message Fail is displayed.
If the person gets a mark from 60 to 79, they see the message Pass. Lastly, should the person
get 80 or higher the message Distinction is displayed.

© PIHE PLC 9|Page


6.7 Accumulating and incrementing totals

The word accumulate means to add together. In programming terms when we say that we are
accumulating the student's marks, we mean that we are adding all the marks of the student
together. We will show this in a process symbol like this:

The word increment means to count. In programming terms when we say that we are incrementing
the number of votes, we mean that we are counting the total number of votes made. We will show
this in a process symbol like this:

© PIHE PLC 10 | P a g e
There are three rules to remember when accumulating or incrementing totals:

1. The totals to be accumulated or incremented must be initialised to zero at the beginning


of the program before the main processing loop because this clears the memory e.g.

2. Totals that have been initialised must be accumulated or incremented within the main
processing loop e.g.

3. Totals that have been accumulated or incremented must be printed at the end of the
program after the end-of-file condition has been met.

Now let’s look at how accumulation and incrementation is shown in a program flowchart.

6.7.1 Accumulating totals - Example


Records for employees at an advertising agency are read from a file. Each record contains the
employee name, gross pay and tax. Draw a program flowchart that will print a payslip for each
of the employees. The output values will be the name, gross pay, tax and net pay. The director
wants to know the total amount that was paid out to all the employees. Net pay is the total that
will be accumulated in this example. The variable names used are:

Gross pay = gPay


Net pay = nPay
Tax = tax
Total Net Pay = totNetPay

© PIHE PLC 11 | P a g e
Using the above example, suppose the first record contained the following data:

name : John Smith


gPay : 2 000
tax : 200

The net pay is equal to the gross pay minus tax, which would give us 1 800. totNetPay has been
initialised to zero and will become 1 800 with John Smith's net pay added to it. If the net pay for
the next record is 1 200, totNetPay will become 3 000.

© PIHE PLC 12 | P a g e
The program will continue adding the net pay for each record to totNetPay. After all the records
have been processed, totNetPay will be printed.

6.7.2 Incrementing totals- Example


There are three candidates for SRC president. Votes have been entered into a file and each
record contains a number, which represents a vote for one of the three candidates.
Draw a program flowchart that will calculate the total votes for each of the three candidates. A
total of all the votes must be printed at the end of file. Variable names:

Total votes for candidate 1 = tc1


Total votes for candidate 2 = tc2
Total votes for candidate 3 = tc3
Total votes overall = total
Number = num

© PIHE PLC 13 | P a g e
6.8 Printed output

Most programs that we create as programmers produce some form of output that the user of the
program can use. We must use the print keyword followed by the value or variable that we wish
to output. Below are a few examples where output takes place.

The following output symbol will display Hello World to the screen. Note that the quotes are not
printed.

© PIHE PLC 14 | P a g e
In the next example we use two symbols to display Welcome to Pearson. In the process symbol
we assign the variable greeting a value. This will not print anything yet. In the output symbol the
value of the variable greeting is printed on the screen. This method is very handy when you wish
to assign different values to a variable depending on certain decisions and output the value once
at the end.

Please note that the keyword print does not print on a piece of paper, but rather outputs the values
on the computer screen for the user to see.

6.8.1 Printed output – Example 1


In the flowchart below we start by printing headings for our data. We then read in the number,
name, hours worked and rate of pay. If it is not the end-of-file, we calculate the gross pay. Before
we read the next record, we print out the number, name, rate of pay, hours worked and gross
pay.

© PIHE PLC 15 | P a g e
Below is a sample output of what could be displayed by the program:

Number Name Rate of pay Hours Gross pay

1 Fred Smith 10.00 40 400.00

2 Jane Blair 20.00 45 900.00

: : : : :

© PIHE PLC 16 | P a g e
6.8.2 Printed output – Example 2
In the flowchart below the name and percentage of a student is read into the program. If it is not
the end-of-file, the percentage is checked and the msg variable is assigned the value of either
Pass or Fail. The name, percentage and the value of the msg variable is printed. The next record
is then read.

© PIHE PLC 17 | P a g e
Below is a sample output of what could be displayed by the program:

name percent msg

Steven 40 Fail

Themba 75 Pass

: : :

6.9 Error checking

Earlier in the unit we learnt that computers can determine and act only on 'yes' and 'no' conditions,
and that the programmer must supply every decision the computer is to make and every resulting
action for each decision. It is extremely important that the program should be able to recover from
incorrect input from the user. This is done through the use of error messages. If error checking is
not used in a program, the program will crash when unexpected information is input.

Up until now we have not factored in that the user could provide incorrect values for our program.
Let’s look at how we can validate user input with some examples.

6.9.1 Error checking example


The program below will accept only three car types. If the car type is 0, the program will stop. If
the car type is 1 or 2, an appropriate message will be printed. If any other number is entered an
error message will be printed and the program will read the next car type.

© PIHE PLC 18 | P a g e
Below is a sample output of what could be displayed by the program if the value 1, 2, and 3 where
used as input:

carType msg Error

1 Beetle

2 BMW

Error – invalid car type

: : :

© PIHE PLC 19 | P a g e
Processing and Logic Concepts

UNIT 7 - Trace tables

7.1 Introduction to trace tables

A trace table is a useful tool in the development of program flowcharts because it is like running
a program on paper. It forces the programmer to be consistent and check the program line-by-
line or box-by-box, without taking shortcuts. A trace table serves the following functions:

• It gives the programmer a deeper understanding of how a program works.

• It allows the programmer to see the output before coding and debugging on computer.

• It serves as a teaching and learning tool for concepts like variables and values.

• It allows for the detection of logic errors and helps identify ways of correcting the problem.

7.2 Constructing a trace table

Constructing a trace table is as simple as drawing columns on a ruled page. The first column is
used for referencing the line number (for program code) or box number (for flowcharts). Next are
columns for each unique variable – each variable must have its own column. These are followed
by columns for control structures, i.e. the conditions/decisions including the one for the loop. The
last column is used for output or print statements. The order of the columns from left to right is
not important.

© PIHE PLC 1|Page


Test/Sample data is necessary to draw a trace table. Test values are placed in the relevant cell
of our table. These values are updated accordingly as we step through the program. Test data
must be planned, and it must include extreme values (upper and lower) and special values to test
all parts of the program. While it is not possible to test all values to show the robustness of a
program, all that is required to prove failure, is one test case in which the program does not work.

Example 1

Note that the numbered boxes on the flowchart represents the first column (Box no columns)
values in the trace table below.

© PIHE PLC 2|Page


Let us assume that the following values are input at box 2:

A B
8 6
15 3
20 8
12 4
7 2

Box No N K A B A mod B = 0? N = 5? Output


1 0 0
2 8 6
3 1
4 No
7 No
2 15 3
3 2
4 Yes
5 1
6 15 3
7 No
2 20 8
3 3
4 No
7 No
2 12 4
3 4
4 Yes
5 2
6 12 4
7 No
2 7 2
3 5
4 No
7 Yes
8 2

© PIHE PLC 3|Page


The first column contains the box number; the next four columns contain the variable names,
followed by the control structures and the output. The trace table keeps track of the results for
each input. You will notice that the variables N and K are incremented depending on the results
of the logical decisions.

Example 2

The first example showed us a trace table of a program flowchart that was correct. It is however
possible that we could make a mistake in our program flowchart and we will then be able to see
this mistake in our trace table. See if you can spot the error in the flowchart below.

© PIHE PLC 4|Page


Let us assume that the following values are input at box 1:

85
75
50

Box No perc EOF? perc < 60? perc < 80? Output
1 85
2 No
3 No
6 Fail
1 75
2 No
3 No
6 Fail
1 50
2 No
3 Yes
4 Yes
7 Pass
1
2 Yes

From the trace table we can see that there is an error in our program. Our program is currently
printing Fail for the results 85 and 75 but Pass for the result 50. We can fix this error by switching
the ‘Yes’ and ‘No’ around for the decision perc < 60.

© PIHE PLC 5|Page


Processing and Logic Concepts
UNIT 8 Pseudocode
8.1 Introduction to pseudocode

Pseudocode is a method of displaying the logical flow and structure of a program. If structured
correctly, the logic makes a program easier to follow and coding becomes a straightforward
process.

There are two parts to writing a program – designing and writing the code. Once the logic of the
design has been checked and corrected, the design is ready to be translated into code. Much
time can be wasted in trying to find logical errors in a program that has been coded without a
properly checked design. Just as a builder should never build a house without a properly drawn-
up plan, one should never code a program without a proper design.

We learned about program flowcharts as a means of describing the flow of logic in a program.
Although a program flowchart is a useful tool for describing what a program must do, it has
definite shortcomings:

• Program flowcharts are time-consuming to construct.


• They can become clumsy and unmanageable when describing larger or more complicated
programs.
• Flowcharts are often difficult to modify because they are drawings.
• They are more difficult to read and follow, especially for larger programs.

© PIHE PLC 1|Page


Pseudocode offers a program design alternative to flowcharts. It is non-code-specific, easily
readable, and can be translated into any high-level computer programming language.

8.2 Why use pseudocode?

Pseudocode is ideal for designing a program plan because it shows the step-by-step logic in a
format that is easy to read and translate into a specific computer language. Pseudocode can
easily be tested to ensure that program output meets the requirements. Finding and correcting
errors at this stage is far easier and less costly than after the coding has been completed.

You would use pseudocode instead of program flowcharts because it is:

• Less clumsy and less time-consuming to prepare.

• Easier to modify, because there are no drawings.

• Easier to convert to a high-level programming language.

• Easier to read and understand.

• More powerful when dealing with larger, more complicated programs.

• Easily included in the final program in the form of comments.

There are literally thousands of programming languages worldwide. Some of them, like C or C++,
are used for engineering applications or in games development, while programs such as C# and
Java are used in commercial and web development. Most of them use commands based on
English.
Programming languages are described as either low level or high level. The closer the language
is to machine code, the lower the level. Low-level languages use ones and zeroes, and
communicate directly with the computer’s processor, whereas the high-level languages are more
English-based and need to be converted to zeroes and ones that can be interpreted by the
processor. The languages mentioned in the preceding paragraph are all examples of high-level
languages.

© PIHE PLC 2|Page


8.3 Program flowcharts vs. pseudocode
For each process that can be described by a program flowchart symbol there is a corresponding
pseudocode statement. The table below shows some of the more common symbols in program
flowcharts, and their equivalent in pseudocode.

Process Program Flowchart Symbol Pseudocode

Start begin

Stop end

Initialisation total = 0

Assignment msg = “Hello World”

total = total + 1

Input statement input num

Read statement read num

© PIHE PLC 3|Page


Output statement print num

print “Hello World”

Single decision if num = 50 then

endIf

Multiple decisions if vote = 1 then

else if vote = 2 then

endIf

Loop decision begin

read num

while num <> 0

print num

read num

endWhile

end

© PIHE PLC 4|Page


8.4 Writing simple pseudocode from Program flowcharts

In this section we will look at how we can write pseudocode from a program flowchart.

Example 1

A program is required to calculate the annual cost of running a motor car. Costs include monthly
instalments, annual fuel cost and insurance. The annual insurance rate is 3% of the car's value.

Required input values are:

• Value of the motor car.

• Monthly instalment amount.

• Annual fuel cost.

The output is the annual cost of running the car.

On the left is the flowchart for the scenario given above and on the right the pseudocode.

© PIHE PLC 5|Page


Program Flowchart Pseudocode

begin

input carValue, monthlyInstallment,


annualFuelCost

annCost = monthlyInstallment * 12

insurance = carValue * 0.03

totalCost = annCost + insurance +


annualFuelCost

print "Total annual cost of running the car",


totalCost

end

Take note of the tab spacing(indentation) used. The begin and end is underneath each other but
the rest of the code is to the right. The reason for this is to make it easier for us to see where a
block of code starts and ends. We use the same spacing when working with decisions such as
the if and while statement. Some programming languages, like Python, makes use of the spacing
to show the start and end of a decision. Failure to create the spacing correctly will result in an
error in Python.

© PIHE PLC 6|Page


Example 2

A school wants to know the average exam mark for a group of students and the total number of
students that got a distinction (mark equal to or greater than 80). The input for the program will
be the name of the student, student mark and the maximum mark of the exam. The output for
each student should be the student name and percentage. The program should stop when a
student name of ZZZ is entered.

Here is the Pseudocode for the above program flowchart:

© PIHE PLC 7|Page


begin

totStud = 0

totPerc = 0

totDistinct = 0

input name

while name != “ZZZ” # this can also be written as: while name <> “ZZZ”

input mark, maxMark

percentage = (mark / maxMark) * 100

if percentage >= 80 then

totDistinct = totDistinct + 1

endIf

print name, percentage

totStud = totStud + 1

totPerc = totPerc + percentage

input name

endWhile

average = totPerc / totStud

print average, totDistinct

end

© PIHE PLC 8|Page

You might also like