11 – Array Variables

35
Mark Dixon 1 11 – Array Variables

description

11 – Array Variables. Questions: Loops. What is the value of t, after this code executes? t = 0 For x = 4 To 6 t = t + x Next. 15. Questions: Loops. - PowerPoint PPT Presentation

Transcript of 11 – Array Variables

Page 1: 11 – Array Variables

Mark Dixon 1

11 – Array Variables

Page 2: 11 – Array Variables

Mark Dixon 2

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

executes? t = 0 For x = 4 To 6 t = t + x Next

15

Page 3: 11 – Array Variables

Mark Dixon 3

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

to change the number of faces:parFaces.innerHTML = parFaces.innerHTML + "<img src=face.jpg>"parFaces.innerHTML = parFaces.innerHTML + "<img src=face.jpg>"parFaces.innerHTML = parFaces.innerHTML + "<img src=face.jpg>"parFaces.innerHTML = parFaces.innerHTML + "<img src=face.jpg>"parFaces.innerHTML = parFaces.innerHTML + "<img src=face.jpg>"parFaces.innerHTML = parFaces.innerHTML + "<img src=face.jpg>"parFaces.innerHTML = parFaces.innerHTML + "<img src=face.jpg>"

Dim fFor f = 1 To 7 parFaces.innerHTML = parFaces.innerHTML + "<img src=face.jpg>"Next

Page 4: 11 – Array Variables

Mark Dixon 4

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 array elements– use for loops with arrays

Page 5: 11 – Array Variables

Mark Dixon 5

Inefficient Code• duplication in both branches of if

If weight > 2.2 Then x = 5 x = 5 Else x = 5 End If

• unused variable declarationsDim x Dim xDim y x = 5 x = 5

• redundant (nil effect) lines of code x = 23 x = 5 x = 5

Page 6: 11 – Array Variables

Mark Dixon 6

Example: German Numbers•User Requirements

– describe user's objectivesno mention of technology

•Software Requirements– Functional

• list facilities to be provided (often numbered)

– Non-functional• list desired characteristics

(often more subjective)

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-functionalshould be easy to use

Page 7: 11 – Array Variables

Mark Dixon 7

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

1 – eins2 – zwei3 – drei4 – vier5 – funf6 – sechs7 – sieben8 – acht9 – neun10 – zehn

Page 8: 11 – Array Variables

Mark Dixon 8

Random Numbers

Rnd()

0.0000

0.2563

0.5678

0.9999

10 *

0.000

2.563

5.678

9.999

Int(

0

2

5

9

)1 +

1

3

6

10

Page 9: 11 – Array Variables

Mark Dixon 9

Example: German Numbers v0Option Explicit Dim n

Sub Window_onLoad() Randomize End Sub

Sub btnStart_onClick() n = 1 + Int(10 *Rnd()) If n = 1 Then parQuest.innerText = "What is eins?" Else If n = 2 Then parQuest.innerText = "What is zwei?" Else If n = 3 Then parQuest.innerText = "What is drei?" Else If n = 4 Then parQuest.innerText = "What is vier?" Else If n = 5 Then parQuest.innerText = "What is funf?" Else If n = 6 Then parQuest.innerText = "What is sechs?" Else If n = 7 Then parQuest.innerText = "What is sieben?" Else If n = 8 Then parQuest.innerText = "What is acht?" Else If n = 9 Then parQuest.innerText = "What is neun?" Else If n = 10 Then parQuest.innerText = "What is zehn?" End If End If End If End If End If End If End If End If End If End If End Sub

Sub btnCheck_onClick() If CInt(txtAns.value) = n Then parRes.innerText = "Correct!" Else parRes.innerText = "Sorry, please try again." & n End If End Sub

Sub btnStart_onClick() n = 1 + Int(10 *Rnd()) If n = 1 Then parQuest.innerText = "What is eins?" Else If n = 2 Then parQuest.innerText = "What is zwei?" Else If n = 3 Then parQuest.innerText = "What is drei?" Else If n = 4 Then parQuest.innerText = "What is vier?" Else If n = 5 Then parQuest.innerText = "What is funf?" Else If n = 6 Then parQuest.innerText = "What is sechs?" Else If n = 7 Then parQuest.innerText = "What is sieben?" Else If n = 8 Then parQuest.innerText = "What is acht?" Else If n = 9 Then parQuest.innerText = "What is neun?" Else If n = 10 Then parQuest.innerText = "What is zehn?" End If End If End If End If End If End If End If End If End If End If End Sub

• Pick random number

• Use If statements– one inside another

Page 10: 11 – Array Variables

Mark Dixon 10

Array Variables (what)

Value

141

151

143

155

139

127

134

• multiple values– stored in single variable

Index

6

5

4

3

2

1

0

• index – identifies individual values (called elements)

• the value of element 3 is 155

• last element

Page 11: 11 – Array Variables

Mark Dixon 11

Arrays Variables

Page 12: 11 – Array Variables

Mark Dixon 12

Array Variables (Declaration)

• General syntax:

Dim varname(lastElement)

• Specific examples:

Dim HR(16) Dim x(8)

Page 13: 11 – Array Variables

Mark Dixon 13

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

Page 14: 11 – Array Variables

Mark Dixon 14

Questions: ArraysConsider 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)

• How many arrays are there?• Name the array(s).• What is in res after the code executes?

1

15.37

age

Page 15: 11 – Array Variables

Mark Dixon 15

Arrays: why? (declaration)

Dim Name(4)

Name(0) = "Bob" Name(1) = "Sally" Name(2) = "Jo" Name(3) = "Fred" Name(4) = "Alison"

Dim Name1Dim Name2Dim Name3Dim Name4Dim Name5 Name1 = "Bob" Name2 = "Sally" Name3 = "Jo" Name4 = "Fred" Name5 = "Alison"

Single array declaration5 variable declarations

Page 16: 11 – Array Variables

Mark Dixon 16

Arrays: why? (use)

Dim Num Num = Int(5 * Rnd()) Res = Name(Num)

Dim Num Num = Int(5 * Rnd()) If Num = 0 Then Res = Name1 ElseIf Num = 1 Then Res = Name2 ElseIf Num = 2 Then Res = Name3 ElseIf Num = 3 Then Res = Name4 Else Res = Name5 End If

Single line of code picks any element

Page 17: 11 – Array Variables

Mark Dixon 17

Example: German Numbers v1Option Explicit Dim Nums(10)Dim n Sub Window_onLoad() 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 Sub btnStart_onClick() n = 1 + Int(10 * Rnd()) parQuest.innerText = "What is " & Nums(n) & "?" End Sub Sub btnCheck_onClick() If CInt(txtAns.value) = n Then parRes.innerText = "Correct!" Else parRes.innerText = "Sorry, please try again." & n End If End Sub

ArrayDeclaration

ArrayUse

ArrayAssignment

Page 18: 11 – Array Variables

Mark Dixon 18

Example: German Numbers v0 vs. v1Option Explicit Dim Nums(10)Dim n

Sub window_onLoad() 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

Sub btnStart_onClick() n = 1 + Int(10 * Rnd()) parQuest.innerText = "What is " & Nums(n) & "?" End Sub

Sub btnCheck_onClick() If CInt(txtAns.value) = n Then parRes.innerText = "Correct!" Else parRes.innerText = "Sorry, please try again." & n End If End Sub

Option Explicit Dim n

Sub window_onLoad() Randomize End Sub

Sub btnStart_onClick() n = 1 + Int(10 * Rnd()) If n = 1 Then parQuest.innerText = "What is eins?" Else If n = 2 Then parQuest.innerText = "What is zwei?" Else If n = 3 Then parQuest.innerText = "What is drei?" Else If n = 4 Then parQuest.innerText = "What is vier?" Else If n = 5 Then parQuest.innerText = "What is funf?" Else If n = 6 Then parQuest.innerText = "What is sechs?" Else If n = 7 Then parQuest.innerText = "What is sieben?" Else If n = 8 Then parQuest.innerText = "What is acht?" Else If n = 9 Then parQuest.innerText = "What is neun?" Else If n = 10 Then parQuest.innerText = "What is zehn?" End If End If End If End If End If End If End If End If End If End If End Sub

Sub btnCheck_onClick() If CInt(txtAns.value) = n Then parRes.innerText = "Correct!" Else parRes.innerText = "Sorry, please try again." & n End If End Sub

v0 v1

27 lines

54 lines

Page 19: 11 – Array Variables

Mark Dixon 19

Error: Subscript Out of Range• Index too big/small

Option ExplicitDim x(3) x(0) = 9 x(1) = 5 x(2) = 21 x(3) = 23 x(4) = 12 x = 5

Page 20: 11 – Array Variables

Mark Dixon 20

Error: Type mismatch• index missing

Option ExplicitDim x(3) x(0) = 9 x(1) = 5 x(2) = 21 x(3) = 23 x = 5

Page 21: 11 – Array Variables

Mark Dixon 21

Error: Type mismatch• index given for non-array variable: b

Option ExplicitDim x(3)Dim b x(0) = 9 x(1) = 5 x(2) = 21 x(3) = 23 b(3) = 12

Page 22: 11 – Array Variables

Mark Dixon 22

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

Books with 56 elements

• Write a line of code that assigns the value 45 to the 18th element of the array.

• Write some code that makes the background red, but only when the 12th array element is larger than 6

Dim Books(55)

Books(17) = 45

If Books(11) >6 Then document.bgColor = "red"End If

Page 23: 11 – Array Variables

Mark Dixon 23

Example: Capital CitiesSPECIFICATION

• User Requirements – help people learn Capital Cities

• Software Requirements– Functional:

–ask user for capital of random country–user enter capital–check if correct

– Non-functionalshould be easy to use

Page 24: 11 – Array Variables

Mark Dixon 24

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

Country City

0 UK

1 France

2 Spain

3 Greece

0 London

1 Paris

2 Madrid

3 Athens

Page 25: 11 – Array Variables

Mark Dixon 25

Example: Capital Cities

Option Explicit Dim Country(4)Dim City(4)Dim Num

Sub window_onLoad() 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

Sub btnStart_onClick() Num = 1 + CInt(Rnd() * 3) parQuest.innerText = "What is the capital of " & Country(Num) & "?" End Sub

• How many array:– declarations?– assignments?

Page 26: 11 – Array Variables

Mark Dixon 26

Question: Arrays• Write a statement

that will decide whether the answer given by the user is correct:

Option Explicit Dim Country(4)Dim City(4)Dim Num

Sub window_onLoad() 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

Sub btnStart_onClick() Num = 1 + CInt(Rnd() * 3) parQuest.innerText = "What is the capital of " & Country(Num) & "?" End Sub

If txtNum.value = City(Num) Then

Page 27: 11 – Array Variables

Mark Dixon 27

Example: Drinks v1

Clears array

Displays array

Total of array

Searches array

Page 28: 11 – Array Variables

Mark Dixon 28

Example: Drinks v1 Dim Units(6) Dim curUnit

Sub window_onLoad() curUnit = 0 End Sub

Sub btnAdd_onClick() Units(curUnit) = txtUnit.value curUnit = curUnit + 1 End Sub

Sub btnClear_onClick() Units(0) = 0 Units(1) = 0 Units(2) = 0 Units(3) = 0 Units(4) = 0 Units(5) = 0 Units(6) = 0 curUnit = 0 End Sub

Sub btnShow_onClick() parRes.innerText = "" parRes.innerText = parRes.innerText & Units(0) & " " parRes.innerText = parRes.innerText & Units(1) & " " parRes.innerText = parRes.innerText & Units(2) & " " parRes.innerText = parRes.innerText & Units(3) & " " parRes.innerText = parRes.innerText & Units(4) & " " parRes.innerText = parRes.innerText & Units(5) & " " parRes.innerText = parRes.innerText & Units(6) End Sub

….

Page 29: 11 – Array Variables

Mark Dixon 29

Example: Drinks v1 ….

Sub btnTotal_onClick() Dim total 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) parRes.innerText = total End Sub

Sub btnFind_onClick() If txtUnit.value = Units(0) Then parRes.innerText = "Found in slot 0" ElseIf txtUnit.value = Units(1) Then parRes.innerText = "Found in slot 1" ElseIf txtUnit.value = Units(2) Then parRes.innerText = "Found in slot 2" ElseIf txtUnit.value = Units(3) Then parRes.innerText = "Found in slot 3" ElseIf txtUnit.value = Units(4) Then parRes.innerText = "Found in slot 4" ElseIf txtUnit.value = Units(5) Then parRes.innerText = "Found in slot 5" ElseIf txtUnit.value = Units(6) Then parRes.innerText = "Found in slot 6" Else parRes.innerText = "Not Found" End If End Sub

Page 30: 11 – Array Variables

Mark Dixon 30

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

Page 31: 11 – Array Variables

Mark Dixon 31

Example: Drinks v2 (Reset)• Use loop counter variable (i) as array index:

Page 32: 11 – Array Variables

Mark Dixon 32

Example: Drinks v2 (Total)

Page 33: 11 – Array Variables

Mark Dixon 33

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

Page 34: 11 – Array Variables

Mark Dixon 34

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

Page 35: 11 – Array Variables

Mark Dixon 35

Tutorial Exercise: Drinks• Task 1: Get the Drinks v2 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).