09 arrays n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
09 – Arrays PowerPoint Presentation
Download Presentation
09 – Arrays

Loading in 2 Seconds...

play fullscreen
1 / 30

09 – Arrays - PowerPoint PPT Presentation


  • 68 Views
  • Uploaded on

09 – Arrays. 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.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

09 – Arrays


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
questions loops
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 loops1
Questions: Loops
  • Simplify the following code, so that it 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 f

For f = 1 To 7

parFaces.innerHTML = parFaces.innerHTML + "<img src=face.jpg>"

Next

session aims objectives
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
example german numbers
Example: German Numbers
  • 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
  • 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)
example german numbers1
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

example german numbers v0
Example: German Numbers v0

Sub btnStart_OnClick()

n = 1 + CInt(Rnd() * 9)

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

<script language=vbscript>

Option Explicit

Dim n

Sub Window_OnLoad()

Randomize

End Sub

Sub btnStart_OnClick()

n = 1 + CInt(Rnd() * 9)

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

</script>

  • Pick random number
  • Use If statements
    • one inside another
array variables what
Array Variables (what)

Index

Value

0

134

1

127

  • index – identifies individual values (called elements)

2

139

3

155

4

143

  • the value of element 3 is 155

5

151

6

141

  • multiple values– stored in single variable
  • last element
array variables declaration
Array Variables (Declaration)
  • General syntax:Dimvarname(lastElement)
  • Specific examples: Dim HR(16) Dim x(8)
array variables assignment
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
arrays why declaration
Arrays: why? (declaration)

5 variable declarations

Single array declaration

Dim Name1

Dim Name2

Dim Name3

Dim Name4

Dim Name5

Name1 = "Bob"

Name2 = "Sally"

Name3 = "Jo"

Name4 = "Fred"

Name5 = "Alison"

Dim Name(4)

Name(0) = "Bob"

Name(1) = "Sally"

Name(2) = "Jo"

Name(3) = "Fred"

Name(4) = "Alison"

arrays why use
Arrays: why? (use)

Dim Num

Num = 1 + Rnd() * 3

If Num = 1 Then

Res = Name1

ElseIf Num = 2 Then

Res = Name2

ElseIf Num = 3 Then

Res = Name3

ElseIf Num = 4 Then

Res = Name4

Else

Res = Name5

End If

Dim Num

Num = 1 + Rnd() * 3

Res = Name(Num)

Single line of code picks any element

example german numbers v1
Example: German Numbers v1

<script language=vbscript>

Option 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 + CInt(Rnd() * 9)

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

</script>

Array

Declaration

Array

Assignment

Array

Use

example german numbers v0 vs v1
Example: German Numbers v0 vs. v1

v0

<script language=vbscript>

Option Explicit

Dim n

Sub Window_OnLoad()

Randomize

End Sub

Sub btnStart_OnClick()

n = 1 + CInt(Rnd() * 9)

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

</script>

<script language=vbscript>

Option 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 + CInt(Rnd() * 9)

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

</script>

v1

27 lines

54 lines

error subscript out of range
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>

</BODY>

</HTML>

questions arrays
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(56)

Books(18) = 45

If Books(12) >6 Thendocument.bgColor = "red"

End If

example 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-functionalshould be easy to use
example capital cities1
Example: Capital Cities
  • How many array:
    • declarations?
    • assignments?

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

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

Country

City

question arrays
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

example drinks v1
Example: Drinks v1

Total of array

Clears array

Displays array

Searches array

example drinks v11
Example: Drinks v1

<script language=VBScript>

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

lblRes.innerText = ""

lblRes.innerText = lblRes.innerText & Units(0) & " "

lblRes.innerText = lblRes.innerText & Units(1) & " "

lblRes.innerText = lblRes.innerText & Units(2) & " "

lblRes.innerText = lblRes.innerText & Units(3) & " "

lblRes.innerText = lblRes.innerText & Units(4) & " "

lblRes.innerText = lblRes.innerText & Units(5) & " "

lblRes.innerText = lblRes.innerText & Units(6)

End Sub

….

example drinks v12
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)

lblRes.innerText = total

End Sub

Sub btnFind_OnClick()

If txtUnit.value = Units(0) Then

lblRes.innerText = "Found in slot 0"

ElseIf txtUnit.value = Units(1) Then

lblRes.innerText = "Found in slot 1"

ElseIf txtUnit.value = Units(2) Then

lblRes.innerText = "Found in slot 2"

ElseIf txtUnit.value = Units(3) Then

lblRes.innerText = "Found in slot 3"

ElseIf txtUnit.value = Units(4) Then

lblRes.innerText = "Found in slot 4"

ElseIf txtUnit.value = Units(5) Then

lblRes.innerText = "Found in slot 5"

ElseIf txtUnit.value = Units(6) Then

lblRes.innerText = "Found in slot 6"

Else

lblRes.innerText = "Not Found"

End If

End Sub

</script>

array algorithms
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
example drinks v2 reset
Example: Drinks v2 (Reset)
  • Use loop counter variable (i) as array index:
tutorial exercise german numbers
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
tutorial exercise capital cities
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
tutorial exercise drinks
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).