Professional Documents
Culture Documents
Problem Solving and Program Design Program Implementation: Prepared By: Miss A. Walford
Problem Solving and Program Design Program Implementation: Prepared By: Miss A. Walford
Program implementation
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
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
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.