You are on page 1of 25

Problem solving and program design

Program implementation

Prepared by: Miss A. Walford


Topics to be prepared for on exam
 Writing Algorithms
 Testing Algorithms
 Programming Languages
 Writing program in Pascal
 Arrays
 Programming Errors
Steps to solve a problem
 Identify and define the problem
 Analyze the problem and break it into components
 Develop an algorithm
 Test the algorithm
 Write a program in programming language
 Test and debug
Writing Algorithms

 In writing an algorithm, we first to identify the inputs,


process and outputs for the program (IPO)
 Three main ways to represent an algorithm
( narrative, pseudocode and flowcharts)
 When creating an algorithm, you need to know that
data can be constants or variables.
 Constants( values does not change) Variables (
changes values throughout program
Data types
Data type Description Examples
Integer Whole numbers, positive 23, -45, 0
and negative
Real All numbers including 15.7, -19.5, 8
fractions
Character Keys on the keyboard ‘a’, ‘z’ , ’8’, ’?’
String Characters put together ‘ hello world’
Boolean True or False TRUE or FALSE

Ques:
Write an algorithm to add three numbers and print
total if the total is greater than 50 otherwise print
“total is less than 50”
Flowcharts
START
Decision Input/
Output

Process

Ques: Draw a flowchart to add three numbers and print total


if the total is greater than 50 otherwise print “total is less
than 50”
LOOPS- Iteration
 These are useful for repeating parts of a program.
There are two types of loop statements:
 Indefinite ( Unbounded Iteration) ( when you do not
know the in advance how many times to repeat the
loop ( WHILE or REPEAT loops)
 Definite (Bounded Iteration) - when you in advance
how many times to repeat the loop (FOR Loop)
Solution
 Algorithm: add three numbers
Read num1, num2, num3
Total = num1+num2+num3
If total >50 then
Print total
Else
Print “total is less than 50”

Insert
stop
Symbol
Truth Tables
 When two values are compared using Boolean
Operators (TRUE OR FALSE), the result is either
TRUE OR FALSE. Therefore if a value A is TRUE and is
represent by 1, then when A is False, the result is 0 or
the opposite of TRUE, The following tables illustrates
the use of NOT, AND and OR operators.
Truth table for NOT
A NOT A A NOT A
TRUE FALSE 1 0
FALSE TRUE 0 1

Truth table for AND


A B A AND A B A AND
B B
TRUE TRUE TRUE 1 1 1
TRUE FALSE FALSE 1 0 0
FALSE TRUE FALSE 0 1 0
FALSE FALSE FALSE 0 0 0
Truth table for OR
A B A OR B A B A OR B
TRUE TRUE TRUE 1 1 1
TRUE FALSE TRUE 1 0 0
FALSE TRUE TRUE 0 1 0
FALSE FALSE FALSE 0 0 0

EXAMPLE: Find result of A OR (NOT B) EXAMPLE:


A B NOT B A NOT B A OR
(NOT B)
1 1 0
1 0 1
1 0 1
1 1 1
0 1 0
0 0 0
0 0 1
0 1 1
pastpaper1 pastpaper2
pastpaper3
TRACE TABLES
Complete the following trace tables
X=1 X Y Z
Y=2 1 2 3
Z=3 3 5 8
WHILE Z<45 DO
8 13 21
X= X+Y
21 34 55
Y=Y+X
Z=Z+Y
ENDWHILE
TRACE TABLES
 M= 1 N M

 FOR N= 1TO 5 DO 1 1

 M=M+2 2 3

 ENDFOR 3 5

 PRINT M,N 4 7

5 9
Programming Languages
 Programming languages are divided into two
categories: low-level and high-level.
 Generations of programming languages
- First Generation- first generation languages are low-
level languages- Machine Language or Machine
Code. E.g. Binary
- Second Generation- these are low- level languages and
are often called Assembly Language
Programming Languages ( Cont’d)
 Third Generation languages are high level languages.
For example:
- Pascal
- FORTRAN (formula translation)
- C
 Fourth Generation Language ( Similar to third
generation (SQL, C++ etc)
 Fifth Generation Language – These are built on
third and fourth generation languages. They are
sometimes called non-procedural languages
Writing a Program in Pascal
Program myexample; Example 2
Var Program profit_division;
Age: integer; Var
Cost: real; Total_profit: real;
Grade: char; Share_omar: real;
Begin Begin
Writeln (‘enter the total
Age: = 32;
profit’);
Cost:= 16.95; Readln (total_profit);
Grade:= ‘A’; Share_omar:= total_profit/2;
Writeln (‘I am’ , age, ‘years old’); Writeln (‘Omar receives $’,
Writeln (‘My book costs’, cost, ‘last Share_omar);
year’); End.
Writeln (‘I want to get a grade’, grade);
End.
Arrays
 A group of data items that are all the same type such as integers.

mark 34 25 40 38 32 39
Subscript/inde 1 2 3 4 5 6
x

 Declaring an array
Var Marks: ARRAYS[1..6] of integer;

 Initialize an array
Var I: integer;
Begin
For I := 1 to 6 do
Marks[I]:=0;
End;
ARRAYS
 Assign a value to an array
Name_of_array[index]:=value;
 Access an element in an array
Assign the value 14 to element 2 of array number_array:
number_array[2] := 14;
 Pascal statement to take the integer stored in
element 2 of number_array and assign it to the
variable mynumber:
mynumber:= number_array[2];
ARRAYS
 Print element in an array
e.g. Writeln(studentnames[3]);
 Print list of elements in an array
For counter:= 1 to 7 do
Writeln(employees[counter]);
 Adding elements in an array
my_array[1]:= 13;
my_array[2]:= 3;
My_array[3]:= my_array[1]+my_array[2];
• Or
Adding elements in an array (cont’d)
 Program sum_array;
Var
I, sum: integer;
Numbers: ARRAY[1..3] of integer;
Begin
Sum:=0;
Numbers[1]:=3;
Numbers[2]:=10;
Numbers[3]:=2;
For I := 1 to 3 do
sum:= sum+Numbers [I];
Writeln (‘sum = ‘, sum);
End.
NOTE:
After you have typed your program using an editor it is
called source code. Next, the code must be tested for
correctness. To do so, the source code must be
translated into machine language called object code so
that the CPU can carry out the program instructions.

Converting a program from source code to object code is


performed by a ‘translator’ program. Types of
translators:
ALSO NOTE:
An Interpreter – translates the source program line by
line, and if the error is detected then translation is
stopped.
 A compiler - Translates all program instructions at
one time and produces a program that can be executed
(run) on its own.
 An assembler – program that translates assembly
language into a machine code.
Programming Errors
 Syntax Errors – this occurs when a mistake is made in the
language rules or sentence structure of the programming
language
Example:
If (age 42) then print “OK” – Incorrect
If (age<42) then print “OK” – Correct ( the operator < was missing)
 Logic Errors – occurs when a programmer makes mistakes
in the sequence of the program sentences such as using
mathematical formula etc
Example:
IF (X<10) THEN writeln (“hello”); If the programmer
intended the operator in the code to be ‘>’ then the
code should be
IF (X>10) THEN writeln (“hello”);
Programming Errors

 Run-time errors – these occurs as the program


compiles or ‘runs’.

 Debugging – this is the process of finding the errors


in the source code (detection), understanding why
they occurred (diagnosis) and correcting them.
Past-Papers
 Select the link below:
See pastpapers here

ALL THE BEST IN YOUR EXAMS!

You might also like