Download presentation
Presentation is loading. Please wait.
Published byLouisa Davidson Modified over 8 years ago
1
Mark Dixon, SoCCE SOFT 136Page 1 08 – Arrays & Structures
2
Mark Dixon, SoCCE SOFT 136Page 2 Session Aims & Objectives Aims –To introduce the main concepts involved in handling more complex (multi valued) data, i.e. arrays and structures Objectives, by end of this week’s sessions, you should be able to: –declare, use, and assign values to arrays –declare, use, and assign values to structures –create and use ListBoxes –create and use an array of structures
3
Mark Dixon, SoCCE SOFT 136Page 3 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) 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-functional should be easy to use
4
Mark Dixon, SoCCE SOFT 136Page 4 Array Variables: what multiple values (of same type) – stored in single variable index – identified individual values (called elements) the value of element 3 is 155 IndexValue 0134 1127 2139 3155 4143 5151 6141
5
Mark Dixon, SoCCE SOFT 136Page 5 Array Variables: how Declaration: Dim varname([first] To [last])[As type] e.g. Dim HR(0 To 6) As Long Dim x(4 To 8) As Double Assignment: arrayname(index) = value e.g. HR(0) = 134 HR(5) = 151 x(5) = 23.87 x(7) = 189.2516
6
Mark Dixon, SoCCE SOFT 136Page 6 Arrays Animation: how
7
Mark Dixon, SoCCE SOFT 136Page 7 Array Variables: why? Dim Person(4) As String Person(0) = “Bob” Person(1) = “Sally” Person(2) = “Jo” Person(3) = “Fred” Person(4) = “Alison” Dim Name1 As String Dim Name2 As String Dim Name3 As String Dim Name4 As String Dim Name5 As String Name1 = “Bob” Name2 = “Sally” Name3 = “Jo” Name4 = “Fred” Name5 = “Alison” Single array declaration5 variable declarations
8
Mark Dixon, SoCCE SOFT 136Page 8 Array Variables: why? Dim Num As Long Num = 2 Res = Name(Num) Dim Num As Long 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
9
Mark Dixon, SoCCE SOFT 136Page 9 Array Variables: why? Most powerful feature of arrays: –can use for loop to move through all array elements easily Dim x(3 To 6) As Integer x(3) = 12 x(4) = 12 x(5) = 12 x(6) = 12 Dim x(3 To 6) As Integer Dim i As Integer For i = 3 To 6 x(i) = 12 Next
10
Mark Dixon, SoCCE SOFT 136Page 10 Arrays Animation: why?
11
Mark Dixon, SoCCE SOFT 136Page 11 Example: German Numbers Option Explicit Dim Nums(10) Dim Num Private Sub Form_Load() 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 Private Sub btnStart_Click() Num = 1 + CInt(Rnd() * 9) lblQuest.Caption = "What is " & Nums(Num) & "?" End Sub Array Declaration Array Use Array Assignment
12
Mark Dixon, SoCCE SOFT 136Page 12 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 'Goldeneye' to the 18 th element of the array. Write some code that makes the background red, but only when the 12 th array element is larger than 6 Dim Books(56) As String Books(18) = "Goldeneye" If Books(12) >6 Then Me.BackColor = vbRed End If
13
Mark Dixon, SoCCE SOFT 136Page 13 Example: Cities v1 Option Explicit Dim Country(4) As String Dim City(4) As String Dim Num As Long Private Sub Form_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 Private Sub btnStart_Click() Num = 1 + CInt(Rnd() * 3) lblQuest.Caption = "What is the capital of " & Country(Num) & "?" End Sub
14
Mark Dixon, SoCCE SOFT 136Page 14 Example: Heart Rate v1 Option Explicit Dim HR(0 To 6) As Long Private Sub Form_Load() HR(0) = 134 HR(1) = 127 HR(2) = 139 HR(3) = 155 HR(4) = 143 HR(5) = 151 HR(6) = 141 End Sub Private Sub btnDraw_Click() Dim i As Long picMain.Cls For i = 0 To 6 picMain.Line -(i * 500, HR(i) * 10) Next End Sub Heart Rate
15
Mark Dixon, SoCCE SOFT 136Page 15 Example Confidential
16
Mark Dixon, SoCCE SOFT 136Page 16 List Box Control Used to give user multiple options (can change) Methods –AddItem: adds a new item to the list lstDrinks.AddItem "Coke" –RemoveItem: removes an existing item from the list lstDrinks.RemoveItem 2 –Clear: removes all existing items from the list lstDrinks.Clear Properties: –ListIndex: index of currently selected item, or -1 if nothing is selected –NewIndex: index of last item added –List: stores text of items in list –ItemData: stores (invisible) number for each item in list
17
Mark Dixon, SoCCE SOFT 136Page 17 Example: Drinks (Design) User Interface Functionality Trigger (when)Actions (what) load event of Form object Fill list with 8 drinks (as per following table), update list count. click event of Drinks list Display current drink's index (in index label), name (in name text box), & cost of drink (in cost text box). click event of Remove button Remove currently selected item from list, update list count. click event of Reset button Restore initial 8 drinks (as per following table), update list count. click event of Exit button End program, close window.
18
Mark Dixon, SoCCE SOFT 136Page 18 Example: Drinks (UI design) IndexText (List)Value (ItemData) 0Coke90 1Lemonade80 2Cider160 3Larger170 4Bitter180 5Whisky190 6Brandy170 7Vodka175 Only the text is displayed on the form.
19
Mark Dixon, SoCCE SOFT 136Page 19 Structures (what) multiple values (of different type) – stored in single variable each element identified by name the value of the firstname element is 'Bob' ElementValue SurnameSmith FirstnameBob GenderMale Age21
20
Mark Dixon, SoCCE SOFT 136Page 20 Structures (how) Declaration: Type typename elementname As type... End Type e.g. Type Student Surname As String FirstName As String Gender As Boolean End Type Assignment: varname.element = value e.g. Dim curStu As Student curStu.Surname = "Smith" curStu.Firstname = "Bob"
21
Mark Dixon, SoCCE SOFT 136Page 21 Structures Groups different types of data Declaration of type: Type TAnimal Name As String Species As String Gender As Boolean End Type Use of type (in variable declaration): Dim myPet As TAnimal Change value of MyPet’s name: myPet.Name = "George"
22
Mark Dixon, SoCCE SOFT 136Page 22 Structures Animation
23
Mark Dixon, SoCCE SOFT 136Page 23 Question: Structures Create a record definition for: –Estate agents: House details (house num., street, price) Write code that will: –Create a variable of the above type –Put data into the elements of that variable Type THouse Num As Long Street As String Price As Double End Type Dim myHouse As THouse myHouse.Street = "Portland Square"
24
Mark Dixon, SoCCE SOFT 136Page 24 Question: Structures Create a record definition for: –Police stolen car register: Car details (Reg. number, colour, model) Write code that will: –Create a variable of the above type –Put data into the elements of that variable Type TCar RegNum As String Colour As String Model As String End Type Dim myCar As TCar myCar.RegNum = "GH23 XRB"
25
Mark Dixon, SoCCE SOFT 136Page 25 Example: Cities v1 Option Explicit Dim Country(1 To 4) As String Dim City(1 To 4) As String Dim Num As Long Private Sub Form_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 Private Sub btnStart_Click() Num = 1 + CInt(Rnd() * 3) lblQuest.Caption = "What is the capital of " & Country(Num) & "?" End Sub
26
Mark Dixon, SoCCE SOFT 136Page 26 We used an array for each piece of information: Dim Country(1 To 4) As String Dim City(1 To 4) As String 2 separate arrays Example: Cities v1 (Data Design) Country: stringCity: string UK 12341234 France Spain Greece London 12341234 Paris Madrid Athens
27
Mark Dixon, SoCCE SOFT 136Page 27 Difficulty This design works However, if –more fields (elements) were implemented, and –more complex operations were added the code would become difficult to manage –having several separate arrays Arrays allow data to be grouped –however, arrays must be homogenous (same data type) it would be useful to be able to group different (heterogeneous) types of data in an array
28
Mark Dixon, SoCCE SOFT 136Page 28 Array of Structures Animation
29
Mark Dixon, SoCCE SOFT 136Page 29 Array of Structures Can also have arrays of structures: Dim MyPets(1 To 5) As TAnimal Change value: MyPets(3).Name = "George" Change value using index variable: ind = 2 MyPets(ind).Name = "Fred"
30
Mark Dixon, SoCCE SOFT 136Page 30 Example: Cities v2 (Data Design) We can now use a single array that uses a user defined type/record/structure: Country: string Countries: tCountry City: string each row is a tCountry makes it easier to get details of single country UK 12341234 France Spain Greece London Paris Madrid Athens
31
Mark Dixon, SoCCE SOFT 136Page 31 Example: Cities v2 Option Explicit Private Type tCountry Country As String City As String End Type Dim Countries(1 To 4) As tCountry Dim Num As Long Private Sub Form_Load() Countries(1).Country = "UK" Countries(1).City = "London" Countries(2).Country = "France" Countries(2).City = "Paris" Countries(3).Country = "Spain" Countries(3).City = "Madrid" Countries(4).Country = "Greece" Countries(4).City = "Athens" Randomize End Sub Private Sub btnStart_Click() Num = 1 + CInt(Rnd() * 3) lblQuest.Caption = "What is the capital of " & Countries(Num).Country & "?" End Sub
32
Mark Dixon, SoCCE SOFT 136Page 32 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.
33
Mark Dixon, SoCCE SOFT 136Page 33 Tutorial Exercise: Cities v1 Task 1: Complete the Capital Cities Example (v1) 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)
34
Mark Dixon, SoCCE SOFT 136Page 34 Tutorial Exercise: Hear Rate Task 1: Get the Heart Rate example (from the lecture) working. Task 2: Modify your code – use constants to remove all magic numbers. Task 3: Modify your code – increase the number of data items. Task 4: Modify your code – to prevent the initial vertical line from being drawn for element 0 of the array. Task 5: Add a facility to calculate an average (mean) heart rate for the data you’ve. Task 6: Add a facility to display the average as a horizontal red line across the data.
35
Mark Dixon, SoCCE SOFT 136Page 35 Tutorial Exercise: Cities v2 Task 1: Modify your Capital Cities program so that it uses an array of structures (as per the lecture example v2).
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.