ALGORITHMS AND FLOWCHARTS Part #2
Feb 15, 2016
ALGORITHMS AND
FLOWCHARTS
Part #2
Three Constructs
Flowcharts for Three Constructs
Pseudocode for Three Constructs
Examples-1 calculate 12 + 22 + ... + n2
Pseudocode
Input: n
sum 0
i 1
Repeat the following
three steps while i n:
sq i * i
sum sum + sq
i i + 1
Output: sum
A B
Set A to the
value of B
Examples-1:
Flowchart
n
sum 0
i 1
i n ?
sq i * i
sum sum + sq
i i + 1
sum
No
Yes
Input / output
Processing step
Decision
Examples-2:
1. Start at pos0, facing dir0
2. If wall in front,
turn 90º clockwise
else
go forward
3. If not back to initial
position / direction
proceed to Step 2
else
stop
dir dir + 90º
pos = pos0
and dir = dir0?
Yes No
Yes
No
pos pos0
dir dir0
Step forward
Input:
pos0, dir0
Stop
Wall in front?
Examples-3:
Sort the following objects based on their
heights
Output
Expected result
Strategy
There are many strategies for solving this
problem. We demonstrate a simple one:
Repeat the following steps while the list is un-sorted:
Start with the first object in the list
Swap it with the one next to it if they are in the wrong order
Repeat the same with the next to the first object
Keep on repeating until you reach the last object in the list
Back to The Objects to be Sorted
12
Q: Is the list sorted?
A: No
13
Sorting: Step A1
14
Sorting: Step A1
Swap? Yes
15
Sorting: Step A2
16
Sorting: Step A2
Swap? Yes
17
Sorting: Step A3
18
Sorting: Step A3
Swap? No
19
Sorting: After Step A7
20
Q: Is the list sorted?
A: No
21
Sorting: Step B1
22
Sorting: Step B1
Swap? Yes
23
Sorting: Step B2
24
Sorting: Step B2
Swap? No
25
Sorting: After Step B7
26
Q: Is the list sorted?
A: No
27
Sorting: Step C1
28
Sorting: Step C1
Swap? No
29
Sorting: After Step C7
30
Q: Is the list sorted?
A: Yes
31
32
Start
n = n+1
Get list
list
sorted?
Stop
SWAP
list[n], list[n+1]
list is an array containing the heights
N is the total number of objects in the list
Flowchart for the Sorting Process
No
Yes
n = 0 list[n] >
list[n+1]?
Yes No n>N ?
Yes
No
Variables
Algorithms usually work with variables
A variable is a “named container”
A variable is like a slate on which a
value can be written and later erased
and replaced with another value
sum sum + sq
sum
Properties of Algorithm
Compactness: an algorithm can use
iterations or recursion to repeat the
same steps multiple times
Generality: the same algorithm applies
to any “size” of task or any input values
Abstractness: an algorithm does not
depend on a particular computer
language or platform (although it may
depend on the general computing
model)
Properties of Algorithm
Input: n
sum 0
i 1
Repeat the following
three steps while i n:
sq i * i
sum sum + sq
i i + 1
Output: sum
Compact: the same length regardless
of n, thanks to iterations the
algorithm repeats the same
instructions many times, but with
different values of the variables
(The “running time” depends on n, of course)
General: works for any n
Properties of Algorithm
function addSquares(n : integer)
: integer;
var
i, sum : integer;
begin
sum := 0;
for i := 1 to n do begin
sum := sum + i * i
end;
addSquares := sum;
end;
Abstract:
Pascal
C/C++
Java
public class MyMath
{
public static int
addSquares(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum += i * i;
return sum;
}
}
int addSquares(int n)
{
int i, sum = 0;
for (i = 1; i <= n; i++)
sum += i * i;
return sum;
}
Iterations
Repeat the same sequence of
instructions multiple times
Start with initial values of variables
Values of some of the variables
change in each cycle
Stop when the tested condition
becomes false
Supported by high-level
programming languages
Iterations: while Loop in Java
For example:
while (<this condition holds>)
{
... // do something
}
while (i <= n)
{
sum += i * i; // add i * i to sum
i++; // increment i by 1
}
Iterations: while Loop in Java
for (<initial setup>; <as long as this condition holds>;
<adjust variable(s) at the end of each iteration>)
{
... // do something
}
for ( int i = 1; i <= n; i++)
{
sum += i * i; // add i * i to sum
}
For example: Increment i
by 1
Assignments
Write an algorithm in pseudocode that finds
the average of two numbers
Write an algorithm to change a numeric grade
to a letter grade.
Write an algorithm to find the largest of a set
of numbers. You do not know the number of
numbers.
Write an algorithm to find the largest of 1000
numbers.