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.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

C) between 18 and 27. D) between 27 and 50.
Solving Equations with Variables on Both Sides
Solving Systems of Linear Equations Graphically and Numerically
Bellwork If you roll a die, what is the probability that you roll a 2 or an odd number? P(2 or odd) 2. Is this an example of mutually exclusive, overlapping,
Slide 1 Insert your own content. Slide 2 Insert your own content.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Multiplication X 1 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 4 x 1 = 4 5 x 1 = 5 6 x 1 = 6 7 x 1 = 7 8 x 1 = 8 9 x 1 = 9 10 x 1 = x 1 = x 1 = 12 X 2 1.
Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
/4/2010 Box and Whisker Plots Objective: Learn how to read and draw box and whisker plots Starter: Order these numbers.
Combining Like Terms. Only combine terms that are exactly the same!! Whats the same mean? –If numbers have a variable, then you can combine only ones.
Graphing Linear Inequalities in Two Variables
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Warm up Complete the analogies: 5 is to 25 as 6 is to ___________ 7 is to 77 as 11 is to ___________ ¼ is to ½ as.25 is to ____________
0 - 0.
1 1  1 =.
1  1 =.
Measures of Central Tendency
Jeopardy Q $100 Q $100 Q $100 Q $100 Q $100 Q $200 Q $200 Q $200
2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt 2 pt 3 pt 4 pt 5 pt 1 pt Time Money AdditionSubtraction.
Addition Facts
M5N1. Students will further develop their understanding of whole numbers. A. Classify the set of counting numbers into subsets with distinguishing characteristics.
Year 6 mental test 10 second questions
Year 6 mental test 15 second questions Numbers and number system Numbers and the number system, Measures and Shape.
£1 Million £500,000 £250,000 £125,000 £64,000 £32,000 £16,000 £8,000 £4,000 £2,000 £1,000 £500 £300 £200 £100 Welcome.
Objective - To simplify expressions using the order of operations. Simplify each expression below. 1) 6 + 5(8 - 2) 2) 3) 4)
ORDER OF OPERATIONS LESSON 2 DAY 2. BEDMAS B – Brackets E – Exponents D – Division from left to right M – Multiply from left to right A – Add from left.
Objective The student will be able to:
Algebra I 1.2 Order of Operations.
0 PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions.
Data Structures: A Pseudocode Approach with C
Ch 6 Sec 4: Slide #1 Columbus State Community College Chapter 6 Section 4 An Introduction to Applications of Linear Equations.
Open Sentences.
Evaluate the numerical expression 52 – 2 ∙ 4 + (7 – 2)
Bell Ringer 1-5 Reading Quiz
What is it and how do I know when I see it?
Columbus State Community College
ALGORITHMS - PART 3 REPETITION/LOOP CONTROL STRUCTURE
Area of triangles.
R.USHA TGT ( MATHEMATICS) KV,GILL NAGAR CHENNAI.
Chapter 2 Section 3.
Fraction XI Adding Mixed Numbers With Unlike Denominators
8 2.
Simultaneous Equations 6-Apr-17
A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
Lets play bingo!!. Calculate: MEAN Calculate: MEDIAN
1, 3, 5, 7, 9, … + 2 TermNumbersPattern of Numbers The n-order for the pattern of odd numbers is 2n – 1, for n is natural numbers n ?
Sets Sets © 2005 Richard A. Medeiros next Patterns.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
The Properties of Mathematics
Control Structures Selections Repetitions/iterations
Addition 1’s to 20.
Decisions If statements in C.
Test B, 100 Subtraction Facts
What Does This Program Do?
Solve inequalities by using the Addition or Subtraction Properties of Inequality. Main Idea/Vocabulary.
Number bonds to 10,
Middle School Lesson 2 Activity 3 – The Guessing Game
Beat the Computer Drill Divide 10s Becky Afghani, LBUSD Math Curriculum Office, 2004 Vertical Format.
CSI 1306 PROGRAMMING IN VISUAL BASIC PART 2. Part 2  1. Strings  2. Translating Conditional Branch Instructions  3. Translation Set 2  4. Debugging.
Types of selection structures
12-2 Conditional Probability Obj: To be able to find conditional probabilities and use formulas and tree diagrams.
An Interactive Tutorial by S. Mahaffey (Osborne High School)
Bottoms Up Factoring. Start with the X-box 3-9 Product Sum
X-box Factoring. X- Box 3-9 Product Sum Factor the x-box way Example: Factor 3x 2 -13x (3)(-10)= x 2x 3x 2 x-5 3x +2.
1 Functions and Applications
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
ALGORITHMS - PART 2 CONDITIONAL BRANCH CONTROL STRUCTURE
REPETITION/LOOP CONTROL STRUCTURE
Presentation transcript:

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 subscripts to indicate the individual members of the list For example, if X is a list of size N, then the individual members are: X 1, X 2, X 3,………., X N

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

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, N Get K Let Val = L K Give Val

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, N Get K Get M Let L K = M Give L

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 L K = M (5) Give L Ln L N K M 1 (1,8,3,6,2) (1,20,3,6,2) 5 output (1,20,3,6,2)

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 Let Mid = L loc Give Mid

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 (3) Let Mid = L loc (4) Give Mid LN L N Loc Mid 1 (1,8,3,6,2) output 3

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

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 = FALSE X or True and not Y or Z = TRUE not(X and not Z or False and (True and Y)) = TRUE Y and Y or not Z = FALSE

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 = X 1 + X 2 + …….. + X N –Total = Total + X I 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

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 + X I Let I = I + 1 Finish Loop Give Total

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 + X I (6) Let I = I + 1 (7) Finish Loop (8) Give Total LN X N I Total Test 1 (3, 7, 5) (1 <= 3) (2 <= 3) (3 <= 3) (4 <= 3) 8 Output 15

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 X I = V? –Stop the index at this point »FOUND at X I –I to process the list

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 (X I = V) Let Found = True Else Let I = I + 1 Finish Loop When (I >N) or (Found) Give Found

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 (X I = 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) False (3 = 7) (2>3) or (F) 6 (7 = 7) 7 True 10 (2>3) or (T) 11 Output True

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 (X I > Max)? –I for list processing Must track not only the Max, but also the location of Max (the value of I)

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 (X I > Max) Let Max = X I Let Loc = I Let I = I + 1 Finish Loop Give Max Give Loc

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 (X I > Max) (6) Let Max = X I (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) (1 <= 4) 5 (8 > -1) (2 <= 4) 5 (3 > 8) (3 <= 4) 5 (25>8) (4 <= 4) 5 (9>25) (5 <= 4) 10 Output Output 3

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 X I = 0? Let Count = Count + 1 if a match –I for list processing

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 (X I = 0) Let Count = Count + 1 Let I = I + 1 Finish Loop When Give Count

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 X I with every other element of the list Loop within a Loop –For each X I »Search for X I Search only until one match is found –Stop the Indexes at the locations

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 (X Test = X I ) 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

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 (X Test = X I ) (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 TEST 1,2,3 (2,8,7,8) 4 False (2=8) (3>4)or(F) 7 (2=7) (4>4)or(F) 7 (2=8) (5>4)or(F) 12 Not (F) (2=4)or(F) (8=7) (4>4)or(F) 7 (8=8) 8 True 11 (5>4)or(T) 12 Not (T) 14 (3=4)or(T) 15 Output True

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 –Bob –Cathy –Dave

Grouped Lists We have three lists –Name = (Ann, Bob, Cathy, Dave) –Midterm = (100, 75, 60, 40) –Final = (30, 28, 90, 80) Since Ann is Name 1, then her midterm mark would be Midterm 1 and her final mark would be Final 1 We can also create a new list from other lists.

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 G I = 0.75*F I *M I Let I = I + 1 Finish Loop Give G

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 G I = 0.75*F I *M I (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,??,??,??) (2 <= 4) 4 (48,40,??,??) (3 <= 4) 4 (48,40,83,??) (4 <= 4) 4 (48,40,83,70) (5 <= 4) 7 Output (48,40,83,70)

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 (G I < Min) Let Min = G I Let Loc = I Let I = I + 1 Finish Loop Let LName = Name Loc Give LName

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 (G I < Min) (6) Let Min = G I (7) Let Loc = I (8) Let I = I + 1 (9) Finish Loop (10) Let LName = Name Loc (11) Give LName Name = (Ann, Bob, Cathy, Dave) G = (48,40,83,70) LN N I LOC MIN Lname TEST 1,2, (1 <= 4) 5 (48<101) (2<=4) 5 (40<48) (3<=4) 5 (83<40) (4<=4) 5 (70<40) (5<=4) 10 Bob 11 Output Bob

Additional Material

Flow Charts

Algorithm 4.1 NAME: FindK GIVENS: L, N, K Change: None RESULTS: Val INTERMEDIATES: None DEFINITION: Val := FindK(L, N, K)

Algorithm 4.2 NAME: ChangeK GIVENS: L, N, K, M Change: L RESULTS: None INTERMEDIATES: None DEFINITION: ChangeK(L, N, K, M)

Algorithm 4.3 NAME: Middle GIVENS: L, N Change: None RESULTS: Mid INTERMEDIATES: Loc DEFINITION: Mid := Middle(L, N)

Algorithm 4.4 Name:SUMLIST Given: X, N Change: None Result: Total Intermediate: I Definition Total := SUMLIST(X,N)

Algorithm 4.5 Name: SEARCHX Given: X, N, V Change: None Result: Found Intermediate: I Definition: Found := SEARCHX(X,N,V)

Algorithm 4.6 Name: MAXLIST Given: X, N Change:None Result: Max, Loc Intermediate: I Definition: (Loc,Max) := MAXLIST(X,N)

Algorithm 4.7 Name: NUM0 Given: X, N Change: None Result: Count Intermediate: I Definition: Count := NUM0(X,N)

Algorithm 4.8 Name: DUPLICATE Given: X, N Change: None Result: Double Intermediate: I, Test Definition: Double := DUPLICATE(X,N)

Algorithm 4.9 Name: FINDGRADE Given: M, F, N Change: None Result: G Intermediate: I Definition: G := FINDGRADE(M,F,N)

Algorithm 4.10 Name: WORST Given: Name,G, N Change: None Result: LName Intermediate: Loc, Min, I Definition: Lname :=WORST(Name,G,N)

NSD

Algorithm 4.1 NAME: FindK GIVENS: L, N, K Change: None RESULTS: Val INTERMEDIATES: None DEFINITION: Val := FindK(L, N, K)

Algorithm 4.2 NAME: ChangeK GIVENS: L, N, K, M Change: L RESULTS: None INTERMEDIATES: None DEFINITION: ChangeK(L, N, K, M)

Algorithm 4.3 NAME: Middle GIVENS: L, N Change: None RESULTS: Mid INTERMEDIATES: Loc DEFINITION: Mid := Middle(L, N)

Algorithm 4.4 Name:SUMLIST Given: X, N Change: None Result: Total Intermediate: I Definition Total := SUMLIST(X,N)

Algorithm 4.5 Name: SEARCHX Given: X, N, V Change: None Result: Found Intermediate: I Definition: Found := SEARCHX(X,N,V)

Algorithm 4.6 Name: MAXLIST Given: X, N Change:None Result: Max, Loc Intermediate: I Definition: (Loc,Max) := MAXLIST(X,N)

Algorithm 4.7 Name: NUM0 Given: X, N Change: None Result: Count Intermediate: I Definition: Count := NUM0(X,N)

Algorithm 4.8 Name: DUPLICATE Given: X, N Change: None Result: Double Intermediate: I, Test Definition: Double := DUPLICATE(X,N)

Algorithm 4.9 Name: FINDGRADE Given: M, F, N Change: None Result: G Intermediate: I Definition: G := FINDGRADE(M,F,N)

Algorithm 4.10 Name: WORST Given: Name,G, N Change: None Result: LName Intermediate: Loc, Min, I Definition: Lname :=WORST(Name,G,N)

Homework

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.

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)