Algorithms_Pseudocode and Flowcharts
Algorithms_Pseudocode and Flowcharts
3. Break It Down
Remember, big problems are just a collection of smaller
problems. Break the problem into smaller, solvable pieces.
Tackle each part individually and then assemble them to
solve the larger issue.
Input-Process-Output Model
Method for Developing an
Algorithm
1. Define the problem: State the problem you are trying to solve in clear
and concise terms.
2. List the inputs (information needed to solve the problem) and the outputs
(what the
algorithm will produce as a result)
3. Describe the steps needed to convert or manipulate the inputs to produce
the outputs.
Start at a high level first, and keep refining the steps until they are
effectively computable
operations.
4. Test the algorithm: choose data sets and verify that your algorithm works!
The Böhm–Jacopini theorem, states the three specific ways (
control structures). These are
1. Sequence. One statement is executed after another
2. Selection. statements can executed or skipped depending on
whether a condition evaluates to TRUE or FALSE
3. Iteration. statements can be executed repeatedly
until a condition evaluates to TRUE or FALSE
*The theorem is typically credited to a 1966 paper by Corrado Böhm and Giuseppe Jacopini.
Selection Structure
There are three selection structures in Java PL and C:
1. if
2. if - else
3. switch
Repetition Structure
There are three repetition structures in Java PL and C:
1. while
2. do…while
3. for
Pseudocode (or Program Design
Language)
• Consists of natural language-like statements that precisely describe the
steps of an algorithm or program
• Statements describe actions
• Focuses on the logic of the algorithm or program
• Avoids language-specific elements
• Written at a level so that the desired programming code can be
generated almost automatically from each statement
• Steps are numbered. Subordinate numbers and/or indentation are used
for dependent statements in selection and repetition structures.
• 2
Pseudocode Keywords
1. //: This keyword is used to represent a comment.
2. BEGIN,END: Begin is the first statement and end is
the last statement.
3. INPUT, GET, READ: The keyword is used to input
data.
4. COMPUTE, CALCULATE: used for calculation of the
result of the given expression.
Pseudocode Keywords
INCREMENT counter1
Algorithm Constructs:
Input/Output
Pseudocode Flowcharts
Double-Selection IF
1. IF condition THEN
1.1 Statement 1
1.2 Statement 2
2. ELSE
2.1 statement 2
2.2 statement 3
3. ENDIF
Algorithms Constructs: Selection
Pseudocode Flowcharts
Double-Selection IF
1. SWITCH expression
TO
1.1 case 1: action1
1.2 case 2: action2
1.3 etc.
1.4 default: actionx
Algorithms Constructs: Repetition
Pseudocode Flowcharts
WHILE
1.WHILE condition
1.1 Statement 1
1.2 Statement 2
Algorithms Constructs: Repetition
Pseudocode Flowcharts
DO-WHILE
6. DO
6.1 statement 1
6.2 statement 2
7. WHILE condition
Tests condition at the end of the loop. Thus,
statements in the structure will always be executed at least once.
Algorithms Constructs: Repetition
Pseudocode Flowcharts
FOR structure
6. FOR bounds on
repetition
6.1 statement 1
6.2 etc.
BEGIN
1. DECLARE variables: dividend, divisor, quotient
2. DISPLAY “enter dividend and divisor”
3. GET dividend and divisor
4. IF divisor is equal to zero, THEN
4.1. DO
4.1.1. Display error message, “divisor must be non-zero”
4.1.2. Prompt user to enter divisor
4.1.3. Get divisor
4.2. WHILE divisor is equal to zero
5. ENDIF
6. DISPLAY dividend and divisor
7. CALCULATE quotient as dividend/divisor
8. DISPLAY quotient
END
(Flowchart) Examples
Express an algorithm to get two numbers from
the user (dividend and divisor), testing to DISPLAY
make sure that the divisor number is not zero,
and displaying their quotient using flowchart.
DISPLAY
Some more examples (Q & A)
The End…
References
https://www.bolton.ac.uk/blogs/the-art-of-problem-solving-in-computer-programming