Top Banner
ALGORITHMS AND FLOWCHARTS Part #2
40

Algorithms and Flowcharts II

Feb 15, 2016

Download

Documents

algoritma and flowchart
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Algorithms and Flowcharts II

ALGORITHMS AND

FLOWCHARTS

Part #2

Page 2: Algorithms and Flowcharts II

Three Constructs

Page 3: Algorithms and Flowcharts II

Flowcharts for Three Constructs

Page 4: Algorithms and Flowcharts II

Pseudocode for Three Constructs

Page 5: Algorithms and Flowcharts II

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

Page 6: Algorithms and Flowcharts II

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

Page 7: Algorithms and Flowcharts II

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?

Page 8: Algorithms and Flowcharts II

Examples-3:

Sort the following objects based on their

heights

Page 9: Algorithms and Flowcharts II

Output

Expected result

Page 10: Algorithms and Flowcharts II

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

Page 11: Algorithms and Flowcharts II

Back to The Objects to be Sorted

Page 12: Algorithms and Flowcharts II

12

Q: Is the list sorted?

A: No

Page 13: Algorithms and Flowcharts II

13

Sorting: Step A1

Page 14: Algorithms and Flowcharts II

14

Sorting: Step A1

Swap? Yes

Page 15: Algorithms and Flowcharts II

15

Sorting: Step A2

Page 16: Algorithms and Flowcharts II

16

Sorting: Step A2

Swap? Yes

Page 17: Algorithms and Flowcharts II

17

Sorting: Step A3

Page 18: Algorithms and Flowcharts II

18

Sorting: Step A3

Swap? No

Page 19: Algorithms and Flowcharts II

19

Sorting: After Step A7

Page 20: Algorithms and Flowcharts II

20

Q: Is the list sorted?

A: No

Page 21: Algorithms and Flowcharts II

21

Sorting: Step B1

Page 22: Algorithms and Flowcharts II

22

Sorting: Step B1

Swap? Yes

Page 23: Algorithms and Flowcharts II

23

Sorting: Step B2

Page 24: Algorithms and Flowcharts II

24

Sorting: Step B2

Swap? No

Page 25: Algorithms and Flowcharts II

25

Sorting: After Step B7

Page 26: Algorithms and Flowcharts II

26

Q: Is the list sorted?

A: No

Page 27: Algorithms and Flowcharts II

27

Sorting: Step C1

Page 28: Algorithms and Flowcharts II

28

Sorting: Step C1

Swap? No

Page 29: Algorithms and Flowcharts II

29

Sorting: After Step C7

Page 30: Algorithms and Flowcharts II

30

Q: Is the list sorted?

A: Yes

Page 31: Algorithms and Flowcharts II

31

Page 32: Algorithms and Flowcharts II

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

Page 33: Algorithms and Flowcharts II

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

Page 34: Algorithms and Flowcharts II

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)

Page 35: Algorithms and Flowcharts II

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

Page 36: Algorithms and Flowcharts II

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;

}

Page 37: Algorithms and Flowcharts II

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

Page 38: Algorithms and Flowcharts II

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

}

Page 39: Algorithms and Flowcharts II

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

Page 40: Algorithms and Flowcharts II

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.