Lecture 06: Algorithms Flowcharts Pseudocode
CS 101: Introduction to Computing
Algorithms Flowcharts Pseudocode
Saima Jabeen
Lec06: Algorithms Flowcharts Pseudocode
S 101: Introduction to Computing
Last time
Computer programming
Programming languages
Programming languages categorization
Algorithm
Today
Algorithms - Examples
Flowcharts
Expressing algorithms: Pseudocode
2
Lec06: Algorithms Flowcharts Pseudocode
S 101: Introduction to Computing
Calculate and print the average grade
of 3 tests for the entire class
Input
output
3 test scores for each student
Average of 3 tests for each student
Process
1.
2.
3.
4.
5.
6.
Get three scores
Add them together
Divide by three to get the average
Print the average
Repeat step 1 to 4 for next student
Stop if there are no more students
3
Lec06: Algorithms Flowcharts Pseudocode
S 101: Introduction to Computing
Flow Charts
A flowchart is a visual
representation of an algorithm.
The flowchart employs a series of blocks and
arrows, each of which represents a particular
operation or step in the algorithm.
The arrows represent the sequence in which the
operations are implemented.
or
graphical
Lec06: Algorithms Flowcharts Pseudocode
S 101: Introduction to Computing
Flowcharts Most Common Symbols
Function
Symbol Name
Terminal
Flow-line
Process
Represents the
of a program.
Represents the
flow of logic.
Represents calculations or data
manipulation.
Input/Output
Decision
beginning or end
Represents inputs or outputs
of data and information.
Represents a comparison,
question, or decision that
determines alternative paths to be
followed.
5
Lec06: Algorithms Flowcharts Pseudocode
S 101: Introduction to Computing
Flowcharts An Example
Find the solution of a quadratic equation
Ax2+Bx+C=0, given A, B and C.
START
INPUT
A, B, C
Calculate
R = SQRT(B2-4AC)
X1 = (-B+R)/(2A)
X2 = (-B-R)/(2A)
PRINT
A, B, C, X1, X2
END
Lec06: Algorithms Flowcharts Pseudocode
S 101: Introduction to Computing
Comparison of Algorithm representations in Natural language, flowchart and
Pseudo-code
START
Step 1: Begin the calculations
INPUT
A, B
Step 2: Input two values A and B
Step 3: Add the values
Add A to B
and store in C
Step 4: Display the result
Step 5: End the calculation
BEGIN Adder
Input A and B
C=A+B
PRINT C
END Adder
OUTPUT
C
END
Natural language
Flowchart
Pseudo-code
7
Lec06: Algorithms Flowcharts Pseudocode
S 101: Introduction to Computing
Algorithm Representation
(Natural Languages)
English or some other natural language.
Are not particularly good:
too verbose
Unstructured
too rich in interpretation (ambiguous)
imprecise
Lec06: Algorithms Flowcharts Pseudocode
S 101: Introduction to Computing
Algorithm Representation
(Using Programming Language)
{
int I, m, Carry;
int a[100], b[100], c[100];
cin >> m;
for ( int j = 0 ; k <= m-1 ; j++ ) {
cin >> a[j];
cin >> b[j];
}
Carry = 0;
i = 0;
while ( i < m ) {
Lec06: Algorithms Flowcharts Pseudocode
S 101: Introduction to Computing
Pseudo-code
Pseudo-code = English but looks like programming
Pseudocode is an artificial and informal language that helps
programmers develop algorithms.
Computer scientists use pseudo-code to express
algorithms:
English like constructs (or other natural language), but
modeled to look like statements in typical programming languages.
Good compromise
Simple, readable, no rules, don't worry about punctuation. Lets
you think at an abstract level about the problem.
Contains only instructions that have a well-defined structure and
resemble programming language
10
Lec06: Algorithms Flowcharts Pseudocode
S 101: Introduction to Computing
Pseudo-code Primitives
Three basic kind of operations:
Sequential
Computation ( Set )
Input/Output ( Get ... / Print ... )
Conditional
If Else
If
Iterative / looping
Repeat
While ...
11