csi 1306 algorithms - part 4 list processing. lists sometimes a problem deals with a list of values...

56
CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING

Upload: seamus-westgate

Post on 30-Mar-2015

230 views

Category:

Documents


10 download

TRANSCRIPT

Page 1: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

CSI 1306

ALGORITHMS - PART 4LIST PROCESSING

Page 2: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

ListsSometimes a problem deals with a list of values We represent such a list with a single name, and

use subscripts to indicate the individual members of the list

For example, if X is a list of size N, then the individual members are:

X1 , X2 , X3 ,………., XN

Page 3: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Lists

Always include the size of the list as part of the information for the algorithm – remember storage? How much room must the

computer set aside for our list?

– there are techniques for working with lists of unknown length, but they are beyond the scope of this course.

– either you will know the length of the list or you will be able to calculate it

Page 4: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.1

Write an algorithm that returns the Kth element of a given list L, with N numbers

– NAME: FindK– GIVENS: L, N, K

• Change: None– RESULTS: Val– INTERMEDIATES: None– DEFINITION: Val := FindK(L, N, K)– -------------------------– METHOD:

Get L, NGet K

Let Val = LK

Give Val

Page 5: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.2

Write an algorithm that replaces the Kth element`s value of a given list L (with N numbers) with the given value M– NAME: ChangeK– GIVENS: L, N, K, M

• Change: L– RESULTS: None– INTERMEDIATES: None– DEFINITION: ChangeK(L, N, K, M)– -------------------------– METHOD:

Get L, NGet KGet M

Let LK = M

Give L

Page 6: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Trace 4.1

Trace Algorithm 4.2 with a list L of 5 elements (1, 8, 3, 6, 2) where K is 2 and M is 20

METHOD:(1) Get L, N(2) Get K(3) Get M

(4) Let LK = M

(5) Give L

Ln L N K M 1 (1,8,3,6,2) 5 2 2 3 20 4 (1,20,3,6,2) 5 output (1,20,3,6,2)

Page 7: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.3

Given a list, L, of N numbers, where N is odd, find the middle number in the list

– NAME: Middle– GIVENS: L, N

• Change: None– RESULTS: Mid– INTERMEDIATES: Loc– DEFINITION: Mid := Middle(L, N)– -------------------------– METHOD:

• Get L, N

• Let Loc = N div 2 + 1• Let Mid = Lloc

• Give Mid

Page 8: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Trace 4.2

Trace Algorithm 4.3 with a list L of 5 elements (1, 8, 3, 6, 2)

METHOD:(1) Get L, N(2) Let Loc = N div 2 + 1(3) Let Mid = Lloc

(4) Give Mid

LN L N Loc Mid

1 (1,8,3,6,2) 5

2 3

3 3

4 output 3

Page 9: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

List Processing

Remember that we represent a list with a single name and use subscripts to indicate the individual members of the list

Processing a list usually involves a loop in which we use the list subscript variable as the loop control variable

Frequently, we also use Boolean operators in our loop tests– The next slide reviews use of these operators

Page 10: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

List Processing

Assume that the Boolean variables X, Y and Z are set to the following values:X = True, Y = False, Z = True

The order of precedence for Boolean operators is Not, And, Or

What is the result of each of the following expressions:(X and Y) or Z = TRUE(True or Y) and False = FALSEX or True and not Y or Z = TRUEnot(X and not Z or False and (True and Y)) = TRUEY and Y or not Z = FALSE

Page 11: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.4

Given a list X of N numbers, find the sum of the numbers in X

Analysis– Basic Algorithm

• SUM• Let Total = 0• Let Total = Total + ????

– Total = X1 + X2 + …….. + XN – Total = Total + XI

• Let I go from 1 to N in a loop; then we will add each X I

• Loop control variable is the list subscript variable

Page 12: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.4

Given a list X of N numbers, find the sum of the numbers in X– Name:SUMLIST

– Given: X, N

• Change: None

– Result: Total

– Intermediate: I

– Definition

• Total := SUMLIST(X,N)

Method

Get X, N

Let Total = 0

Let I = 1

Loop When (I <= N)

Let Total = Total + XI

Let I = I + 1

Finish Loop

Give Total

Page 13: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Trace 4.3

Trace algorithm 4.4 with the list (3,7,5). N is 3.

(1) Get X, N

(2) Let Total = 0

(3) Let I = 1

(4) Loop When (I <= N)

(5) Let Total = Total + XI

(6) Let I = I + 1

(7) Finish Loop

(8) Give Total

LN X N I Total Test 1 (3, 7, 5) 3 2 0 3 1

4 (1 <= 3)5 36 2 4 (2 <= 3) 5 106 3

4 (3 <= 3)5 156 44 (4 <= 3)

8 Output 15

Page 14: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.5

Given a list X of N numbers, determine whether the given value V occurs in the list X.

Analysis– Basic Algorithm

• SEARCH

• Let Found = False

• Does XI = V?

– Stop the index at this point

» FOUND at XI

– I to process the list

Page 15: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.5

Given a list X of N numbers, determine whether the given value V occurs in the list X.Name: SEARCHX

Given: X, N, V

Change: None

Result: Found

Intermediate: I

Definition:

Found := SEARCHX(X,N,V)

Method Get X, N Get V Let Found = False Let I = 1 Loop If (XI = V) Let Found = True Else Let I = I + 1

Finish Loop When (I >N) or (Found)

Give Found

Page 16: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Trace 4.4

Trace algorithm 4.5 with the list (3,7,5) and with a value of 7 for V

(1) Get X, N(2) Get V(3) Let Found = False(4) Let I = 1(5) Loop(6) If (XI = V)(7) Let Found = True(8) Else(9) Let I = I + 1

(10) Finish Loop When (I >N) or Found

(11) Give Found

LN X N V I Found Test 1 (3,7,5) 3 2 7 3 False 4 1 6 (3 = 7) 9 2 10 (2>3) or (F)

6 (7 = 7) 7 True10 (2>3) or (T) 11 Output True

Page 17: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.6

Given a list X of N positive numbers, find the maximum number and its position in X

Analysis– Basic Algorithm

• MAX

• Let Max = -1

• (XI > Max)?

– I for list processing• Must track not only the Max, but also the location of Max (the

value of I)

Page 18: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.6

Given a list X of N positive numbers, find the maximum number and its position in X

– Name: MAXLIST

– Given: X, N

• Change:None

– Result: Max, Loc

– Intermediate: I

– Definition:

– (Loc,Max) := MAXLIST(X,N)

Method Get X, N Let Max = -1 Let I = 1

Loop When (I <= N) If (XI > Max) Let Max = XI

Let Loc = I Let I = I + 1 Finish Loop

Give MaxGive Loc

Page 19: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Trace 4.5 Trace algorithm 4.6 with the list (8,3,25,9)

(1) Get X, N(2) Let Max = -1(3) Let I = 1

(4) Loop When (I <= N)(5) If (XI > Max)(6) Let Max = XI

(7) Let Loc = I (8) Let I = I + 1(9) Finish Loop

(10) Give Max(11) Give Loc

LN X N I MAX LOC TEST

1,2,3 (8,3,25,9) 4 1 -1

4 (1 <= 4)

5 (8 > -1)

6 8

7 1

8 2

4 (2 <= 4)

5 (3 > 8)

8 3

4 (3 <= 4)

5 (25>8)

6 25

7 3

8 4

4 (4 <= 4)

5 (9>25)

8 5

4 (5 <= 4)

10 Output 25

11 Output 3

Page 20: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.7

Given a list X of N numbers, find the number of times zero occurs in the list

Analysis– Basic Algorithm

• COUNT

• Let Count = 0

• XI = 0?

• Let Count = Count + 1 if a match

– I for list processing

Page 21: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.7

Given a list X of N numbers, find the number of times zero occurs in the list

– Name: NUM0

– Given: X, N

• Change: None

– Result: Count

– Intermediate: I

– Definition:

– Count := NUM0(X,N)

Method

Get X, N

Let Count = 0

Let I = 1

Loop When (I <= N)

If (XI = 0)

Let Count = Count + 1

Let I = I + 1

Finish Loop When

Give Count

Page 22: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.8

Given a list X of N numbers, see if X contains any two numbers which are the same

Analysis– Basic Algorithm

• Variant of SEARCH• Compare each XI with every other element of the list• Loop within a Loop

– For each XI

» Search for XI

• Search only until one match is found– Stop the Indexes at the locations

Page 23: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.8

Given a list X of N numbers, see if X contains any two numbers which are the same– Name: DUPLICATE– Given: X, N

• Change: None– Result: Double– Intermediate: I, Test– Definition:– Double := DUPLICATE(X,N)

Method Get X, N Let Double = False Let Test = 1 Loop Let I = Test + 1 Loop If (XTest = XI) Let Double = True Else Let I = I + 1 Finish Loop When (I >N) or (Double) If (Not (Double)) Let Test = Test + 1 Finish Loop When (Test = N) or (Double)

Give Double

Page 24: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Trace 4.6 Trace algorithm 4.8 with the list

(2,8,7,8)(1) Get X, N(2) Let Double = False(3) Let Test = 1(4) Loop(5) Let I = Test +1(6) Loop(7) If (XTest = XI)(8) Let Double = True(9) Else(10) Let I = I + 1(11) Finish Loop When (I >N) or (Double)(12) If (Not(Double))(13) Let Test = Test + 1(14) Finish Loop When (Test = N) or (Double)

(15) Give Double

LN X N Double Test I TEST1,2,3 (2,8,7,8) 4 False 1 5 2 7 (2=8)10 311 (3>4)or(F) 7 (2=7)10 411 (4>4)or(F) 7 (2=8)10 511 (5>4)or(F)12 Not (F)13 214 (2=4)or(F) 5 3 7 (8=7)10 411 (4>4)or(F) 7 (8=8) 8 True11 (5>4)or(T)12 Not (T)14 (3=4)or(T)15 Output True

Page 25: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Grouped Lists

If we want to process multiple, related lists (as in a database), we must maintain the order relationship between elements in each list

E.g. Student grades– Name - Midterm - Final

– Ann - 100 - 30

– Bob - 75 - 28

– Cathy - 60 - 90

– Dave - 40 - 80

Page 26: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Grouped Lists

We have three lists– Name = (Ann, Bob, Cathy, Dave)

– Midterm = (100, 75, 60, 40)

– Final = (30, 28, 90, 80)

Since Ann is Name1, then her midterm mark would be Midterm1 and her final mark would be Final1

We can also create a new list from other lists.

Page 27: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.9

Given two lists, Midterm and Final, of N numbers each, calculate a Final grade of 75% Final and 25% Midterm

– Name: FINDGRADE

– Given: M, F, N

• Change: None

– Result: G

– Intermediate: I

– Definition:

– G := FINDGRADE(M,F,N)

Method

Get M, F, N

Let I = 1

Loop When (I <= N)

Let GI = 0.75*FI + 0.25*MI

Let I = I + 1 Finish Loop

Give G

Page 28: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Trace 4.7

Trace algorithm 4.9 with the grouped list defined previously

Method

(1) Get M, F, N

(2) Let I = 1

(3) Loop When (I <= N)

(4) Let GI = 0.75*FI + 0.25*MI

(5) Let I = I + 1(6) Finish Loop

(7) Give G

F = (30, 28, 90, 80)

M = (100, 75, 60, 40)

LN I N G Test

1,2 1 4 (??,??,??,??)

3 (1 <= 4)

4 (48,??,??,??)

5 2

3 (2 <= 4)

4 (48,40,??,??)

5 3

3 (3 <= 4)

4 (48,40,83,??)

5 4

3 (4 <= 4)

4 (48,40,83,70)

5 5

3 (5 <= 4)

7 Output (48,40,83,70)

Page 29: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.10

Given a set of lists, Name and Grade, determine who received the lowest grade.– Name: WORST

– Given: Name,G, N

• Change: None

– Result: LName

– Intermediate:

• Loc, Min, I

– Definition:

– Lname :=WORST(Name,G,N)

Method Get Name, G, N Let Min = 101 Let I = 1 Loop When (I <= N)

If (GI < Min)

Let Min = GI

Let Loc = I

Let I = I + 1 Finish Loop

Let LName = NameLoc

Give LName

Page 30: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Trace 4.8

Trace algorithm 4.10 with the grouped list defined previously

(1) Get Name, G, N(2) Let Min = 101(3) Let I = 1(4) Loop When (I <= N)(5) If (GI < Min)(6) Let Min = GI

(7) Let Loc = I

(8) Let I = I + 1(9) Finish Loop(10) Let LName = NameLoc

(11) Give LName

Name = (Ann, Bob, Cathy, Dave)G = (48,40,83,70)

LN N I LOC MIN Lname TEST1,2,3 4 1 101 4 (1 <= 4) 5 (48<101) 6 48 7 1 8 2

4 (2<=4) 5 (40<48) 6 40 7 2 8 3

4 (3<=4) 5 (83<40) 8 4

4 (4<=4) 5 (70<40) 8 5 4 (5<=4)

10 Bob 11 Output Bob

Page 31: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Additional Material

Page 32: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Flow Charts

Page 33: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.1NAME: FindK

GIVENS: L, N, K

Change: None

RESULTS: Val

INTERMEDIATES: None

DEFINITION:

Val := FindK(L, N, K)

StartFINDK

Get L,NGet K

Let Val = LK

Give Val

FinishFINDK

Page 34: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.2

NAME: ChangeK

GIVENS: L, N, K, M

Change: L

RESULTS: None

INTERMEDIATES: None

DEFINITION:

ChangeK(L, N, K, M)

StartCHANGEK

Get L,NGet KGet M

Let LK =M

Give L

FinishCHANGEK

Page 35: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.3NAME: Middle

GIVENS: L, N

Change: None

RESULTS: Mid

INTERMEDIATES: Loc

DEFINITION:

Mid := Middle(L, N)

StartMIDDLE

Get L,N

Let Loc = N div 2 + 1Let Mid = LLoc

Give Mid

FinishMIDDLE

Page 36: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.4Name:SUMLIST

Given: X, N

Change: None

Result: Total

Intermediate: I

Definition

Total := SUMLIST(X,N)

StartSUMLIST

Get X, N

Let Total = 0Let I = 1

(I <= N)

Let Total = Total + XI

Let I = I + 1

Give Total

FinishSUMLIST

Y

N

Page 37: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.5

Name: SEARCHX

Given: X, N, V

Change: None

Result: Found

Intermediate: I

Definition:

Found := SEARCHX(X,N,V)

StartSEARCHX

Get X, NGet V

Let Found = FalseLet I = 1

If(XI = V)

Let Found = True Let I = I + 1

(I > N) or(Found)

Give Found

FinishSEARCHX

N

Y

N

Y

Page 38: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.6

Name: MAXLIST

Given: X, N

Change:None

Result: Max, Loc

Intermediate: I

Definition:

(Loc,Max) := MAXLIST(X,N)

StartMAXLIST

Get X,N

Let Max = -1Let I = 1

(I <= N)

Let Max = XI Let Loc = I

Give MaxGive Loc

FinishMAXLIST

Y

N

If (XI > Max)

Let I = I + 1

Y

N

Page 39: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.7

Name: NUM0

Given: X, N

Change: None

Result: Count

Intermediate: I

Definition:

Count := NUM0(X,N)

StartNUM0

Get X,N

Let Count = 0Let I = 1

(I <= N)

Let Count = Count + 1

Give Count

FinishNUM0

Y

N

If (XI = 0)

Let I = I + 1

Y

N

Page 40: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.8

Name: DUPLICATE

Given: X, N

Change: None

Result: Double

Intermediate: I, Test

Definition:

Double := DUPLICATE(X,N)

StartDUPLICATE

Get X,N

Let Double = FalseLet Test = 1

Give Double

FinishDUPLICATE

Let Double = True Let I = I + 1

(I > N) or (Double)

If (XTest = XI )

Let I = Test + 1

If (Not (Double))

Let Test = Test + 1

(Test = N) or (Double)

N

Y

N

Y

Y

N

Y

N

Page 41: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.9

Name: FINDGRADE

Given: M, F, N

Change: None

Result: G

Intermediate: I

Definition:

G := FINDGRADE(M,F,N)

StartFINDGRADE

Get M,F,N

Let I = 1

(I <= N)

Let GI = 75%FI + 25%MI

Let I = I + 1

Give G

FinishFINDGRADE

Y

N

Page 42: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.10

Name: WORST

Given: Name,G, N

Change: None

Result: LName

Intermediate:

Loc, Min, I

Definition:

Lname :=WORST(Name,G,N)

StartWORST

Get Name, G, N

Let Min = 101Let I = 1

(I <= N)

Let Min = GI

Let Loc = I

Give LName

FinishWORST

Y

N

If (GI < Min)

Let I = I + 1

Y

N

Let LName = NameLoc

Page 43: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

NSD

Page 44: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.1NAME: FindK

GIVENS: L, N, K

Change: None

RESULTS: Val

INTERMEDIATES: None

DEFINITION:

Val := FindK(L, N, K)

Get L,NGet KLet Val = LK

Give Val

Page 45: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.2

NAME: ChangeK

GIVENS: L, N, K, M

Change: L

RESULTS: None

INTERMEDIATES: None

DEFINITION:

ChangeK(L, N, K, M)

Get L, NGet K

Get M

Let LK = M

Give L

Page 46: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.3NAME: Middle

GIVENS: L, N

Change: None

RESULTS: Mid

INTERMEDIATES: Loc

DEFINITION:

Mid := Middle(L, N)

Get L, NLet Loc = N div 2 + 1Let Mid = LLoc

Give Mid

Page 47: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.4Name:SUMLIST

Given: X, N

Change: None

Result: Total

Intermediate: I

Definition

Total := SUMLIST(X,N)

While (I <=N)

Let Total = Total + XI

Let I = I + 1

Get X, NLet Total = 0

Let I = 1

Give Total

Page 48: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.5

Name: SEARCHX

Given: X, N, V

Change: None

Result: Found

Intermediate: I

Definition:

Found := SEARCHX(X,N,V)

Y N

Let Found = True Let I = I + 1

Let I = 1

Let Found = False

Get VGet X, N

Give FoundUntil (I > N) or (Found)

If (XI = V)

Page 49: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.6

Name: MAXLIST

Given: X, N

Change:None

Result: Max, Loc

Intermediate: I

Definition:

(Loc,Max) := MAXLIST(X,N)

Y NLet Max = XI

Let Loc = I

While (I <= N)

Give MaxGive Loc

Get X, NLet Max = -1

Let I = 1

If (XI > Max)

Do Nothing

Let I = I + 1

Page 50: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.7

Name: NUM0

Given: X, N

Change: None

Result: Count

Intermediate: I

Definition:

Count := NUM0(X,N)

Y N

Let Count = Count + 1 Do Nothing

Give CountLet I = I + 1

If (XI = 0)

While (I <= N)

Let Count = 0Get X, N

Let I = 1

Page 51: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.8

Name: DUPLICATE

Given: X, N

Change: None

Result: Double

Intermediate: I, Test

Definition:

Double := DUPLICATE(X,N)Y N

Let Double = True Let I = I + 1

NDo Nothing

Let I = Test + 1

Give Double

If (XTest = XI)

Until (I > N) or (Double)If (Not(Double))

YLet Test = Test + 1Until (Test = N) or (Double)

Get X, NLet Double = False

Let Test = 1

Page 52: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.9

Name: FINDGRADE

Given: M, F, N

Change: None

Result: G

Intermediate: I

Definition:

G := FINDGRADE(M,F,N)

While (I <= N)

Let GI = 75%FI + 25% MI

Let I = I + 1

Get M, F, NLet I = 1

Give G

Page 53: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Algorithm 4.10

Name: WORST

Given: Name,G, N

Change: None

Result: LName

Intermediate:

Loc, Min, I

Definition:

Lname :=WORST(Name,G,N)

Y NLet Min = GI

Let Loc = I

Get Name, G, NLet Min = 101

Let I = 1

If (GI < Min)

While (I <= N)

Let LName = NameLoc

Give Lname

Do Nothing

Let I = I + 1

Page 54: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Homework

Page 55: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Write an algorithm to find the maximum value in a list of positive numbers. Each entry in the list is unique (ie. There are no duplicate numbers)

Write an algorithm that fills a list with the first 50 multiples of 7. (ie. 7, 14, 21…350)

Write an algorithm to populate a list with names. The list will continue to grow until the user indicates it is time to stop.

Page 56: CSI 1306 ALGORITHMS - PART 4 LIST PROCESSING. Lists Sometimes a problem deals with a list of values We represent such a list with a single name, and use

Rewrite Algorithm 4.6, such that the variable Max is not used at all:

– Name: MAXLIST

– Given: X, N

• Change:None

– Result: Loc

– Intermediate: I

– Definition:

– Loc := MAXLIST(X,N)