S4- CST ICT -Elementary Programming
S4- CST ICT -Elementary Programming
Computer Programming
A program – is an organized list of statements (instructions) that when
executed, cause the computer to behave in a pre-determined manner or carry
out a defined task.
Object code
This refers to the program code that is in machine –readable i.e a source code
that has been translated into machine language.
Translators
These are programming tools that translates /convert the source program into
object code. E.g. Assemblers, compilers, interpreters etc.
Assembler
An assembler translates a program written in assembly language into machine
language.
Interpreter
This translates the source programs line-by-line, allowing the CPU to execute
one line before translating the next. The translated line is not stored in the
computer memory, hence every time the program is executed, it has to be
translated.
Compiler
This translates the entire source program into object code. The compiler
translates each high level instruction into several machine code instructions in
a process called COMPILATION and produces a complete independent program
that can be run by the computer as often as required without the original
source program being present.
1|Page
There are two major levels namely; -
i) Low level languages
ii) High- level languages.
1. Low-level languages.
- These languages are classified as low because they can be directly, or
easily understood by the computer with little effort to translate into
computer understandable form.
- These languages are hardware oriented and therefore they are not
portable. I.e. a program written for one computer cannot be installed
and used on other.
-
Types of low level languages
A . Machine language: First generation (languages)
- In this language, instructions are written using binary logic. Given that
data and instructions are in binary form, many lines of codes are
needed to accomplish even a simple task like adding two numbers i.e.
program written in this language look like this.
111000110 0000011 10000001
0001111 10001101
10001111 1111111 1000011
The program code is hard for humans to understand but it’s easily understood
by computers.
2. High-level languages.
These languages are very close to the human language (English –like) and
they can be read and understood even by people who are not experts in
programming. These languages are machine independent. This means that a
programmer concentrates on problem solving during a programming session
rather than how a machine operates.
This generation make programming an even easier task than the third
generation language because they present the programmer with more
programming tools. Examples of such tools are command buttons, forms etc.
the 4 GLs are easy to learn and understand because they are user based. The
languages syntax (grammar) is natural , near English language and use
menus to prompts to guide a non-specilist or retrieve data with ease.
Examples of 4GLs
a) Visual Basic
b) Delphi Pascal
c) Visual cobol
d) C + +
iii) Fifth generation languages ( 5 G’s)
These languages are designed around the concept of solving problems by
enabling the computer to depict human like intelligence. These programs are
designed to make the computer solve the problem programmer rather than
programmer spending a lot of time to come up with the solution.
Examples of 5GL’s
a) PROLOG
b) MERCURY
c) LISP
d) OCCAM.
iv) Object oriented programming languages. (OOP)
The concept behind OOP languages is to look at a program as having various
objects instructing to make up a whole. Each object has a specific data
3|Page
values that are unique to it (called state) and a set of the things it can
accomplish called (functions or behavior). This process of having data and
functions that operate on the data within an object is called Encapsulation.
Several objects can then be linked together to form a complete program OOP
has greatly contributed to development of Graphical user interface
operating systems and application programs.
Examples of OOP
a) Java
b) Simula
c) Small talk.
d) Python
e) C++
f) Visual basic.net
g) Sea sharp.
Program development
There are six stages of program development. They include
Problem Documentation
recognition
Problem definition
Program design
Program coding
Program testing
Program implementation
i) Program recognition
5|Page
and seeks to solve them by writing computer program that would provide the
solution.
There are two types of errors (bugs) that can be encountered when testing
i) Syntax errors – They occur as a result of improper use of language rules. e.g.
grammar mistakes , punctuation , improper naming of the variables etc.
These errors are detectable by translator and must be corrected before the
program runs.
ii) Logical errors- They are not detectable by the translator. The program rules
but gives wrong output or halts during execution.
7|Page
Meant for computer operators. E.g Technician. it helps them to install and
maintain the program.
Development of algorithms.
Algorithms – Refers to a limited number of logical steps that a program follows
in order to solve a problem.
Pseudo code – Refers to a set of statements written in a readable language
(English – like) but expressing the processing logic of program.
Guidelines for designing a good pseudo code.
1. The statements must be short, clear and readable.
2. Pseudo code lines should be clearly outlined and indented clearly.
3. It should show clearly the start and stop of executable statements and
control structures.
4. Statements must not have more than one meaning.
Solution
START
Print “Enter two numbers”
Input x,y
Sum – x+y
Average = sum /2
PRINT sum
PRINT Average
STOP
Program flowcharts.
A flowchart is a diagrammatic representation of a program in algorithms. It
uses statements and symbols that have specific meaning. The symbols are of
different standard shapes linked to show the order of processing. Each shape
contains note stating what the operation is.
Guidelines for drawing a flowchart
1. There should be only one entry and one exit point of a program algorithm.
2. Use correct symbol at each stage in the flowchart.
3. Avoid a crossd flow lines.
4. Be as neat and tidy in drawing as possible.
5. Genearal direction of flow in any flowchart is from top to bottom , left to
right.
Examples of flowchart
Draw a flowchart for a program used to prompt the user to enter two number s.
the
8|Page
program should find the sum, and average then display the output
Start
In put
XY
Sum = x+y
Average = z+y/2
Print
Sum,
Average
STOP
Types of flowchart
System flowchart
It’s a chart that depicts the systems as a whole with only subsystems or a major elements shown.
Program flowchart
This chart shows the sequence of operations as carried out by a computer program.
Advantages of flowchart.
1. Gives programmer good visual reference of what the program will do.
2. Serves as program or system documentation.
3. Its easy to trace through from the start to find the action resulting from a set of condition .
4.Allows programmer to test alternative solutions to a problem without over coding the program.
Disadvantages.
1. There are so many different ways to draw them.
2. Its difficult to remain neat and uncluttered if the logic is complex.
9|Page
1. Constructing a flowchart is time consuming.
2. They take up considerable space.
3. They are difficult to amend without redrawing
ii) Selection/decision
This structure is used to branch, depending on whether the condition returns a value of True or
False (yes or no)
For example
If < condition >
Then Action 1
Else Action 2
Endif.
General format
If < condition > Then
Statements :
Endif
10 | P a g e
IF……………………………….THEN…………ELSE
Used when there are two available options for example in a football match a player is given a
RED CARD if he does a very serious mistake otherwise he is given Yellow Card.
General Format
If < condition >THEN
Statements 1,
ELSE
Statement 2
EndIF
3. Nested IF
This is used where two or more options have to be considered to make a selection. For
example, to award grade according to the marks as follows
a) 80 marks Grade A
b) 60 marks Grade B
c) 50 marks Grade C
d) 40 marks Grade D
General format
If < conditions >Then
Statement
ELSE
If < condition >Then
Statement
ELSE
If < condition >Then
Statement
ELSE
Statement
EndIf
EndIf
End
CASE SELECTION
Its an alternative to the nested IF. This selection is preffered to the Nested if in order to reduce
the many lines of codes . case selection can only be expressed using integers and alphabetic
characters only. The Boolean expression should be CASE interger OF or CASE char OF.
11 | P a g e
General format
CASE x of
Label 1: statement 1
Label 2: statement 2
:
label n: statement n-1
ELSE
Statement n
End case
Example
CASE average OF
80…100: Grade = “A”
70-79 Grade= “B”
60-69 Grade = “C”
40-49 Grade =” E”
ELSE
Grade = “F”
End case.
iii) Iteration (coping) repetition
This is designed to execute the same block of code again and again until a certain condition is
fulfilled . Itelaration is important in situations where the same operation has to be carried out
on a set of data many times.
There are three main looping controls.
i) THE – WHILE-DO LOOP
This repetitive structure tests the condition first before executing the successful code if and
only if the Boolean expression returns a true value.
Example
To withdraw money using an ATM a customer must have a balance in his/her account.
General format
While <condition >Do
Statement
End while.
Flowchart
Bal yes
>o?
12 | P a g e
End loop
no
Example
Pseudo code segment
REPEAT
Withdraw cash
Update account
Until balance <0:
General format
REPEAT
Statement
UNTIL <condition>
Flowchart
Withdraw cash
update account
Yes
Bal
>o?
No
Exit loop
iii) The FOR LOOP
13 | P a g e
This structure is used where execution of the chosen statements has to be repeated a
predetermined number of times. For example if a program is to calculate the sum of ten numbers
provided by the user. The FOR LOOP can be used to prompt the user to enter the 10 numbers at
most ten times. Once the numbers are entered the program calculates and displays the sum.
Pseudo code
FOR count = 1 to 10 Do
Writeln “Enter a number (N)”
Readln N
Sum = sum +N
End FOR.
Display sum
The counter has to be set to a start value and sometimes to an end value.
14 | P a g e
a) Prompt the user for shares and deposits of a particular member.
b) Calculate the interest and total savings.
c) Display the interest and total savings on the screen for a particular member.
Pseudo code
Start
Input Name, share,Deposit
If share> 10000 THEN
Intrest = 0.05 x shares
ELSE
Interest = 0.03 x shares
EndIf
Total savings = Deposit Interest + shares
Print Name., Totalsaving , Interest
Stop
Flowchart
Diagram
2. Botswana has a population of 3,000,000 but thi si filling by 4% each year . The island of
Reunion has a population of 95,000 but this is increasing by 95 each year.
a) Draw a flowchart that predicts the year in which the population of reunion will be grater than
that of Botswana if the trend continues.
Solution
Pseudo code
Start
Bts: 3,000
IR: = 95,000
Year : = 0
REPEAT
Bts = Bts – (Bts * (4/100)
IR = IR + (IR (9/100))
Year = year +1
UNTIL IR > Bts
Print year
Stop
3. Write a program that will allow the input of name of student marks obtained in 5 subjects
(Math, Engllish, Computer, Biology) .
The program should calculate the total and evearge marks for each student and assign the
grades depending on the average marks obtained as follows.
80- 100 A
70- 79 B
60- 69 C
50-59 D
Below 50 –E
The program should then display each students name , total marks and average .
15 | P a g e
Pseudo code
START
REPEAT
Print “ Enter name and subject marks”
Input Name, maths,English , Kiswahil , Computer , Biology
Sum = maths,English , Kiswahil , Computer , Biology
AVG = sum/5
If (AVG > 80) AND (AVG < 100) THEN
Grade = “A”
If (AVG > 70) AND (AVG <79) THEN
Grade = “B”
If (AVG >60) AND (AVG <69 THEN
Grade = “c”
If (AVG > 50) AND (AVG <59) THEN
Grade = “D”
ELSE
Grade = “E”
Endif
Endif
Endif
Endif
Print name, sum, AVG, Grade
Until count = Number of students.
Stop
Flowchart
1. Diagram
a) write a program segment for the flowchart using a high language (7 marks)
b) What would be the output from the flowchart if the statement in the decision box is changed to
(3 marks)
2003
1 a) Distinguish between Machine and Assembly language (2 marks)
b) State the type of translator necessary for a program written in
i) High level language
ii) Assembly language
2. Briefly explain the purpose of the following types of a program documentation ( 2marks)
16 | P a g e
i) User manual
ii) Reference guide
3. State any two features of user-friendly program (2 marks)
2. Study the flowchart below and the question that follow.
a) Write a high level language program for the above flowchart ( 7 marks)
b) List the outputs of the flow chart above (5 marks)
KCSE 2004
1. Distinguish between a compiler and an interpreter ( 2 marks)
2. What is meant by the term DRY Running as used in program development (2 marks)
3. Differentiate between source program and object program (2 marks)
4. Bidii wholesaler has two categories of customers for order processing. Category “A” obtains
10% discount on all orders up to Ksh 10,000. Otherwise the discount is 20% on the entire order.
Category “B” obtains 30% discount on all orders if the debt repayment is “good” otherwise the
discount is 15% . Draw a flowchart for the order processing (15 marks)
KCSE 2005
1. Distinguish between Real, Integer and character data types a used in programming (3 marks)
Diagram
17 | P a g e
b) Object code and source code.
2. State 3 advantages of high level languages over low level language.
3. Outline the stages of program development in their respective order
2. State two advantages of modula programming.
3. distinguish between pseudo code and Algorithm.
4. explain three types of control structures used in programming
5. write a pseudo code that will inform the user of what to waer depending on the weather . if its
raining “wear rain coat” if not “wear overcoat”
6. draw a flowchart for a program to display the name of a suspect to a crime who is aged
between 20 and 35 year and between 66 and 70 inches tall.
7. draw a flowchart to compute and print the grades for an examination. The input Data is Roll.
No and marks for six subjects out of 100 . grades are allocated on the following basis
% marks
grades
75 and above A
60 and less than 75 B
Less than 60 C
PREDICTION QUESTIONS ON THE TOPIC
1. a) What is meant by structured programming ( 1 mark)
b) State 3 advantages of using modules in program development ( 2 marks)
2. Give a reason why its necessary to have a program design (1 mark)
2. Distinguish between user documentation and operator documentation (2 marks)
3. state two advantages and two disadvantages of using flowchart in program design (4 marks)
4. using a simple sketch , illustrate the
i) REPEAT……….UNTIL control structure ( 3 marks)
ii) WHILE ………..DO control structure
iii) CASE control structure
5. Give an advantage of compiling a program rather than interpreting it ( 1 mark)
18 | P a g e