Algorithms Pseudocode Flowcharts PDF
Algorithms Pseudocode Flowcharts PDF
Algorithms
Pseudocode
Flowcharts
Instructor zgr ZEYDAN (PhD)
CIV 112 Computer Programming
http://cevre.beun.edu.tr/zeydan/
2/25/2015
http://en.wikipedia.org/wiki/File:Sieve_of_Eratosthenes_animation.gif
Instructor zgr ZEYDAN
2/25/2015
A pictorial representation of
problem-solving
2/25/2015
2/25/2015
2/25/2015
Programming Tools
Tools used to convert algorithms into computer programs:
Pseudocode: An informal high-level description of the
operating principle of a computer program. It uses the
structural conventions of a programming language, but
is intended for human reading rather than machine
reading.
Flowcharts: Graphically depict the logical steps to carry
out a task and show how the steps relate to each other.
Pseudocode vs Flowcharts
Artificial and Informal
language
Helps programmers to
plan an algorithm
Similar to everyday
English
Not an actual
programming language
A graphical way of
writing pseudocode
Rounded rectangle
terminal
Parallelogram input /
output
Rectangle actions
Diamonds decision /
conditional
Circles connector
2/25/2015
Flowchart Symbols
Example Pseudocode
Start
Read A, B
Calculate C = A*B
Display C
Stop
Start - Terminal
Read A, B Input
Calculate C = A*B - Action
Display C - Output
Stop - Terminal
2/25/2015
Example Flowchart
Start Terminal.
Program start
here
S ta rt
Read
Read
A
B
Input.
Enter values for
A and B
C a lc u l a t e R e s u t
C = A *B
Process
D is p la y t h e
R e s u lt C
Output
S to p
Stop Terminal
Program end
here
2/25/2015
Question ???
Structured Programming
Structured programming was first suggested by Corrado
Bohm and Guiseppe Jacopini. The two mathematicians
demonstrated that any computer program can be written
with just three structures: sequences, decisions, and loops.
Sequences: one command is executed after previous one.
Decisions (selections): statement(s) is (are) executed if
certain condition gives TREU or FALSE value.
Loops (repetition): statement(s) is (are) executed
repeatedly until certain condition gives TREU or FALSE
value.
Corrado; B. and Jacopini, G. (May 1966). "Flow Diagrams,
Turing Machines and Languages with Only Two Formation
Rules". Communications of the ACM 9 (5): 366371.
Instructor zgr ZEYDAN
2/25/2015
Sequences
Decisions (selections)
Three selection structure in C
programming:
If
If else
Switch
10
2/25/2015
Decisions
(selections)
Loops (repetition)
Three repetition structure
in C programming:
While
Do while
For
11
2/25/2015
Loops (repetition)
Ref: Deitel P J (Ed.) (2010) C How to Program, 6th Edition, Prentice Hall
Instructor zgr ZEYDAN
12
2/25/2015
Example - 2
Write an algorithm to determine a students average grade
and indicate whether he is successful or not.
The average grade is calculated as the average of mid-term
and final marks.
Student will be successful if his average grade is grater or
equals to 60.
Pseudocode
Start
Use variables mid term , final and average
Input mid term and final
Calculate the average by summing mid term and final and
dividing by 2
if average is below 60
Print FAIL
else
Print SUCCESS
Stop
Instructor zgr ZEYDAN
13
2/25/2015
Detailed Algorithm
1. Step:
2. Step:
3. Step:
endif
Flowchart
START
Input
mid-term, final
If
average<60
PRINT
SUCCESS
PRINT
FAIL
STOP
Instructor zgr ZEYDAN
14
2/25/2015
Nested If
Simply, if structure in if structure
Example - 3: Both final and average grades must be grater
than or equals to 35 for curve calculation in BEU.
if (final >= 35) then
{ if (average >= 35) then
execute curve calculation commands
endif
}
else
Print FF grade
endif
Instructor zgr ZEYDAN
15
2/25/2015
Example - 4
Write an algorithm which calculates the average exam
grade for a class of 5 students.
What are the program inputs?
the exam grades
Processing:
Find the sum of the grades;
count the number of students; (counter controlled)
calculate average grade = sum of grades / number of
students.
What is the program output?
the average exam grade
Instructor zgr ZEYDAN
Pseudocode
Start
Use variables total, counter, grade, average
Initialize total = 0
Initialize counter = 1
While (counter <= 5)
Input grade
Calculate total = total + grade
Calculate counter = counter + 1
End-while
Calculate average = total / 5
Display average
Stop
Instructor zgr ZEYDAN
16
2/25/2015
Flowchart
Example - 5
Write an algorithm which calculates the average exam
grade for a class of unknown number of students.
What are the program inputs?
the exam grades
Processing:
Find the sum of the grades till sentinel value is given; for
example -99 to break loop (sentinel controlled)
calculate average grade = sum of grades / number of
students.
What is the program output?
the average exam grade
Instructor zgr ZEYDAN
17
2/25/2015
Pseudocode
Start
Use variables total, counter, grade, average
Initialize total = 0
Initialize counter = 0
While (grade != -99)
Input grade
Calculate total = total + grade
Calculate counter = counter + 1
End-while
Calculate average = total / counter
Display average
Stop
Instructor zgr ZEYDAN
Example - 6
Write an algorithm which calculates the average exam
grade for a class of unknown number of students.
This time, the number of students have been asked at the
beginning of the program.
Use counter controlled structure.
18
2/25/2015
Pseudocode
Start
Question
Draw a flowchart for example 6.
19
2/25/2015
20