09 Arrays Mark Dixon SOFT 131 Page 1

  • Slides: 30
Download presentation
09 – Arrays Mark Dixon SOFT 131 Page 1

09 – Arrays Mark Dixon SOFT 131 Page 1

Questions: Loops • What is the value of t, after this code executes? t

Questions: Loops • What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next 15 Mark Dixon SOFT 131 Page 2

Questions: Loops • Simplify the following code, so that it easy to change the

Questions: Loops • Simplify the following code, so that it easy to change the number of faces: par. Faces. inner. HTML = par. Faces. inner. HTML + "<img src=face. jpg>" par. Faces. inner. HTML = par. Faces. inner. HTML + "<img src=face. jpg>" Dim f For f = 1 To 7 par. Faces. inner. HTML = par. Faces. inner. HTML + "<img src=face. jpg>" Next Mark Dixon SOFT 131 Page 3

Session Aims & Objectives • Aims – To introduce the main concepts involved in

Session Aims & Objectives • Aims – To introduce the main concepts involved in handling more complex (multi valued) data • Objectives, after this week’s sessions, you should be able to: – declare arrays – assign values to array elements – use for loops with arrays Mark Dixon SOFT 131 Page 4

Example: German Numbers • User Requirements SPECIFICATION – describe user's objectives no mention of

Example: German Numbers • User Requirements SPECIFICATION – describe user's objectives no mention of technology • User Requirements – help people learn German numbers 1 - 10 • Software Requirements – Functional • list facilities to be provided (often numbered) – Non-functional • list desired characteristics (often more subjective) Mark Dixon SOFT 131 – Functional: – show German word for numbers (between 1 and 10) – user enter digits – check if correct – Non-functional should be easy to use Page 5

Example: German Numbers • Problem: can't directly pick random word • Can: pick random

Example: German Numbers • Problem: can't directly pick random word • Can: pick random number, and then work out word 1 – eins 2 – zwei 3 – drei 4 – vier 5 – funf 6 – sechs 7 – sieben 8 – acht 9 – neun 10 – zehn Mark Dixon SOFT 131 Page 6

Example: German Numbers v 0 <script language=vbscript> Option Explicit Dim n Sub Window_On. Load()

Example: German Numbers v 0 <script language=vbscript> Option Explicit Dim n Sub Window_On. Load() Randomize End Sub • Pick random number • Use If statements – one inside another Mark Dixon Sub btn. Start_On. Click() n = 1 + CInt(Rnd() * 9) If n = 1 Then par. Quest. inner. Text = "What is eins? " Else If n = 2 Then par. Quest. inner. Text = "What is zwei? " Else If n = 3 Then par. Quest. inner. Text = "What is drei? " Else If n = 4 Then par. Quest. inner. Text = "What is vier? " Else If n = 5 Then par. Quest. inner. Text = "What is funf? " Else If n = 6 Then par. Quest. inner. Text = "What is sechs? " Else If n = 7 Then par. Quest. inner. Text = "What is sieben? " Else If n = 8 Then par. Quest. inner. Text = "What is acht? " Else If n = 9 Then par. Quest. inner. Text = "What is neun? " Else If n = 10 Then par. Quest. inner. Text = "What is zehn? " End If End If End If End Sub btn. Check_On. Click() If CInt(txt. Ans. value) = n Then par. Res. inner. Text = "Correct!" Else par. Res. inner. Text = "Sorry, please try again. " & n End If End Sub </script> SOFT 131 Sub btn. Start_On. Click() n = 1 + CInt(Rnd() * 9) If n = 1 Then par. Quest. inner. Text = "What is eins? " Else If n = 2 Then par. Quest. inner. Text = "What is zwei? " Else If n = 3 Then par. Quest. inner. Text = "What is drei? " Else If n = 4 Then par. Quest. inner. Text = "What is vier? " Else If n = 5 Then par. Quest. inner. Text = "What is funf? " Else If n = 6 Then par. Quest. inner. Text = "What is sechs? Else If n = 7 Then par. Quest. inner. Text = "What is siebe Else If n = 8 Then par. Quest. inner. Text = "What is acht Else If n = 9 Then par. Quest. inner. Text = "What is neu Else If n = 10 Then par. Quest. inner. Text = "What is ze End If End If End If End Sub Page 7

Array Variables (what) • multiple values – stored in single variable • index –

Array Variables (what) • multiple values – stored in single variable • index – identifies individual values (called elements) Index Value 0 134 1 127 2 139 3 155 • the value of element 3 is 155 4 143 5 151 6 141 Mark Dixon SOFT 131 Page 8

Arrays Variables Mark Dixon SOFT 131 Page 9

Arrays Variables Mark Dixon SOFT 131 Page 9

Array Variables (Declaration) • General syntax: Dim varname(last) • Specific examples: Dim HR(16) Dim

Array Variables (Declaration) • General syntax: Dim varname(last) • Specific examples: Dim HR(16) Dim x(8) Mark Dixon SOFT 131 Page 10

Array Variables (Assignment) • General syntax: arrayname(index) = value • Specific examples: HR(0) =

Array Variables (Assignment) • General syntax: arrayname(index) = value • Specific examples: HR(0) = 134 HR(5) = 151 + b x(5) = 23. 87 x(7) = (y + 189. 2516) / 2 Mark Dixon SOFT 131 Page 11

Arrays: why? (declaration) 5 variable declarations Dim Name 1 Dim Name 2 Dim Name

Arrays: why? (declaration) 5 variable declarations Dim Name 1 Dim Name 2 Dim Name 3 Dim Name 4 Dim Name 5 Name 1 = Name 2 = Name 3 = Name 4 = Name 5 = Mark Dixon Single array declaration Dim Name(4) "Bob" "Sally" "Jo" "Fred" "Alison" SOFT 131 Name(0) Name(1) Name(2) Name(3) Name(4) = = = "Bob" "Sally" "Jo" "Fred" "Alison" Page 12

Arrays: why? (use) Dim Num = 1 + Rnd() * 3 If Num =

Arrays: why? (use) Dim Num = 1 + Rnd() * 3 If Num = 1 Then Res = Name 1 Else. If Num = 2 Then Res = Name 2 Else. If Num = 3 Then Res = Name 3 Else. If Num = 4 Then Res = Name 4 Else Res = Name 5 End If Mark Dixon SOFT 131 Dim Num = 1 + Rnd() * 3 Res = Name(Num) Single line of code picks any element Page 13

Example: German Numbers v 1 Array Declaration Array Assignment Array Use Mark Dixon SOFT

Example: German Numbers v 1 Array Declaration Array Assignment Array Use Mark Dixon SOFT 131 <script language=vbscript> Option Explicit Dim Nums(10) Dim n Sub Window_On. Load() Randomize Nums(1) = "eins" Nums(2) = "zwei" Nums(3) = "drei" Nums(4) = "vier" Nums(5) = "funf" Nums(6) = "sechs" Nums(7) = "sieben" Nums(8) = "acht" Nums(9) = "neun" Nums(10) = "zehn" End Sub btn. Start_On. Click() n = 1 + CInt(Rnd() * 9) par. Quest. inner. Text = "What is " & Nums(n) & "? End Sub btn. Check_On. Click() If CInt(txt. Ans. value) = n Then par. Res. inner. Text = "Correct!" Else par. Res. inner. Text = "Sorry, please try again. " & End If End Sub </script> Page 14

Example: German Numbers v 0 vs. v 1 v 0 <script language=vbscript> Option Explicit

Example: German Numbers v 0 vs. v 1 v 0 <script language=vbscript> Option Explicit Dim n <script language=vbscript> Option Explicit Dim Nums(10) Dim n Sub Window_On. Load() Randomize End Sub 54 lines Mark Dixon Sub btn. Start_On. Click() n = 1 + CInt(Rnd() * 9) If n = 1 Then par. Quest. inner. Text = "What is eins? " Else If n = 2 Then par. Quest. inner. Text = "What is zwei? " Else If n = 3 Then par. Quest. inner. Text = "What is drei? " Else If n = 4 Then par. Quest. inner. Text = "What is vier? " Else If n = 5 Then par. Quest. inner. Text = "What is funf? " Else If n = 6 Then par. Quest. inner. Text = "What is sechs? " Else If n = 7 Then par. Quest. inner. Text = "What is sieben? " Else If n = 8 Then par. Quest. inner. Text = "What is acht? " Else If n = 9 Then par. Quest. inner. Text = "What is neun? " Else If n = 10 Then par. Quest. inner. Text = "What is zehn? " End If End If End If End Sub v 1 Sub Window_On. Load() Randomize Nums(1) = "eins" Nums(2) = "zwei" Nums(3) = "drei" Nums(4) = "vier" Nums(5) = "funf" Nums(6) = "sechs" Nums(7) = "sieben" Nums(8) = "acht" Nums(9) = "neun" Nums(10) = "zehn" End Sub btn. Start_On. Click() n = 1 + CInt(Rnd() * 9) par. Quest. inner. Text = "What is " & Nums(n) & "? " End Sub btn. Check_On. Click() If CInt(txt. Ans. value) = n Then par. Res. inner. Text = "Correct!" Else par. Res. inner. Text = "Sorry, please try again. " & n End If End Sub </script> 27 lines Sub btn. Check_On. Click() If CInt(txt. Ans. value) = n Then par. Res. inner. Text = "Correct!" Else par. Res. inner. Text = "Sorry, please try again. " & n End If End Sub </script> SOFT 131 Page 15

Error: Subscript Out of Range • Index too big/small <HTML> <HEAD> <TITLE></TITLE> <script language=vbscript>

Error: Subscript Out of Range • Index too big/small <HTML> <HEAD> <TITLE></TITLE> <script language=vbscript> Option Explicit Dim x(3) x(0) = 9 x(1) = 5 x(2) = 21 x(3) = 23 x(4) = 12 </script> </HEAD> <BODY> </HTML> Mark Dixon SOFT 131 Page 16

Questions: Arrays • Write a line of code that declares an array called Books

Questions: Arrays • Write a line of code that declares an array called Books with 56 elements Dim Books(56) • Write a line of code that assigns the value 45 to the 18 th element of the array. Books(18) = 45 • Write some code that makes the background red, but only when the 12 th array element is larger than 6 If Books(12) >6 Then document. bgcolor = "red" End If Mark Dixon SOFT 131 Page 17

Example: Capital Cities SPECIFICATION • User Requirements – help people learn Capital Cities •

Example: Capital Cities SPECIFICATION • User Requirements – help people learn Capital Cities • Software Requirements – Functional: – ask user for capital of random country – user enter capital – check if correct – Non-functional should be easy to use Mark Dixon SOFT 131 Page 18

Example: Capital Cities Option Explicit Dim Country(4) Dim City(4) Dim Num • How many

Example: Capital Cities Option Explicit Dim Country(4) Dim City(4) Dim Num • How many array: – declarations? – assignments? Mark Dixon Sub Window_On. Load() Country(1) = "UK" City(1) = "London" Country(2) = "France" City(2) = "Paris" Country(3) = "Spain" City(3) = "Madrid" Country(4) = "Greece" City(4) = "Athens" Randomize End Sub btn. Start_On. Click() Num = 1 + CInt(Rnd() * 3) par. Quest. inner. Text = "What is the capital of " & Country(N End Sub SOFT 131 Page 19

Example: Capital Cities • Two arrays – stored in same order: Country 0 1

Example: Capital Cities • Two arrays – stored in same order: Country 0 1 2 3 Mark Dixon City UK France Spain Greece 0 1 2 3 SOFT 131 London Paris Madrid Athens Page 20

Question: Arrays Option Explicit Dim Country(4) Dim City(4) Dim Num Sub Window_On. Load() Country(1)

Question: Arrays Option Explicit Dim Country(4) Dim City(4) Dim Num Sub Window_On. Load() Country(1) = "UK" City(1) = "London" Country(2) = "France" City(2) = "Paris" Country(3) = "Spain" City(3) = "Madrid" Country(4) = "Greece" City(4) = "Athens" Randomize End Sub • Write a statement that will decide whether the answer given by the user is correct: Sub btn. Start_On. Click() Num = 1 + CInt(Rnd() * 3) par. Quest. inner. Text = "What is the capital of " & Country(Num) & "? " End Sub If txt. Num. value = City(Num) Then Mark Dixon SOFT 131 Page 21

Example: Drinks v 1 Total of array Clears array Displays array Mark Dixon Searches

Example: Drinks v 1 Total of array Clears array Displays array Mark Dixon Searches array SOFT 131 Page 22

Example: Drinks v 1 <script language=VBScript> Dim Units(6) Dim cur. Unit Sub Window_On. Load()

Example: Drinks v 1 <script language=VBScript> Dim Units(6) Dim cur. Unit Sub Window_On. Load() cur. Unit = 0 End Sub btn. Add_On. Click() Units(cur. Unit) = txt. Unit. value cur. Unit = cur. Unit + 1 End Sub btn. Clear_On. Click() Units(0) = 0 Units(1) = 0 Units(2) = 0 Units(3) = 0 Units(4) = 0 Units(5) = 0 Units(6) = 0 cur. Unit = 0 End Sub btn. Show_On. Click() lbl. Res. inner. Text = "" lbl. Res. inner. Text = lbl. Res. inner. Text & Units(0) & " " lbl. Res. inner. Text = lbl. Res. inner. Text & Units(1) & " " lbl. Res. inner. Text = lbl. Res. inner. Text & Units(2) & " " lbl. Res. inner. Text = lbl. Res. inner. Text & Units(3) & " " lbl. Res. inner. Text = lbl. Res. inner. Text & Units(4) & " " lbl. Res. inner. Text = lbl. Res. inner. Text & Units(5) & " " lbl. Res. inner. Text = lbl. Res. inner. Text & Units(6) End Sub …. Mark Dixon SOFT 131 Page 23

Example: Drinks v 1 …. Sub btn. Total_On. Click() Dim total = 0 total

Example: Drinks v 1 …. Sub btn. Total_On. Click() Dim total = 0 total = total + Units(0) total = total + Units(1) total = total + Units(2) total = total + Units(3) total = total + Units(4) total = total + Units(5) total = total + Units(6) lbl. Res. inner. Text = total End Sub btn. Find_On. Click() If txt. Unit. value = Units(0) Then lbl. Res. inner. Text = "Found in slot 0" Else. If txt. Unit. value = Units(1) Then lbl. Res. inner. Text = "Found in slot 1" Else. If txt. Unit. value = Units(2) Then lbl. Res. inner. Text = "Found in slot 2" Else. If txt. Unit. value = Units(3) Then lbl. Res. inner. Text = "Found in slot 3" Else. If txt. Unit. value = Units(4) Then lbl. Res. inner. Text = "Found in slot 4" Else. If txt. Unit. value = Units(5) Then lbl. Res. inner. Text = "Found in slot 5" Else. If txt. Unit. value = Units(6) Then lbl. Res. inner. Text = "Found in slot 6" Else lbl. Res. inner. Text = "Not Found" End If End Sub </script> Mark Dixon SOFT 131 Page 24

Array Algorithms • Common tasks to many programs: – Reset all elements – Display

Array Algorithms • Common tasks to many programs: – Reset all elements – Display all elements – Total all elements – Search all elements – Find maximum value – Find minimum value – Average – Sort Mark Dixon SOFT 131 Page 25

Example: Drinks v 2 (Reset) • Use counter variable (i) as array index: Mark

Example: Drinks v 2 (Reset) • Use counter variable (i) as array index: Mark Dixon SOFT 131 Page 26

Example: Drinks v 2 (Total) Mark Dixon SOFT 131 Page 27

Example: Drinks v 2 (Total) Mark Dixon SOFT 131 Page 27

Tutorial Exercise: German Numbers • Task 1: Complete German Numbers Example from lecture. You

Tutorial Exercise: German Numbers • Task 1: Complete German Numbers Example from lecture. You will need to complete the code for checking the user's answer • Task 2: Modify your page so that it hides and shows the buttons appropriately • Task 3: Modify your page to allow the user 3 attempts only. • Task 4: Modify your page to prevent same random number appearing twice – store used numbers – use Do Until new value different from previous • Task 5: Modify your page so that it plays appropriate sounds when the user gets the answer right/wrong Mark Dixon SOFT 131 Page 28

Tutorial Exercise: Capital Cities • Task 1: Complete Capital Cities Example from the lecture,

Tutorial Exercise: Capital Cities • Task 1: Complete Capital Cities Example from the lecture, adding some more cities. You will need to complete the code for checking the user's answer • Task 2: Modify your page so that it hides and shows the buttons appropriately • Task 3: Modify your page to allow the user 3 attempts only. • Task 4: Modify your page so that it is case in-sensitive (i. e. user can type upper or lower case) • Task 5: Modify your page so that it displays an appropriate picture of the selected capital city. Hint: create another array for the file names. • Task 6: Modify your page so that it plays appropriate sounds when the user gets the answer right/wrong Mark Dixon SOFT 131 Page 29

Tutorial Exercise: Drinks • Task 1: Get the Drinks v 2 example (from the

Tutorial Exercise: Drinks • Task 1: Get the Drinks v 2 example (from the lecture) working. You have the code for Add, Clear, & Show but not for Total and Find • Task 2: Modify your page so that it displays a meaningful message when all elements of the array are used up (not the error dialogue below). Mark Dixon SOFT 131 Page 30