2. What is Problem Solving
This is the process that requires a critical
analysis of the situation(the problem)
and careful consideration of possible ways of
over coming the problem (solving).
3. Steps for problem solving
⚫Define the problem
⚫Propose and analyze solution
⚫Determine the most efficient solution
⚫Develop and represent the algorithm
⚫Test and validate the solution.
4. Define the Problem
Define the problem means to get a good
understanding of the problem to find out what
we are supposed to do.
5. Proposes and analyze solution
This is the stage where you should concentrate
on considering alternative solution to the
problem.
6. Determine the most efficient solution
In this stage you consider which solution is the
best.
Both solution can solve the problem. However use
the
one which is most efficient.
7. Develop and Algorithm
Once the solution is selected. It is time to put it in
action. It is a systematic procedure to show you how to
implement the solution (algorithm).
What is an Algorithm
An algorithm is a set of instructions that if when
followed in a sequence will lead to a solution for the
problem.
Characteristics of Algorithm
1. Must be precise 4. Must pass the flow of
control
2. Must be unambiguous from one process to
another.
3. Eventually terminate
8. Test and Validate the solution
This is the final step. This is where you see if the
problem works in other word if it is solve. One
way of test is by using a flow chart.
11. Constants and Variables
⚫Program data are like ingredients in a recipe,
must be stored in a suitable container.
⚫Constants is a data item with a name and value
that remains the same during the execution of a
program .
Eg. Pie = 3.14
⚫ Variable is a named storage area whose contents
can be changed.
12. Program Data Constants/variable Proposed variable
name
Price per hour Constant priceperhour
Number of hours Variable numberofhours
Total cost Variable totalcost
13. Data types
A data type is a classification or category of
various data
that states the possible values that can be taken,
how
they are sorted and shat range of operations are
allowed
on them.
Data type Possible values Examples
integer Any whole
number
0,-27,540
Real Any decimal
number
13.5,2.1427, -8.0
Character Any single value D, K, @. #
String Multiple
characters
Frank, jack
Boolean Two possible True/false ,
14. Program Data Constants/
variable
Proposed
variable name
Data type
Price per hour Constant priceperhour real
Number of hours Variable numberofhours Integer
Total cost Variable totalcost Integer/real
PROGRAM ProgramName (FileList); CONST (* Constant declarations *) TYPE (* Type declarations *) VAR (* Variable declarations *) (* Subprogram definitions
*) BEGIN (* Executable statements *) END.
15. What do you
want to do
Verb to use How to use it
Input
Input
Read
Get
Input name
Get price
Read num
Output
Print
Display
Write
Output
Print “sum”
Display “sum”
Write ”sum”
Output ”sum”
Processing
Age
Sum
Age = 14
Sum:
num1+num2
Decision
If, then If x > 20 then
Printer x
Repetition
While While x> 5 do
16. The basic structure of a Pascal
program is:
PROGRAM ProgramName (FileList);
CONST (* Constant declarations *) (not mandatory)
VAR (* Variable declarations *) (* Subprogram definitions *)
BEGIN (* Executable statements *)
END.
17. Read/Readln
⚫The READ and READLN instructions read a
variable from the keyboard. The command will
continue to read until ENTER is pressed (with an
exception). In contrast
with READ, READLN reads the variable and,
when the user pressed ENTER, it moves the
cursor on the next line.
⚫These instructions can read one variable or more
variables. For example:
18. Read two numbers and find their sum. Print their sum
Algorithm
Algorithm Cal
Var num1,num2,sum:integer
Start
Output(‘Please enter a number’)
Read(num1)
Output(‘Please enter a second
number’)
Read(num2)
sum num1+num2
Output(‘the sum is’, sum)
Stop
Pascal
Program Cal;
Var num1,num2,sum:integer;
Begin
Writeln (‘Please enter a number’);
Readln(num1);
Writeln(‘Please enter a second number’);
Readln(num2);
Sum:=num1+num2;
Writeln(‘the sum is’, sum);
Readln;
End.
19. Types of errors
⚫Syntax – are errors in the way we use the
programming language.
⚫Logic – are mistake in the design of the program
such as a branch to a wrong statement.
⚫Run Time –is a program flaw detected during
program execution which may cause the
program to terminate abnormally.
20. Using a constant
program cal;
const
a = 5;
b = 385.3;
Var
alpha, beta : real;
begin
alpha := a + b;
beta := b / a ;
Writeln('the answer is', alpha ,
beta);
readln;
end.
Without a constant
program cal;
Var
alpha, beta,a,b : real;
begin
Writeln('please enter a figure for a');
Readln(a);
Writeln('please enter a figure for b');
Readln(b);
alpha := a + b;
beta := b / a ;
Writeln('the answer is', alpha , beta);
readln;
end.
21. Terms to know
⚫ Low level languages – Generation 1 and 2
⚫ High level languages – Generation 3 to 5
⚫ Source code – This is a sequence of statement in a programming
language.
⚫ Translator – is a system program that converts a program
written in the source code to machine code.
⚫ Compiler- is a system program that processes all the line of code
in an entire program.
⚫ Object code – This contains the machine instruction for
executable program and is used to create the finish executable
program in another process called linking.
⚫Interpreter – is a translator of a high level
programming language that translate and run
the program at the same time.
22. Write a an array to read ten numbers and calculate their sum. Print sum.
Program summm;
var
num:array[1..2] of integer;
sum, x: integer;
begin
Writeln('Let us write our first array');
for x:= 1 to 2 do begin
Writeln('Plese enter a number');
Readln(num[x]);
sum:= sum + num[x];
end;
Writeln('The sum is',' ',sum);
Writeln('Congrats for writings your first array');
end.
23. Write an array to read 10 numbers and print
the numbers
Program summm;
var
num:array[1..2] of integer;
x: integer;
Begin
for x:= 1 to 2 do begin
Writeln('Plese enter a number');
Readln(num[x]);
end;
Writeln;
Writeln;
for x:= 1 to 2 do begin
Writeln('The numbers are',' ',num[x]);
end;
end.
24. Program Counterr;
var
name:array[1..2] of string;
num:array[1..2] of integer;
x, y, z: integer;
begin
y:=0;
z:=0;
for x:= 1 to 2 do begin
Writeln('Cources offered are:');
Writeln ('1 Mathematics');
Writeln ('2 English');
Writeln('Please press 1 for Mathematics and 2 for English');
Readln(num[x]);
Writeln('Plese enter you name');
Readln(name[x]);
if num[x]= 1 then
y:=y+1;
if num[x] = 2 then
z:=z+1;
end;
Writeln('Total for Mathematics is',' ', y);
Writeln('Total for English is',' ', z);
end.
Write a program to read the names
of students who register for
Mathematics and English. Display
the number of students for each
course.