PROBLEM-SOLVING
& PROGRAM DESIGN
WHAT IS PROBLEM
SOLVING?
⦿ Problem solving is the act of defining a problem;
⦿ determining the cause of the problem; identifying,
prioritizing, and selecting alternatives for a solution;
⦿ implementing a solution.
OUTLINE THE STEPS IN PROBLEM-
SOLVING;
⦿ define the problem;
⦿ propose and evaluate solutions;
⦿ determine the most efficient solution;
⦿ develop the algorithm;
⦿ test and validate the solution.
DEFINE THE PROBLEM
⦿ In defining a problem, the problem must be looked
at carefully and if not phrased properly, you can
modify it to ensure that it is clearly stated
⦿ Once a problem is well understood it can be solved
easily
PROPOSE AND EVALUATE
SOLUTIONS
⦿ Allproblem statements have more than one solutions
⦿ Therefore all solutions must be evaluated to see
which is the most appropriate one for you and
choose it
DETERMINE THE MOST
EFFICIENT SOLUTION
⦿ Once all solutions are evaluated, you would now see
which solution would best suit your
business/organization
⦿ The most efficient solution would then be developed
into an algorithm
DEVELOP THE ALGORITHM
⦿ An algorithm
◼ Simple manageable steps
◼ is a finite number of accurate, unambiguous steps that solve a
problem
⦿ There are THREE types of algorithms:
◼ Narrative
◼ Pseudocode
◼ Flowchart
CHARACTERISTICS OF AN
ALGORITHM
⦿ The number of steps must be finite
⦿ The steps must be precise
⦿ The steps must be unambiguous
⦿ The steps must have a flow of control from one
process to another
⦿ The steps must terminate
⦿ The steps must lead to an output
TEST AND VALIDATE THE
SOLUTION
⦿ The algorithm developed can be checked using values
to ensure that it produces the required results
⦿ This process is called desk checking
◼ It allows users to solve errors before the algorithm is
converted into computer instructions
DECOMPOSE LARGE
EVERYDAY PROBLEMS INTO
SMALLER TASKS
⦿ In our life we are faced with many problems
⦿ Every problem requires a solution to resolve it
⦿ Finding a correct solution to a problem can be
considered as problem solving
⦿ Problem solving involves
◼ Identifying, analyzing and resolving problems using logic,
reasoning and analytical skills
DECOMPOSE LARGE
EVERYDAY PROBLEMS INTO
SMALLER TASKS;
⦿ Not all real-life problems can be solved by computers
⦿ Problems that computers can help with include:
◼ Mathematical problems
◼ Research
◼ Information storage and transformation
◼ Computer problems
DEFINE A PROBLEM BY DECOMPOSING
IT INTO ITS SIGNIFICANT
COMPONENTS
⦿ A defining diagram (IPO Chart) may be used to delineate
the components. This is not a chart but a table with three
columns.
⦿ Each column represents three components:
◼ input, process, and output.
⦿ Input is the information you need to solve the problem
⦿ Processing are the steps needed to convert the input data
to the desired outputs
⦿ Outputs are the goal of the problem solution
IPO CHART
INPUT PROCESSING OUTPUT
EXAMPLE OF IPO
EXAMPLE (IDENTIFYING IPO – CSEC 2018)
DISTINGUISH BETWEEN
VARIABLES AND CONSTANTS
⦿ Variables
◼ as an area of storage whose value can change during
processing;
◼ used to hold values that can change.
◼ can be of any data type. Variable name is the identifier of
the variable.
DECLARING VARIABLES
⦿ All variables used in a program must be declared
before it can be used.
⦿ In declaring a variable, it must be assigned a
meaningful name and indicate its data type.
⦿ To declare a variable in Pascal, the VAR statement
must be used. For example:
VAR
N1, N2, sum: integer;
VARIABLE NAME
Rules to follow when choosing a variable name:
⦿ Choose a meaningful variable name so that the name
can suggest what value will be stored in the variable
⦿ The Pascal language is case insensitive, meaning that
an uppercase letter is the same as a lowercase letter
‘B’ is the same as ‘b’.
VARIABLE NAME
⦿ Pascalreserves words that already have meanings and
cannot be used as a variable name. Such words are
begin, end, var, const, integer, real and char.
⦿ Spaces cannot be used in a variable name. For
example date of birth is not allowed but
date_of_birth or dateofbirth is allowed
VARIABLE NAME
⦿A variable name must start with a letter or with an
underscore (_). After the first character it may
consist of any combination of letters, digits or
underscore _num or num1;
⦿ A variable name cannot exceed 31 characters.
ASSIGNING AN INITIAL VALUE TO
A VARIABLE
⦿ Itmay be necessary to initialise a variable at the start
of a program, that is, give it an initial value.
⦿ For example, if you have a variable called sum, you
may want to ensure that the value in sum at the start
of the program is zero.
ASSIGNING AN INITIAL VALUE
TO A VARIABLE
⦿ In pseudocode we write an assignment like this:
sum /= 0
⦿ In Pascal we use the assignment statement: sum:= 0;
⦿ You should note that the variable sum must be
declared and the symbol for assignment is ‘:=’/=.
DISTINGUISH BETWEEN
VARIABLES AND CONSTANTS
⦿ Constants
◼ value that is given a name;
◼ the value of never change;
◼ can be used anywhere in a program and will always have the
same value.
DECLARING A CONSTANT
⦿ In Pascal, a constant is declared by using the
statement const, and then indicating the name of the
constant and its fixed value.
const const
Hr = 3600; pi = 3.142;
DATA TYPES
⦿ Data types determines the type of data that a variable
can store
⦿ Types of data types are:
◼ Integers – negative and positive whole numbers (23, -76)
◼ Floating point/real - decimal or fractional values (0.345, ½)
◼ Character (char) – a single letter or symbol (L, a, %, *)
◼ String – combination of characters (fruit)
◼ Boolean – has two possible values ( True or false)
ACTIVITY
State the data types for the following:
⦿ Name
⦿ Age
⦿ Yes/No
⦿ Taxi fare
⦿ M/F
ACTIVITY
State whether the following in an appropriate variable
name
⦿ 3num
⦿ First_name
⦿ Last name
⦿ Char
⦿ 7894
⦿ monthoftheyear
ACTIVITY
Declare the following variables:
⦿ Name
⦿ Age
⦿ Yes/No
⦿ Taxi fare
⦿ M/F
ACTIVITY
Declare the following constants
⦿ Days of the week
⦿ Months of the year
⦿ Pi
⦿ Hours in a day
⦿ Minutes in a hour
TYPES OF ALGORITHMS
⦿ Narrative
◼ This is a representative of an algorithm where each
instruction is written in everyday language
⦿ Pseudocode
◼ This is using instructions with words and symbols that closely
resemble computer programming language
⦿ Flowchart
◼ This is a graphical representation of an algorithm that uses
symbols to depict input, process and output
PSEUDO CODE
⦿A pseudo code can contain
◼ Variables
◼ Constants
◼ Operators
◼ Terminology
OPERATORS - ARITHMETIC
ARITHMETIC OPERATOR OPERATION
+ Addition
- Subtraction
* Multiplication
/ Division
MOD Produces the remainder in the
result (11 MOD 2) = 1
DIV Integer division – produces only
the whole number in the result
(11 DIV 2) = 5
OPERATORS - RELATIONAL
RELATIONAL OPERATOR OPERATION
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
= Equal to
OPERATORS - LOGICAL
LOGICAL OPERATOR OPERATION
AND And
OR Or
NOT Not
TRUTH TABLE - NOT
⦿ Truth tables are tables illustrating the use of the
logical operators NOT, AND and OR
CONDITION NOT CONDITION NOT
TRUE FALSE 1 0
FALSE TRUE 0 1
TRUTH TABLE - AND
CONDITION 1 CONDITION 2 RESULT RESULT
TRUE TRUE TRUE 1
TRUE FALSE FALSE 0
FALSE TRUE FALSE 0
FALSE FALSE FALSE 0
⦿ If both conditions are TRUE then the result must be
TRUE
⦿ If one condition is FALSE then the result must be
FALSE
TRUTH TABLE - OR
CONDITION 1 CONDITION 2 RESULT RESULT
TRUE TRUE TRUE 1
TRUE FALSE TRUE 1
FALSE TRUE TRUE 1
FALSE FALSE FALSE 0
⦿ If ONE of the conditions is TRUE then the result must
be TRUE
⦿ If ALL the conditions are FALSE then the result must
be FALSE
PSEUDO CODE
STEPS
INPUT
TERMINOLOGY
TERMS
INPUT, READ
OUTPUT OUTPUT, DISPLAY, WRITE
ASSIGNMENT SET, STORE
SELECTION IF-THEN-ELSE
ITERATION – LOOPING (BOUNDED) FOR - ENDFOR
ITERATION – LOOPING WHILE- ENDWHILE
(UNBOUNDED) REPEAT UNTIL
TEST ALGORITHMS FOR
CORRECTNESS
⦿ The testing can be done using:
◼ Desk checks/dry run construction
◼ Use of trace tables to verify results.
⦿ Trace tables consist of variable names (identifiers) as
column headings and values in the cells, one row for
each pass.
ACTIVITIES
⦿ SAMPLE QUESTIONS FROM CSEC PAST
PAPERS