0% found this document useful (0 votes)
5 views64 pages

Problem Solving & Programming Tutorial

This document provides an accelerated review of problem-solving and programming concepts for CSEC Information Technology. It outlines the steps in problem-solving, the divide and conquer approach, and the use of IPO charts to decompose problems. Additionally, it covers variables, constants, data types, algorithms, control structures, operators, truth tables, and methods for testing algorithms for correctness.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views64 pages

Problem Solving & Programming Tutorial

This document provides an accelerated review of problem-solving and programming concepts for CSEC Information Technology. It outlines the steps in problem-solving, the divide and conquer approach, and the use of IPO charts to decompose problems. Additionally, it covers variables, constants, data types, algorithms, control structures, operators, truth tables, and methods for testing algorithms for correctness.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

CSEC

Information Technology
For CXC
Problem Solving & Programming
OCtober 2021
K. Johnson
Believe in Yourself...
Problem Solving
& Programming
Problem Solving & Programming
Accelerated Review/Revision of Concepts

This is an accelerated lesson to quickly review the concepts


covered in Grade 10, so that students may easily remember,
understand, clarify and relate the concepts and knowledge gained
as prerequisites to tackle and solve the required related
Problem Solving and Programming SBA component.
Problem-Solving and Programming
What is a Problem?

● A perceived gap between an existing state and a desired one.


What is Problem Solving?

● The process of working through details of a problem to reach a solution.


It may include mathematical or systematic operations.
Problem Solving with Computing

● Include the use of programs containing computer instructions that


directs the computer to perform actions in order to arrive at a solution.
1

The Steps
in
Problem Solving
Problem-Solving and Programming
Steps in Problem Solving

1. Define/Identify the problem


2. Propose and evaluate solutions
3. Determine the most efficient
solution
4. Develop the algorithm/steps
5. Test and validate the solution
2

The
Divide and Conquer
Approach
Problem-Solving and Programming
The Divide & Conquer Approach

This approach may be used to break down large everyday


problems into simpler more manageable tasks that may solve the
ultimate problem. Consider finding the Average of 3 Numbers.
4
3 Result
2
Divide the Sum by Average of
1 the Amount of #:
List & Sum each #:

Know the amount Sum = num1 + num2 Average = Sum / 3


3 Numbers
of #s: ? 3 + num3
Problem-Solving and Programming
The Divide & Conquer
Approach

View this animation on how the


a larger task was broken down
into smaller tasks to quickly
solve a major problem that
would have probably taken
ages to solve.
3

The Main or
Significant Components
of a Problem
Problem-Solving and Programming
Decomposing Problems into their Significant Components.

The IPO chart or defining diagram is a table that is used to list


the Input, Process and Output components of any problem.

Input Process Output


What is required, given The operations, The expected results
or accepted from the calculations or tasks that is printed,
user to solve the that must be displayed or relayed to
problem. performed the user.
Problem-Solving and Programming
Decomposing Problems into their Significant Components.

Write the IPO to find the average of 3 numbers and output


their result.

Input Process Output


3 numbers READ num1, num2, Average
(num1, num2, num3) num3
Sum = num1 + num2 +
num3
Average = Sum / 3
PRINT Average
Problem-Solving and Programming
Decomposing Problems into their Significant Components.

Consider the scenario to find the average of when numbers


5, 10, 15 are entered.

Input Process Output


3 numbers READ num1, num2, Average
(num1, num2, num3) num3
Sum = num1
30 + num2 +
5 10 15 num3 10
Average = Sum /103
PRINT Average
Problem-Solving and Programming
Decomposing Problems into their Significant Components.

Write the IPO to the perimeter of a square plot of land, if you


are given the length of one of the sides.

Input Process Output


Length READ Length Perimeter

Perimeter = 4 x Length

PRINT Perimeter
Problem-Solving and Programming
IPO Challenge.

Write the IPO to find GCT if the GCT Rate and the Price is
given then find and print the GCT Amount and Total Price

Input Process Output


Problem-Solving and Programming
IPO Challenge.

Write the IPO to find GCT if the GCT Rate and the Price is
given then find and print the GCT Amount and Total Price

Input Process Output


GCTRate READ GCTRate, Price GCTAmount
Price GCTAmount = GCTRAte * Price TotalPrice
TotalPrice = GCTAmount +
Price
PRINT GCTAmount, TotalPrice
4

Variables, Constants
and
Data Types
Variables for Storing Data
What is a Variable?

● It is a portion of the computer’s memory that is identified by a name and


its contents may change during execution of the program instructions.
● Must not contain spaces. - first name, date of birth, gct Amount
● Must not begin with a number or special characters - 7-Eleven, !even,
#pay, @dob, $amount
● Should be simple and self descriptive
Good examples of variables - firstname, first_name, date_of_birth, dob
lastName, num1, total, count, gctAmount
Constants Stored in Problem Solving
What is a Constant?

● It is a part of the computer’s memory that is identified by a name but


unlike variables, its contents will not change during program execution.
● Are sometimes called literals
● A name or word that is associated with a value
● Examples include: gctRate = 0.15 discountAmount = 2/100
Sunday = 1 days_in_a_Week = 7
Data Types used in Problem Solving
What is a Data types?

● keywords that specify or describe the type of data the variable can store.

DATA TYPES EXAMPLES

Integers -600 0 1 25 999 336500

Real -207.5 1.4 0.05 15.1723 999.99

Char A b % $ &

String Excelsior School, address, Phone #

Boolean True/False or Yes/No or 1/0


5

The Concept
of
Algorithms
Algorithm and Its Attributes
What is an Algorithm?

● A finite set of step by step instructions to solve a problem.


Attributes of Algorithms include:

● They must be precise.


● Unambiguous - clearly provide what needs to be done, without a doubt.
● Must be finite, terminate or end.
● Instructions must flow into the next or follow a logical sequence or order.
How are Algorithms expressed?

1). As a Pseudocode 2). Using a Flowchart


6

Represent Algorithms
using
Pseudocodes & Flowcharts
Pseudocode Algorithms
What is a Pseudocode?

● Is an algorithm that consist of English like statements and mathematical


notations that is not specific to any programming language
Keywords used in Pseudocodes:

Terminators Data Entry Information Output


START / STOP INPUT OUTPUT
BEGIN / END ENTER PRINT
GET WRITE
READ DISPLAY
Program Instructions as Pseudocode
The following program prompts the user for and accept two
whole numbers, then calculate and output the sum.
Pseudocode

START
DECLARE num1, num2, sum As Integers
WRITE ‘Enter two numbers’
READ num1
READ num2
sum = num1 + num2
WRITE sum
STOP.
Flowchart Algorithms
What is a Flowchart?

● A graphical form of expressing an algorithm with the use of shapes and


arrows to show the flow of operations, from one stage to the next.
Flowchart Shapes / Symbols
Flowchart Algorithms
Flowchart Shapes / Symbols
Program Instructions as Flowchart
The following program prompts the user for and accept two
whole numbers, then calculate and output the sum.
Flowchart START

WRITE ‘Enter two numbers’

READ num1, num2

sum = num1 + num2

WRITE sum

STOP
Side by Side view of Algorithms
Which Control Structure is this program using to control the
This program prompts the user for and accept two whole
flow of data as the instructions are processed and executed?
numbers, then calculate and output their sum.
Flowchart START
Pseudocode

START WRITE ‘Enter two numbers’


DECLARE num1, num2, sum As
Integers READ num1, num2
WRITE ‘Enter two numbers’
Selection
READ num1, num2
Sequential Iteration
sum = num1 + num2

sum = num1 + num2


WRITE sum
WRITE sum
STOP.
STOP
Control Structure
In What order are program instructions processed?
Instructions are sometimes executed in different order based on the results
required or conditions determined to control the processing of the algorithm.
● Sequentially - in the order written, from start to finish.
● Selection/Conditionally - using statements such as IF Then,
IF Then Else
● Repetitively/Iteratively/Looping - carrying out instructions over and
over for a set time or an undetermined amount of time with statements
such as FOR, WHILE DO, REPEAT UNTIL
Sequentially ordered Control
This program prompts the user for and accept two whole
numbers, then calculateStructure
and output their sum.
Flowchart START
Pseudocode

START WRITE ‘Enter two numbers’


DECLARE num1, num2, sum As
Integers READ num1, num2
WRITE ‘Enter two numbers’
sum = num1 + num2
READ num1, num2
sum = num1 + num2
WRITE sum
WRITE sum
STOP.
STOP
IF Then Conditional Structure
This program prompts the user for Flowchart START
their age, then prints ‘Adult Fare’ if
the age is more than 18 years. PRINT ‘Enter your age’

Pseudocode
READ age

START
DECLARE age As Integer T IF age > 18
F
PRINT ‘Enter your age’
READ age PRINT ‘Adult Fare’
IF age > 18 THEN
PRINT ‘Adult Fare’
ENDIF
STOP
STOP.
IF Then Else Flowchart
This program prompts the user for
Structure
their age, then prints ‘Adult Fare’ if
START

the age is more than 18 years or ‘Child


PRINT ‘Enter your age’
Fare’ otherwise.
Pseudocode READ age
START
DECLARE age As Integer
PRINT ‘Enter your age’ T F
IF age > 18
READ age
IF age > 18 THEN
PRINT ‘Adult Fare’ PRINT ‘Adult Fare’ PRINT ‘Child Fare’
ELSE
PRINT ‘Child Fare’
ENDIF
STOP. STOP
FOR Loop Structure Flowchart

START
This program uses a definite loop to
count and print days 1 through to 7.
day = 1

Pseudocode
F
START day <= 7

DECLARE day As Integer day = day + 1 T


FOR day = 1 to 7 DO PRINT day

PRINT day
ENDFOR
STOP. STOP
Flowchart
WHiLE Loop
This program uses an indefinite loop to START
Structure
prompt for the lucky letter. It will
repeatedly display ‘Try Again’ until the DISPLAY ‘Enter Lucky Letter’
user enters ‘S’ then it will display
‘Winner!’ GET guess
Pseudocode
START DISPLAY ‘Try Again’
DECLARE guess As Char F
guess <> ‘S’
DISPLAY ‘Enter Lucky Letter’
GET guess DISPLAY
T
WHILE guess <> ‘S’ DO ‘Winner!’
DISPLAY ‘Try Again’
GET guess
ENDWHILE
STOP. STOP
6

Operators
Operators
Mathematical/Arithmetic Operators

● Used to perform calculations.

Operator Function
+ Addition

- Subtraction

/ Division

* Multiplication

MOD Modulus/ Remainder


Operators
Relational Operators

● Used to perform comparison with data on either side of the operator.

Operator Function Operator Function

< Less Than <= Less Than or Equal To

> Greater Than >= Greater Than or Equal To

= Equal To <> Not Equal To


Operators
Logical Operators

● Used to test the truth value of data and perform compound operations.

Operator Function

AND Combines data to evaluate it and gives a True if the all


the data being evaluated is true. Eg. T AND T = T

OR Gives a True if only if any one of the data being


evaluated is True. Eg. T OR F = T

NOT Gives the opposite Truth value of the data being


evaluated. IF A = True then NOT A = False and vice
versa.
6

Truth Tables
&
Logic Gates
Truth Tables
Truth Table

● Shows the resulting truth value of a complex statement as well as all the
possible truth values for its smaller parts.
● Therefore it shows whether the output is True (1) or False (0)
● It derives this result from the combined truth values of the inputs,
whether there are also True (1) or False (0)
Truth Tables
The AND Truth Table

AND TRUTH TABLE NB: AND output can only be True when all inputs are True
Inputs Output
The output Y may therefore be expressed as:
A B Y Y = A AND B
Y = AB
F F F Y = A.B
AND GATE/CIRCUIT
F T F

T F F Y
T T T
Truth Tables
The OR Truth Table

OR TRUTH TABLE
NB: OR output is True if any of the input is True
Inputs Output
The output Y may therefore be expressed as:
A B Y
Y = A OR B
F F F Y = A+B

F T T OR GATE/CIRCUIT
T F T
Y
T T T
Truth Tables
The NOT Truth Table

NOT TRUTH
NB: NOT outputs the inverse of its input, and input of F
TABLE becomes and output of T and vice versa.
Inputs Output
The output Y may therefore be expressed as:
A Y Y = NOT A
Y = A’
F T
NOT GATE/CIRCUIT
T F
Basic Logic Gates & Their Truth
Tables
7

Test Algorithms
for
Correctness
Problem Solving & Programming
Testing and Verifying Programs & Results

We may test the instructions of a problem and its results by using:-

● Desk checks/dry runs in the form of Trace Tables

● A Trace Table consists of variable names (identifiers) as

column headings and values in the cells, one row for each pass.
Checking Algorithms for
Look at the following Algorithm examples finds the sum of
Correctness
numbers entered for which we will check its correctness.
Flowchart START
Pseudocode

START WRITE ‘Enter two numbers’


DECLARE num1, num2, sum As
Integers READ num1, num2
WRITE ‘Enter two numbers’
sum = num1 + num2
READ num1, num2
sum = num1 + num2
WRITE sum
WRITE sum
STOP.
STOP
Trace Tables
What is a Trace Table?

● It a tool that is used to check


if the logic of an algorithm or
program is operating as it
should.
● It shows the change of
variables as the instructions
are executed.
● This procedure is often called
Desk Check / Dry Run
Trace Tables
Using the Trace Table to perform a Desk Check/Dry Run

START

WRITE ‘Enter two numbers’ Line/Instruction num1 num2 sum Output

READ num1 5
READ num1, num2
READ num2 5 10
sum = num1 + num2
sum = num1 + num2 5 10 15

WRITE sum WRITE sum 5 10 15 15

STOP
Trace Tables Explained
Desk Check/Dry Run of Algorithms & Flowcharts
8

Program Implementation
using the
Pascal Language
Pascal Programming
Pascal

● Is a 3rd generation programming language that lets you code in


statements very close to human language. Basic Pascal Program
includes:
PROGRAM programName;
Constant Specification & Variable Declarations;
BEGIN
Assignment of data to Variables and other Program Instructions.
END.
Sample Pascal Program #1
This program prompts the user for and accept two whole
numbers, then calculate and output their sum.
Pseudocode Pascal Program

START PROGRAM addTwoNumbers;


DECLARE num1, num2, sum As VAR num1, num2, sum : integer;
Integers BEGIN
WRITE ‘Enter two numbers’ WRITELN (‘Enter two numbers’);
READ num1, num2 READLN (num1, num2);
sum = num1 + num2 sum := num1 + num2;
WRITE sum WRITELN (sum);
STOP END.
Sample Pascal Program #2
This program prompts the user for their age, then prints ‘Adult Fare’
if the age is more than 18 years.

Pseudocode Pascal Program

START PROGRAM fareType;


DECLARE age As Integer VAR age : integer;
BEGIN
PRINT ‘Enter your age’
WRITELN (‘Enter your age’);
READ age READLN (age);
IF age > 18 THEN IF (age > 18) THEN
PRINT ‘Adult Fare’ BEGIN
ENDIF WRITELN (‘Adult Fare’);
STOP. END;
END.
Past Paper
Questions & Solutions
Past Paper Question #1
Past Paper Answer#1
START
Declare number, sum, count as Integer
Count = 0, Sum = 0
PRINT “Please enter positive integers one at a time and –1 to for no further input.”
READ Number
WHILE (number < > -1) DO
Sum = Sum + Number
Count = Count + 1
READ Number
ENDWHILE
PRINT “The Sum = ”, Sum
PRINT “The Average = ”, Sum / Count
STOP
Past Paper Question #2

Study the following


algorithm then state what
the program is trying to do
and how it will achieve it.
Past Paper Answer#2
It allows the user to enter 10 numbers, then it will determine and display the
smallest of the numbers entered.

It does this by using a loop to repeatedly check each number entered to see if it
is smaller that the number stored in the variable selected. If so it will update or
store the new smallest number in this variable.

When the program exits the loop the last value stored in the variable selected
will be print as the smallest number entered..
Past Paper Question #3
Past Paper Answer #3
START
Declare itemName as String
Declare itemCost, tax, totalCost as Real IF (isTaxable = 1) Then
Declare isTaxable as Integer Tax = itemCost x VAT
VAT = 0.16 ENDIF
PRINT “Enter name of Item or X to end” IF (isTaxable = 2) Then
READ itemName Tax = 0.0
WHILE itemName < > ‘X’ DO ENDIF
PRINT “Enter Cost” totalCost = itemCost + Tax
READ itemCost PRINT “Item is ”, itemName, “ and Cost is”,
PRINT “Enter 1 for taxable or 2 for totalCost
non-taxable” PRINT “Enter name of Item or X to end”
READ isTaxable READ itemName
ENDWHILE
STOP.

You might also like