21
Mark Dixon, SoCCE SOFT 131 Page 1 07 – Arrays, & Constants

07 – Arrays, & Constants

  • Upload
    phuoc

  • View
    38

  • Download
    0

Embed Size (px)

DESCRIPTION

07 – Arrays, & Constants. In-class Test Results. Score (max 25). Student Number. Session Aims & Objectives. Aims To introduce the main concepts involved in handling more complex (multi valued) data Objectives, by end of this week’s sessions, you should be able to: - PowerPoint PPT Presentation

Citation preview

Page 1: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 1

07 – Arrays, & Constants

Page 2: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 2

In-class Test Results

StudentNumber

Score(max 25)

10055997 22385517 22373691 22

10010758 2210029161 21

10004337 17 10020412 2110015690 17 367652 21

304999 17 10019846 2110003421 17 10013492 21

333570 17 10015185 2110010613 17 10010900 20

381357 16 377132 2010015570 12 10046095 14 10005427 16 10048048 20

350369 12 315132 14 10049108 16 383108 20322253 12 363217 14 282204 16 10000274 19

10047406 7 10029095 9 365617 11 10042486 13 273616 16 607598 1910015422 6 778317 8 357730 11 381388 13 265339 15 10011926 19

280841 5 373277 8 324635 11 10038146 13 368747 15 10019259 18343867 3 358391 8 10020461 10 10009451 13 351734 15 258921 18

<30% (fail) <40% (comp) <50% (3) <60% (2:2) <70% (2:1) >=70% (1)

Page 3: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 3

Session Aims & Objectives• Aims

– To introduce the main concepts involved in handling more complex (multi valued) data

• Objectives,by end of this week’s sessions, you should be able to:

– declare, assign values to, and use:• constants, and• arrays,

Page 4: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 4

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:

–store text of German numbers 1-10

–select one at random–present text to user–user enter digits

– Non-functionalshould be easy to use

Page 5: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 5

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

Page 6: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 6

Array Variables (how)• Declaration:

Dim varname(last)e.g. Dim HR(16) Dim x(8)

• Assignment: arrayname(index) = valuee.g. HR(0) = 134 HR(5) = 151 x(5) = 23.87 x(7) = 189.2516

Page 7: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 7

Arrays Animation

Page 8: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 8

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 9: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 9

Arrays: why? (use)

Dim Num Num = 2 Res = Name(Num)

Dim Num Num = 2 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

Single line of code picks any element

Page 10: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 10

Example: German Numbers<html> <head> <title>German Numbers</title> <script language=vbscript> Option Explicit Dim Nums(10) Dim Num

Sub Window_OnLoad() 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" Randomize End Sub

Sub btnStart_OnClick() Num = 1 + CInt(Rnd() * 9) lblQuest.innerText = "What is " & Nums(Num) & "?" End Sub </script> </head>

<body> <p ID=lblQuest> <p><input type=button id=btnStart value=Start> <input type=text id=txtNum> <input type=button id=btnCheck value=Check> </body></html>

ArrayDeclaration

ArrayUse

ArrayAssignment

Page 11: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 11

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

Books with 56 elements

• Write a line of code that assigns to 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 Then document.bgcolor = "red"End If

Page 12: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 12

Example: Capital Cities<html> <head> <title>Caplital Cities</title> <script language=vbscript> 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) lblQuest.innerText = "What is the capital of " & Country(Num) & "?" End Sub </script> </head>

<body> <p ID=lblQuest>Please press Start button to begin. <p><input type=button id=btnStart value=Start> <input type=text id=txtNum> <input type=button id=btnCheck value=Check> </body></html>

Page 13: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 13

Script Debugging• Labs have script debugging disabled

• Tools menu– Internet Options

• Advanced tab– Disable Script

Debugging

Page 14: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 14

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>

Page 15: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 15

Constants• similar to variable

– value given in declaration

• however value can’t be changed

• useful for removing 'magic numbers'• declaration syntax:

• name used to represent literal value

Const constantname = expression

Page 16: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 16

Constants: Example• Some numbers

represent thesame thing– e.g. the last element

of the array

• changes– take time– could make mistake

Option Explicit Dim Nums(10) Dim Num

Sub Window_OnLoad() 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" Randomize End Sub

Sub btnStart_OnClick() Num = 1 + CInt(Rnd() * 9) lblQuest.innerText = "What is " & Nums(Num) & "?" End Sub

Page 17: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 17

Constants: Example• replace

literal numbers– with constant

• name representsvalue

• only need to changeconstant

Option Explicit Const Last = 10 Dim Nums(Last) Dim Num

Sub Window_OnLoad() 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" Randomize End Sub

Sub btnStart_OnClick() Num = 1 + CInt(Rnd() * (Last - 1)) lblQuest.innerText = "What is " & Nums(Num) & "?" End Sub

Declaration ofConstant

Use ofConstant

Page 18: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 18

Error: Illegal assignment<HTML> <HEAD> <TITLE></TITLE> <script language=vbscript> Const hi=4 Dim x(hi) x(1) = 5 x(2) = 12 x(3) = 9 hi=6 </script> </HEAD> <BODY> </BODY></HTML>

Page 19: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 19

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

MaxGoes with a value of 3

• Write some code that makes the background red, but only when a variable called Goes is larger than MaxGoes

Const MaxGoes = 3

If Goes > MaxGoes Then document.bgcolor = "red"End If

Page 20: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 20

Tutorial Exercise: German Numbers

• Task 1: Complete German Numbers Example from lecture, using constants.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 so that it plays appropriate sounds when the user gets the answer right/wrong

• Task 4: Modify your page to allow the user 3 attempts only.

Page 21: 07 – Arrays, & Constants

Mark Dixon, SoCCE SOFT 131 Page 21

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

lecture, adding some more cities, and using constants.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 so that it plays appropriate sounds when the user gets the answer right/wrong

• Task 4: Modify your page to allow the user 3 attempts only.

• Task 5: Modify your page so that it is case in-sensitive (i.e. user can type upper or lower case)