11 Array Variables Mark Dixon 1 Questions Loops
11 – Array Variables Mark Dixon 1
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 2
Questions: Loops • Simplify the following code, so that it is 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 3
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 4
Inefficient Code • duplication in both branches of if If weight > 2. 2 Then x=5 Else x=5 End If • unused variable declarations Dim x Dim y x=5 • redundant (nil effect) lines of code x = 23 x=5 Mark Dixon x=5 Dim x x=5 5
Example: German Numbers • User Requirements – describe user's objectives no mention of technology • Software Requirements – Functional • list facilities to be provided (often numbered) – Non-functional • list desired characteristics (often more subjective) Mark Dixon SPECIFICATION • User Requirements – help people learn German numbers 1 - 10 • Software Requirements – Functional: – show German word for numbers (between 1 and 10) – user enter digits – check if correct – Non-functional should be easy to use 6
Example: German Numbers • Problem: can't directly pick random word • Can: pick random number 1 – eins 2 – zwei 3 – drei 4 – vier 5 – funf 6 – sechs 7 – sieben 8 – acht 9 – neun 10 – zehn Mark Dixon 7
Example: German Numbers v 0 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 + Int(Rnd() * 10) 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 btn. Start_on. Click() n = 1 + Int(Rnd() * 10) 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 8
Array Variables (what) • multiple values – stored in single variable • index – identifies individual values (called elements) • the value of element 3 is 155 Index Value 0 134 1 127 2 139 3 155 4 143 5 151 6 141 • last element Mark Dixon 9
Arrays Variables Mark Dixon 10
Array Variables (Declaration) • General syntax: Dim varname(last. Element) • Specific examples: Dim HR(16) Dim x(8) Mark Dixon 11
Array Variables (Assignment) • General syntax: arrayname(index) = expression • Specific examples: HR(0) = 134 HR(5) = 151 + b x(5) = 23. 87 x(7) = (y + 189. 2516) / 2 Mark Dixon 12
Questions: Arrays Consider the following code: Dim x Dim res Dim age(2) x=2 age(0) = 19. 6 age(1) = 11. 23 age(2) = 15. 37 res = age(x) 1 • How many arrays are there? age • Name the array(s). • What is in res after the code executes? 15. 37 Mark Dixon 13
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" Name(0) Name(1) Name(2) Name(3) Name(4) = = = "Bob" "Sally" "Jo" "Fred" "Alison" 14
Arrays: why? (use) Dim Num = Int(Rnd() * 5) If Num = 0 Then Res = Name 1 Else. If Num = 1 Then Res = Name 2 Else. If Num = 2 Then Res = Name 3 Else. If Num = 3 Then Res = Name 4 Else Res = Name 5 End If Mark Dixon Dim Num = Int(Rnd() * 5) Res = Name(Num) Single line of code picks any element 15
Example: German Numbers v 1 Array Declaration Array Assignment Array Use Mark Dixon 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 + Int(Rnd() * 10) 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 16
Example: German Numbers v 0 vs. v 1 v 0 Option Explicit Dim n Sub window_on. Load() Randomize End Sub 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 v 1 Sub btn. Start_on. Click() n = 1 + Int(Rnd() * 10) 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 Sub btn. Start_on. Click() par. Quest. inner. Text = "What is vier? " n = 1 + Int(Rnd() * 10) Else par. Quest. inner. Text = "What is " & Nums(n) & "? " If n = 5 Then End Sub par. Quest. inner. Text = "What is funf? " Sub btn. Check_on. Click() Else If CInt(txt. Ans. value) = n Then If n = 6 Then par. Res. inner. Text = "Correct!" par. Quest. inner. Text = "What is sechs? " Else par. Res. inner. Text = "Sorry, please try again. " & n If n = 7 Then End If par. Quest. inner. Text = "What is sieben? " End Sub 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 27 lines 54 lines Mark Dixon 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 17
Error: Subscript Out of Range • Index too big/small Option Explicit Dim x(3) x(0) = 9 x(1) = 5 x(2) = 21 x(3) = 23 x(4) = 12 x=5 Mark Dixon 18
Error: Type mismatch • index missing Option Explicit Dim x(3) x(0) = 9 x(1) = 5 x(2) = 21 x(3) = 23 x=5 Mark Dixon 19
Error: Type mismatch • index given for non-array variable: b Option Explicit Dim x(3) Dim b x(0) = 9 x(1) = 5 x(2) = 21 x(3) = 23 b(3) = 12 Mark Dixon 20
Questions: Arrays • Write a line of code that declares an array called Books with 56 elements Dim Books(55) • Write a line of code that assigns the value 45 to the 18 th element of the array. Books(17) = 45 • Write some code that makes the background red, but only when the 12 th array element is larger than 6 If Books(11) >6 Then document. bg. Color = "red" End If Mark Dixon 21
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 22
Example: Capital Cities • Two arrays – stored in same order: Country 0 1 2 3 Mark Dixon UK France Spain Greece City 0 1 2 3 London Paris Madrid Athens 23
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 24
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 25
Example: Drinks v 1 Total of array Clears array Displays array Mark Dixon Searches array 26
Example: Drinks v 1 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 …. 27
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 Mark Dixon 28
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 29
Example: Drinks v 2 (Reset) • Use loop counter variable (i) as array index: Mark Dixon 30
Example: Drinks v 2 (Total) Mark Dixon 31
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 disables and enables the buttons appropriately • Task 3: Modify your page so that the text box gets the focus (cursor) after the start button is pressed • Task 4: Modify your page to allow the user 3 attempts only. • Task 5: Modify your page to prevent same random number appearing twice – store used numbers – use Do Until new value different from previous • Task 6: Modify your page so that it plays appropriate sounds when the user gets the answer right/wrong Mark Dixon 32
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 33
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 34
- Slides: 34