1 / 21

07 – Arrays, & Constants

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:

phuoc
Download Presentation

07 – Arrays, & Constants

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 07 – Arrays, & Constants

  2. In-class Test Results Score (max 25) StudentNumber

  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,

  4. Example: German Numbers • 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 • 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)

  5. Array Variables (what) • multiple values – stored in single variable • index – identifies individual values (called elements) • the value of element 3 is 155

  6. Array Variables (how) • Declaration:Dimvarname(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

  7. Arrays Animation

  8. 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”

  9. Arrays: why? (use) 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 Dim Num Num = 2 Res = Name(Num) Single line of code picks any element

  10. Example: German Numbers Array Declaration Array Assignment Array Use <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>

  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 Thendocument.bgcolor = "red" End If

  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>

  13. Script Debugging • Labs have script debugging disabled • Tools menu • Internet Options • Advanced tab • Disable ScriptDebugging

  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>

  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 Constconstantname=expression

  16. Constants: Example 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 • Some numbersrepresent thesame thing • e.g. the last elementof the array • changes • take time • could make mistake

  17. Constants: Example Declaration ofConstant Use ofConstant 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 • replaceliteral numbers • with constant • name representsvalue • only need to changeconstant

  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>

  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

  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.

  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)

More Related