08 Arrays Structures Mark Dixon So CCE SOFT

  • Slides: 35
Download presentation
08 – Arrays & Structures Mark Dixon, So. CCE SOFT 136 Page 1

08 – Arrays & Structures Mark Dixon, So. CCE SOFT 136 Page 1

Session Aims & Objectives • Aims – To introduce the main concepts involved in

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 List. Boxes – create and use an array of structures Mark Dixon, So. CCE SOFT 136 Page 2

Example: German Numbers • User Requirements SPECIFICATION – describe user's objectives no mention of

Example: German Numbers • User Requirements SPECIFICATION – describe user's objectives no mention of technology • User Requirements – help people learn German numbers 1 - 10 • Software Requirements – Functional • list facilities to be provided (often numbered) – Non-functional • list desired characteristics (often more subjective) Mark Dixon, So. CCE SOFT 136 – 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 Page 3

Array Variables: what • multiple values (of same type) – stored in single variable

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 Mark Dixon, So. CCE SOFT 136 Index Value 0 134 1 127 2 139 3 155 4 143 5 151 6 141 Page 4

Array Variables: how • Declaration: Dim varname([first] To [last])[As type] e. g. Dim HR(0

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 Mark Dixon, So. CCE SOFT 136 Page 5

Arrays Animation: how Mark Dixon, So. CCE SOFT 136 Page 6

Arrays Animation: how Mark Dixon, So. CCE SOFT 136 Page 6

Array Variables: why? 5 variable declarations Single array declaration Dim Name 1 As String

Array Variables: why? 5 variable declarations Single array declaration Dim Name 1 As String Dim Name 2 As String Dim Name 3 As String Dim Name 4 As String Dim Name 5 As String Name 1 = “Bob” Name 2 = “Sally” Name 3 = “Jo” Name 4 = “Fred” Name 5 = “Alison” Dim Person(4) As String Mark Dixon, So. CCE Person(0) Person(1) Person(2) Person(3) Person(4) SOFT 136 = = = “Bob” “Sally” “Jo” “Fred” “Alison” Page 7

Array Variables: why? Dim Num As Long Num = 2 If Num = 1

Array Variables: why? Dim Num As Long Num = 2 If Num = 1 Then Res = Name 1 Else. If Num = 2 Then Res = Name 2 Else. If Num = 3 Then Res = Name 3 Else. If Num = 4 Then Res = Name 4 Else Res = Name 5 End If Mark Dixon, So. CCE SOFT 136 Dim Num As Long Num = 2 Res = Name(Num) Single line of code picks any element Page 8

Array Variables: why? • Most powerful feature of arrays: – can use for loop

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 Dim i As Integer x(3) = 12 x(4) = 12 x(5) = 12 x(6) = 12 Mark Dixon, So. CCE For i = 3 To 6 x(i) = 12 Next SOFT 136 Page 9

Arrays Animation: why? Mark Dixon, So. CCE SOFT 136 Page 10

Arrays Animation: why? Mark Dixon, So. CCE SOFT 136 Page 10

Example: German Numbers Option Explicit Dim Nums(10) Dim Num Array Declaration Array Assignment Array

Example: German Numbers Option Explicit Dim Nums(10) Dim Num Array Declaration Array Assignment Array Use Mark Dixon, So. CCE 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 btn. Start_Click() Num = 1 + CInt(Rnd() * 9) lbl. Quest. Caption = "What is " & Nums(Num) End Sub SOFT 136 Page 11

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

Questions: Arrays • Write a line of code that declares an array called Books with 56 elements Dim Books(56) As String • Write a line of code that assigns to value 'Goldeneye' to the 18 th element of the array. Books(18) = "Goldeneye" • Write some code that makes the background red, but only when the 12 th array element is larger than 6 If Books(12) >6 Then Me. Back. Color = vb. Red End If Mark Dixon, So. CCE SOFT 136 Page 12

Example: Cities v 1 Option Explicit Dim Country(4) As String Dim City(4) As String

Example: Cities v 1 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 Mark Dixon, So. CCE Private Sub btn. Start_Click() Num = 1 + CInt(Rnd() * 3) lbl. Quest. Caption = "What is the capital of " & Country(Num) & End Sub SOFT 136 Page 13

Example: Heart Rate v 1 Option Explicit Dim HR(0 To 6) As Long Private

Example: Heart Rate v 1 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 Heart Rate Mark Dixon, So. CCE SOFT 136 Private Sub btn. Draw_Click() Dim i As Long pic. Main. Cls For i = 0 To 6 pic. Main. Line -(i * 500, HR(i) * 10) Next Page 14 End Sub

Example o C Mark Dixon, So. CCE l ia t n e d i

Example o C Mark Dixon, So. CCE l ia t n e d i f n SOFT 136 Page 15

List Box Control • Used to give user multiple options (can change) • Methods

List Box Control • Used to give user multiple options (can change) • Methods – Add. Item: adds a new item to the list lst. Drinks. Add. Item "Coke" – Remove. Item: removes an existing item from the list lst. Drinks. Remove. Item 2 – Clear: removes all existing items from the list lst. Drinks. Clear • Properties: – List. Index: index of currently selected item, or -1 if nothing is selected – New. Index: index of last item added – List: stores text of items in list – Item. Data: stores (invisible) number for each item in list Mark Dixon, So. CCE SOFT 136 Page 16

Example: Drinks (Design) User Interface Functionality Trigger (when) Actions (what) load event of Form

Example: Drinks (Design) User Interface Functionality Trigger (when) Actions (what) load event of Form object click event of Drinks list Fill list with 8 drinks (as per following table), update list count. Display current drink's index (in index label), name (in name text box), & cost of drink (in cost text box). click event of Remove currently selected item Remove button from list, update list count. click event of Restore initial 8 drinks (as per Reset button following table), update list count. click event of Exit End program, close window. button Mark Dixon, So. CCE SOFT 136 Page 17

Example: Drinks (UI design) Index Text (List) Value (Item. Data) 0 Coke 90 1

Example: Drinks (UI design) Index Text (List) Value (Item. Data) 0 Coke 90 1 Lemonade 80 2 3 4 5 6 7 Cider Larger Bitter Whisky Brandy Vodka 160 170 180 190 175 Only the text is displayed on the form. Mark Dixon, So. CCE SOFT 136 Page 18

Structures (what) • multiple values (of different type) – stored in single variable Element

Structures (what) • multiple values (of different type) – stored in single variable Element • each element identified by Surname Value Smith Firstname Bob • the value of the firstname element is 'Bob' Mark Dixon, So. CCE SOFT 136 Gender Male Age 21 Page 19

Structures (how) • Declaration: Type typename elementname As type. . . End Type e.

Structures (how) • Declaration: Type typename elementname As type. . . End Type e. g. Type Student Surname As String First. Name As String Gender As Boolean End Type • Assignment: varname. element = value e. g. Dim cur. Stu As Student cur. Stu. Surname = "Smith" cur. Stu. Firstname = "Bob" Mark Dixon, So. CCE SOFT 136 Page 20

Structures • Groups different types of data • Declaration of type: Type TAnimal Name

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 my. Pet As TAnimal • Change value of My. Pet’s name: my. Pet. Name = "George" Mark Dixon, So. CCE SOFT 136 Page 21

Structures Animation Mark Dixon, So. CCE SOFT 136 Page 22

Structures Animation Mark Dixon, So. CCE SOFT 136 Page 22

Question: Structures • Create a record definition for: – Estate agents: House details (house

Question: Structures • Create a record definition for: – Estate agents: House details (house num. , street, price) Type THouse Num As Long Street As String Price As Double End Type • Write code that will: – Create a variable of the above type Dim my. House As THouse – Put data into the elements of that variable my. House. Street = "Portland Square" Mark Dixon, So. CCE SOFT 136 Page 23

Question: Structures • Create a record definition for: – Police stolen car register: Car

Question: Structures • Create a record definition for: – Police stolen car register: Car details (Reg. number, colour, model) Type TCar Reg. Num As String Colour As String Model As String End Type • Write code that will: – Create a variable of the above type Dim my. Car As TCar – Put data into the elements of that variable my. Car. Reg. Num = "GH 23 XRB" Mark Dixon, So. CCE SOFT 136 Page 24

Example: Cities v 1 Option Explicit Dim Country(1 To 4) As String Dim City(1

Example: Cities v 1 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 btn. Start_Click() Num = 1 + CInt(Rnd() * 3) lbl. Quest. Caption = "What is the capital of " & Country(Num) & "? " End Sub Mark Dixon, So. CCE SOFT 136 Page 25

Example: Cities v 1 (Data Design) • We used an array for each piece

Example: Cities v 1 (Data Design) • We used an array for each piece of information: Dim Country(1 To 4) As String Dim City(1 To 4) As String Country: string 1 2 3 4 UK France Spain Greece City: string 1 2 3 4 London Paris Madrid Athens • 2 separate arrays Mark Dixon, So. CCE SOFT 136 Page 26

Difficulty • This design works • However, if – more fields (elements) were implemented,

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 Mark Dixon, So. CCE SOFT 136 Page 27

Array of Structures Animation Mark Dixon, So. CCE SOFT 136 Page 28

Array of Structures Animation Mark Dixon, So. CCE SOFT 136 Page 28

Array of Structures • Can also have arrays of structures: Dim My. Pets(1 To

Array of Structures • Can also have arrays of structures: Dim My. Pets(1 To 5) As TAnimal • Change value: My. Pets(3). Name = "George" • Change value using index variable: ind = 2 My. Pets(ind). Name = "Fred" Mark Dixon, So. CCE SOFT 136 Page 29

Example: Cities v 2 (Data Design) • We can now use a single array

Example: Cities v 2 (Data Design) • We can now use a single array that uses a user defined type/record/structure: Countries: t. Country 1 2 3 4 UK France Spain Greece London Paris Madrid Athens each row is a t. Country City: string Country: string • makes it easier to get details of single country Mark Dixon, So. CCE SOFT 136 Page 30

Example: Cities v 2 Option Explicit Private Type t. Country As String City As

Example: Cities v 2 Option Explicit Private Type t. Country As String City As String End Type Dim Countries(1 To 4) As t. Country 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 btn. Start_Click() Num = 1 + CInt(Rnd() * 3) lbl. Quest. Caption = "What is the capital of " & Countries(Num). Country & "? " End Sub Mark Dixon, So. CCE SOFT 136 Page 31

Tutorial Exercise: German Numbers • Task 1: Complete German Numbers Example from lecture, using

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. Mark Dixon, So. CCE SOFT 136 Page 32

Tutorial Exercise: Cities v 1 • Task 1: Complete the Capital Cities Example (v

Tutorial Exercise: Cities v 1 • Task 1: Complete the Capital Cities Example (v 1) 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 insensitive (i. e. user can type upper or lower case) Mark Dixon, So. CCE SOFT 136 Page 33

Tutorial Exercise: Hear Rate • Task 1: Get the Heart Rate example (from the

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. Mark Dixon, So. CCE SOFT 136 Page 34

Tutorial Exercise: Cities v 2 • Task 1: Modify your Capital Cities program so

Tutorial Exercise: Cities v 2 • Task 1: Modify your Capital Cities program so that it uses an array of structures (as per the lecture example v 2). Mark Dixon, So. CCE SOFT 136 Page 35