0% found this document useful (0 votes)
51 views42 pages

(M2 Main) PDF

This document provides information about computer programming and program logic design. It discusses algorithms, pseudocode, and flowcharts. Key points covered include: - Algorithms are step-by-step methods for solving problems and can be expressed using pseudocode or flowcharts. - Pseudocode uses plain English and keywords to describe an algorithm without formal syntax. - Flowcharts use standard graphic symbols to visually represent the sequence of coded instructions in a program. - Several examples are provided to demonstrate how to write algorithms as pseudocode and represent them in flowcharts.

Uploaded by

Artus Artigas
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)
51 views42 pages

(M2 Main) PDF

This document provides information about computer programming and program logic design. It discusses algorithms, pseudocode, and flowcharts. Key points covered include: - Algorithms are step-by-step methods for solving problems and can be expressed using pseudocode or flowcharts. - Pseudocode uses plain English and keywords to describe an algorithm without formal syntax. - Flowcharts use standard graphic symbols to visually represent the sequence of coded instructions in a program. - Several examples are provided to demonstrate how to write algorithms as pseudocode and represent them in flowcharts.

Uploaded by

Artus Artigas
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
You are on page 1/ 42

COMPUTER

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

Example: Example: Example:


num := 1 FOR num = 1 TO 10 num :=1
WHILE num<=10 PRINT num DO
PRINT num ENDFOR PRINT num
INCREMENT num INCREMENT num
ENDWHILE WHILE num<=10
PSEUDOCODE EXAMPLES
Problem Specification 1: Input Processing Output
Ms. Noreen wants a program TotalBill tip = TotalBill x tip
that will calculate and display TipPercentage TipPercentage
the amount she should tip a
waiter at a restaurant. Tip can Pseudocode
be computed by multiplying the ENTER TotalBill, TipPercentage
total bill and the tip (using a COMPUTE tip := TotalBill x TipPercentage
percentage). DISPLAY tip
Input Processing Output
Problem Specification 2:
firstNumber firstNumber > highest number
Make a program that will secondNumber secondNumber and lowest number
display the lowest and highest
of two integers entered by the Pseudocode
user using if-else statement. ENTER firstNumber, secondNumber
IF firstNumber > secondNumber
THEN
DISPLAY firstNumber(highest),
secondNumber(lowest)
ELSE
DISPLAY secondNumber(highest),
firstNumber(lowest)
ENDIF
Input Processing Output
Problem Specification 3:
Make a program that will ask letter input letter letter
the user to choose from letters matching interpretation
A to C then display the
Pseudocode
equivalent fruit starting from
the selected letter using case INPUT letter
statement.
CASE letter of
A or a: PRINT “A is for apple"
B or b: PRINT “B is for banana“
C or c: PRINT “C is for cranberry"
OTHERS
PRINT “Input a valid letter (A, B, C) “
ENDCASE
Input Processing Output
Problem Specification 5:
Make a program that will number sum := sum + sum
display the sum of three input number
numbers using for loop
Pseudocode
statement
SET sum = 0

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

WHILE numberCounter < 3


INPUT number
CALCULATE sum := sum + number
INCREMENT numberCounter
ENDWHILE
PRINT sum
Problem Specification 6: Input Processing Output
Make a program that will display number sum := sum + sum
the sum of three input numbers number
using do-while loop statement Pseudocode
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.

• Program Flowchart is a diagram that uses a set of standard graphic


symbols to represent the sequence of coded instructions fed into a
computer, enabling it to perform specified logical and arithmetical
operations.
SYMBOL MEANING SYMBOL MEANING

Initialization Input / Output

Decision Flow Lines

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

Ms. Noreen wants a program that calculates and INPUT


display the amount she should tip a waiter at a TotalBill,
tipPercentage
restaurant. Tip can be computed by multiplying the
total bill and the tip (using a percentage).
tip = TotalBill x
Input Processing Output TipPercentage
TotalBill tip = TotalBill x tip
TipPercentage TipPercentage
OUTPUT
Pseudocode tip
ENTER TotalBill, TipPercentage
COMPUTE tip =: TotalBill x TipPercentage
DISPLAY tip END
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

Make a program that will display the sum of


counter = 0, sum = 0
three input numbers using while loop statement

Input Processing Output FALSE


number sum := sum + sum counter
OUTPUT END
< 3?
number sum

TRUE
Pseudocode
INPUT
SET numberCounter = 0, sum = 0 number

WHILE numberCounter < 3


INPUT number sum = sum+number
CALCULATE sum := sum + number
INCREMENT numberCounter
ENDWHILE
PRINT sum counter = counter+1
START

Make a program flowchart that m=1


will display numbers from 1 to
10 using looping structure FALSE
if END
m<=10

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

START #include<iostream> Pre-processor


using namespace std;
int main() Main function
INPUT
x, y { Start
int x, y; Variable declarations
cout <<"Enter two integers: " ;
average = (x+y) / 2
cin >> x >> y; Input
average = (x + y) /2 ; Computation (Process)
OUTPUT cout <<"The average of "<<x<<“ and
average Output
“<<y<<“is”<<average;
system ("pause");

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

You might also like