(M2 Main) PDF
(M2 Main) PDF
PROGRAMMING 1
MODULE 2
PROGRAM LOGIC DESIGN
AND FORMULATION
Upon completion of this module, you will be able to:
• Write algorithmic solutions to problems
• Apply the different program logic design tools in solving
computing problems
• Design, read, understand and construct program
flowchart
• Express algorithms using pseudocode and flowcharts
ALGORITHM
STEP-BY-STEP METHOD OF
SOLVING A PROBLEM
• Algorithm is a procedure or formula
for solving a problem, based on
conducting a sequence of specified
actions.
• Algorithm can be expressed using
pseudocode or flowchart.
• Plan the algorithm that will transform the problem’s input into
its output.
• Write the algorithm in the Processing column of the Input-
Process-Output (IPO) chart.
• Each instruction in the algorithm will describe an action that
the computer needs to take.
• Thus, each instruction should start with a verb.
• Most algorithms begin with instruction to enter the input items
into the computer.
• Next, record instructions to process the input items to achieve
the problem’s output.
• The processing typically involves performing one or more
calculations using the input items.
• Most algorithms end with instruction to display, print, or store
the output items.
• Display, print, and store refer to the computer screen, printer,
and a file on a disk, respectively.
Example:
IPO chart
PSEUDOCODE
FALSE CODE
• Pseudocode is called false code because, it has no syntax like
any of the programming language and thus can’t be compiled
or interpreted by the computer.
• It is simply an implementation of an algorithm in the form of
annotations and informative text written in plain English.
• It is not standardized.
• Every programmer has his or her own version, but you will find
some similarities among other versions.
HOW TO WRITE A
PSEUDOCODE?
FALSE CODE
• In Pseudocode, keywords are used to indicate common input-
output and processing operations. They are written fully in
uppercase.
• It also used mathematical, relational and logical operators to
express arithmetic and logical operations.
• When writing pseudocode, we assume that the order of execution
of the statements is from top to bottom. This changes when
using control structures, functions and exception handling.
KEYWORDS:
• START: This is the start of your pseudocode.
• INPUT, ENTER, READ / GET : This is data retrieved from the
user through typing or through an input device.
• PRINT, OUTPUT, DISPLAY, SHOW: This will show your output to
a screen or the relevant output device.
KEYWORDS:
• COMPUTE, CALCULATE, DETERMINE: This is used to calculate
the result of an expression.
• SET, INIT: To initialize values
• INCREMENT, BUMP: To increase the value of a variable
• DECREMENT: To reduce the value of a variable
COMMON OPERATORS:
• Assignment: ← or := • Comparison (Relational):
Example: =, ≠, <, >, ≤, ≥
c ← 2πr, c := 2πr Example:
• Arithmetic: +, −, ×, /, mod a = b, x ≠ y, m > n, s < t, n1 ≤ n2
Example: • Logical: and, or
a + b, x – y, m x n, Example:
s / t, num mod 2 m > n and s < t
m > n or s < t
KEYWORDS in CONDITIONAL:
During algorithm development, we need statements which evaluate
expressions and execute instructions depending on whether the
expression evaluated to True or False.
Here are some common conditions used in Pseudocode:
• IF — THEN – ENDIF
• IF – ELSE IF — ENDIF
• IF — ELSE IF — ELSE – ENDIF
• CASE – OTHERS – ENDCASE
KEYWORDS in CONDITIONAL:
Examples:
KEYWORDS in ITERATION/LOOPING:
To iterate is to repeat a set of instructions in order to generate a
sequence of outcomes.
WHILE condition FOR iteration bounds DO
sequence sequence sequence
ENDWHILE ENDFOR WHILE condition
FOR numberCounter = 1 to 3
INPUT number
CALCULATE sum := sum + number
ENDFOR
PRINT sum
Input Processing Output
Problem Specification 4:
Make a program that will number sum := sum + sum
display the sum of three input number
numbers using while loop
Pseudocode
statement
SET numberCounter = 0, sum = 0
DO
INPUT number
CALCULATE sum := sum + number
INCREMENT numberCounter
WHILE numberCounter < 3
PRINT sum
FLOWCHART
Flowchart is a pictorial representation of an ordered, step-by-step solution to
a problem.
On-page
Terminal
Connector
Off-page
Process
Connector
Flowcharts can be used to express different structures:
• sequence
• selection/conditional
• loop.
Selection Loop
Sequence
START
INPUT
PROCESS
OUTPUT
END
• In drawing a proper flowchart, all necessary requirements should be listed out in
logical order.
• The flowchart should be clear, neat and easy to follow. There should not be
any room for ambiguity in understanding the flowchart.
• The usual direction of the flow of a procedure or system is from left to right or
top to bottom.
• Only one flow line should come out from a process symbol.
• Only one flow line should enter a decision symbol, but two or three flow lines,
one for each possible answer, should leave the decision symbol.
• Ensure that the flowchart has a
logical start and finish.
• If the flowchart becomes complex, it is
better to use connector symbols to
reduce the number of flow lines.
• Avoid the intersection of flow lines if
you want to make it more effective and
better way of communication.
FLOWCHART EXAMPLES
START
INPUT
Input Processing Output firstNumber,
secondNumber
firstNumber firstNumber > highest number
secondNumber secondNumber and lowest
number
FALSE
firstNumber >
OUTPUT
Pseudocode secondNumber? secondNumber
Make a program (highest)
firstNumber
that will display ENTER firstNumber, secondNumber (lowest)
IF firstNumber > secondNumber
the lowest and TRUE
THEN
highest of two DISPLAY firstNumber(highest), OUTPUT
integers entered firstNumber A
secondNumber(lowest) (highest)
by the user. ELSE secondNumber
(lowest)
DISPLAY
secondNumber(highest), A
firstNumber(lowest) END
ENDIF
START
Make a program that will
determine and display whether
the input number is POSITIVE, INPUT
N
NEGATIVE or ZERO.
FALSE FALSE
OUTPUT
N>0? N<0? A
N “is
ZERO”
TRUE TRUE
OUTPUT OUTPUT
N “is N “is
POSITIVE” NEGATIVE”
A
A
END
START
TRUE
Pseudocode
INPUT
SET numberCounter = 0, sum = 0 number
TRUE
Output
m
m = m+1
PSEUDOCODE, FLOWCHART
& SOURCE CODE
Make a program that will determine and display the average of two
input integers.
Pseudocode
Input Processing Output ENTER x, y
x, y average= (x + y ) / average COMPUTE average =: (x + y ) / 2
2 DISPLAY average
FLOWCHART SOURCE CODE
END
return 0;
} End
• Cadenhead, R et. Al. (2016). C++ in 24 Hours, Sams Teach Yourself (6th
Edition). Sams Publishing
• McGrath, M. (2017). C++ programming in easy steps (5th ed.).
Warwickshire, United Kingdom: Easy Steps Limited
• Tale, T. (2016). C++: The Ultimate Beginners Guide to C++ Programing.
CreateSpace Independent Publishing Platform
• Cadenhead, R et. Al. (2016). C++ in 24 Hours, Sams Teach Yourself (6th
Edition).Sams Publishing
• McGrath, M. (2017). C++ Programming in Easy Steps (5th ed.).
Warwickshire, United Kingdom: Easy Steps Limited
• Deitel (2017), How to Program C++, 10th Edition