0% found this document useful (0 votes)
2 views

Problem_Solving_class

The document outlines the process of problem solving, which includes defining the problem, proposing and analyzing solutions, determining the most efficient solution, developing an algorithm, and testing the solution. It also explains the concept of algorithms, constants, variables, data types, and provides examples of programming in Pascal. Additionally, it discusses types of errors in programming and includes sample programs demonstrating the use of arrays and input/output operations.

Uploaded by

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

Problem_Solving_class

The document outlines the process of problem solving, which includes defining the problem, proposing and analyzing solutions, determining the most efficient solution, developing an algorithm, and testing the solution. It also explains the concept of algorithms, constants, variables, data types, and provides examples of programming in Pascal. Additionally, it discusses types of errors in programming and includes sample programs demonstrating the use of arrays and input/output operations.

Uploaded by

chamm
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

Problem Solving

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).
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.
Define the Problem
Define the problem means to get a good
understanding of the problem to find out what
we are supposed to do.
Proposes and analyze solution
This is the stage where you should concentrate
on considering alternative solution to the
problem.
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.
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
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.
Example 1
Add two number and find their sum
solutio
n
Input Process Output
Num 1 Sum=num1+ sum
Num 2 num 2
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.
Program Data Constants/variable Proposed variable
name
Price per hour Constant priceperhour
Number of hours Variable numberofhours
Total cost Variable totalcost
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 sortedPossible
Data type
and shat range of operations
Examples
are allowed values
oninteger
them. Any whole 0,-27,540
number
Real Any decimal 13.5,2.1427, -
number 8.0
Character Any single D, K, @. #
value
String Multiple Frank, jack
characters
Boolean Two possible True/false ,
PROGRAM ProgramName (FileList); CONST (* Constant declarations *) TYPE (* Type declarations *) VAR (* Variable declarations *) (* Subprogram definitions
*) BEGIN (* Executable statements *) END.

Program Data Constants/ Proposed Data type


variable variable name
Price per hour Constant priceperhour real
Number of Variable numberofhours Integer
hours
Total cost Variable totalcost Integer/real
What do you Verb to use How to use it
want to do
Input
Input Input name
Read Get price
Get Read num
Output
Print Print “sum”
Display Display “sum”
Write Write ”sum”
Output Output ”sum”
Processing
Age Age = 14
Sum Sum:
num1+num2
Decision
If, then If x > 20 then
Printer x
Repetition
While While x> 5 do
For Repeat … Until x
Until >5
For x = 1 to 5 do
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.
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:
Read two numbers and find their sum. Print their sum
Algorithm Pascal
Algorithm Cal Program Cal;
Var num1,num2,sum:integer Var num1,num2,sum:integer;

Start Begin
Output(‘Please enter a number’) Writeln (‘Please enter a number’);
Read(num1) Readln(num1);

Output(‘Please enter a second Writeln(‘Please enter a second number’);


number’) Readln(num2);
Read(num2)
Sum:=num1+num2;
sum num1+num2 Writeln(‘the sum is’, sum);
Output(‘the sum is’, sum) Readln;
Stop End.
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.
Without a constant
Using a constant
program cal; program cal;
const
Var
a = 5; alpha, beta,a,b : real;
b = 385.3; begin
Var
Writeln('please enter a figure for a');
alpha, beta : real;
Readln(a);
begin
alpha := a + b; Writeln('please enter a figure for b');
beta := b / a ; Readln(b);
Writeln('the answer is', alpha , alpha := a + b;
beta); beta := b / a ;
readln;
end. Writeln('the answer is', alpha , beta);

readln;
end.
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.
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.
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.
Program Counterr;
var Write a program to read the names
name:array[1..2] of string;
of students who register for
num:array[1..2] of integer;
x, y, z: integer; Mathematics and English. Display
begin the number of students for each
y:=0; course.
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.

You might also like