CXC CSEC Problem Solving
CXC CSEC Problem Solving
CSEC INFORMATION
TECHNOLOGY
INTRODUCTION
… is a sequence of precise
instructions which results in a
solution
PROBLEM-SOLVING ON THE COMPUTER
Ques 5: A, B & C
PROBLEM 1
Print Average_Age
Analyse the following problems by dividing them into input, process and output
Introduction
Initialization of Variables
FINDING A SOLUTION TO THE PROBLEM
INTRODUCTION
Memory locations
Num 5
Variable 1 3
Num
s
2 25
Num
3
FINDING A SOLUTION TO THE PROBLEM
THE CONCEPT OF VARIABLES
Option 2 Option 1
Get num1, num2, num3 Get num1, num2, num3
Average = (num1 + num2 Sum = num1 + num2 +
num3)/3 num3
Print Average Average = sum/3
Stop Print Average
Stop
The most efficient solution should have the following
attributes:
• Should be maintainable
• Should be memory efficient
• Should be robust
INITIALIZATION OF VARIABLES
E.g. Count = 0
Algorithmic structure
Problem:
Write an algorithm that prompts a student to
enter his/her name and age, accepts the
name and age and then display a welcoming
message on the screen such as “hello
Michael! You are 16 years old!”
Print
Average
Stop
SELECTION STRUCTURES
Selection (decision)
The IF…THEN selection statement
Syntax:
IF (expression) THEN
{Statement (s)} Execute this statement if
logical expression is TRUE
No action is intended
should the mark be
less than 50
SELECTION STRUCTURES
IF … THEN … ELSE construct syntax:
e.g. FOR X = 1 to 10 DO
{Statements to be executed}
ENDFOR
REPETITION STRUCTURES
INPUT Value
WHILE (expression) Do
{statements} executed if Boolean argument
in logical expression is true
INPUT value to be tested
ENDWHILE
REPETITION STRUCTURES
WHILE Loop
In processing, the argument (condition) between
WHILE and DO is tested to see if it is true.
If the condition returns true, the statement(s)
that follow DO will be executed.
If the condition returns false, execution of the
WHILE statement is stopped. It is possible for
the loop never to be executed if the initial input
renders the condition is to be proven false.
REPETITION STRUCTURES
WHILE Loop
The while loop can iterate indefinitely, as long as
the condition in the argument expression remains
true. It is terminated only when the condition
returns false.
FOR X = 1 to 3 DO
FOR Y = 1 to 3 DO
Product = Y * X
Print Product
ENDFOR
ENDFOR
TRACE TABLES
What’s printed by the following Algorithm?
AMOUNT = 2
G=4
WHILE AMOUNT < 50 Do
G = G*2
AMOUNT = AMOUNT + G
Print AMOUNT, G
ENDWHILE
TRACE TABLES
INPUT A,B,C
A=B+C
B=A–C
C= A+B
IF A > B THEN
C=A–B
ELSE
C=B–A
ENDIF
Print A,B,C
State what is printed by the algorithm above when the following data is
input:
(i) 5, 7, 9
(ii) 9, -7, 5
TRACE TABLES
What is an Array?
Accessing the Elements in an Array
Initializing Arrays
Reading Values into an Array
Displaying Array Values
Traversing Arrays
Manipulating Arrays
TOP-DOWN DESIGN METHODOLOGY
Hierarchy Charts
Steps in Modularization
Pascal in a Nutshell
Summary
PROGRAM EXECUTION ON THE COMPUTER
Debugging
THANK YOU
For your attention