# Multi-dimensional Arrays by yurtgc548

VIEWS: 8 PAGES: 21

• pg 1
```									Multi-dimensional Arrays

CS0004
Lecture 16
Chapter 7.5
Sorting

   Sorting a list
– Numbers
– Strings
– Anything we can compare
   Bubble Sort
– Compare pairs
– Move items to correct position
– Repeat for n-1 times
Example
Pebbles   Barney    Barney    Barney    Barney
Barney    Pebbles   Pebbles   Pebbles   Pebbles
First
Pass    Wilma     Wilma     Wilma     Fred      Fred
Fred      Fred      Fred      Wilma     Dino
Dino      Dino      Dino      Dino      Wilma

Barney    Barney    Barney    Barney
Pebbles   Pebbles   Fred      Fred
Second
Pass    Fred      Fred      Pebbles   Dino
Dino      Dino      Dino      Pebbles
Wilma     Wilma     Wilma     Wilma
Example
Barney    Barney    Barney
Fred      Fred      Dino
Third
Pass     Dino      Dino      Fred
Pebbles   Pebbles   Pebbles
Wilma     Wilma     Wilma

Barney    Barney
Dino      Dino
Fourth
Pass    Fred      Fred
Pebbles   Pebbles
Wilma     Wilma
Write the code
Loop over the size of the list minus 1
Then loop from the start of the list to the end
minus the number of times we have already
looped
If item is out of order with previous
swap them
End If
End Loop
End Loop
Sort Order

   Ascending
– 1,2,3,4,8,10,200,304,650
   Descending
– 605,304,200,10,8,4,3,2,1
   Currently Bubble Sort sorts ascending,
how do you make it descending?
Shell Sort

   Much more complex
– General idea
• Compare items at further distance (known as
the gap) and swap like bubble sort
• Then reduce the gap size by 1 and do it again
• Continue until the gap size is set to 0, meaning
we are comparing neighbors (as we do in
bubble sort)
– Not required to understand, details in book
Two-Dimensional Arrays

 One-Dimensional arrays store a single
list of items of the same type
 Two-Dimensional arrays store a table of
items of the same type
Two-Dimensional Arrays
Dim rm(3, 3) As Double
rm(0,0)=0, rm(0,1)=2054, rm(0,2)=802

Chicago   LA     NY     Philly

Chicago     0       2054   802    738
LA       2054       0    2786   2706

NY       802      2786    0     100

Philly    738      2706   100      0
Two-Dimensional Arrays
Dim rm(3, 3) As Double
rm(0,0)=0, rm(0,1)=2054, rm(0,2)=802
rm(1,0)=2054, rm(1,1)=0, rm(1,2)=2786

Chicago   LA     NY     Philly

Chicago     0       2054   802    738
LA       2054       0    2786   2706

NY       802      2786    0     100

Philly    738      2706   100      0
Two-Dimensional Arrays
Consider the rows of the table as numbered 0,
1, 2, ,,, m and the columns numbered 0, 1, 2,
…, n. Then the array is declared with the
statement
Dim arrayName(m, n) As DataType
and the item in the ith row, jth column is
denoted
arrayName(i, j)
Populating Array
Dim rm(3, 3) As Double
'Fill two-dimensional array
‘with intercity mileages
Dim sr As IO.StreamReader = _
IO.File.OpenText("DISTANCE.TXT")
For row As Integer = 0 To 3
For col As Integer = 0 To 3
Next
Next
sr.Close()
End Sub
Two-Dimensional Arrays
An unsized two-dimensional array can be
declared with a statement of the form
Dim arrayName(,) As varType
and a two-dimensional array can be
declared and initialized at the same time
with a statement of the form
Dim arrayName(,) As varType =
{{ROW0}, {ROW1},... {ROWm}}
Two-Dimensional Arrays
 An already-created array can be resized with
ReDim arrayName(r, s)
 which loses the current contents, or with
ReDim Preserve arrayName(r, s)
 When Preserve is used, only the column can
be resized.
 ReDim cannot change the number of
dimensions in an array.
GetUpperBound

   Remember GetUpperBound?
– array.GetUpperBound(0)
• Returned the size of a one-dimensional array
– The 0 means the 0th dimension
Dim array(10,5) As String
10 rows with 5 columns each
array.GetUpperBound(0) -> 10
array.GetUpperBound(1) -> 5
Multi-Dimensional Arrays

   You can create arrays with any
dimension
– 3-dimensional arrays
Dim array(10,20,5) As varType
– 4-dimensional arrays
Dim array(10,20,5,20) As varType
– Accessing them is similar
array(2,2,4,5)
– GetUpperBound still works
Structures
   A way of grouping data together
   Also called a UDT (User Defined Type)
   Sample structure definition:
Structure College
Dim name As String
Dim state As String
Dim yearFounded As Integer
End Structure
Structure Definition
Each subvariable in a structure is called a
member.
To declare a variable of a structure type:
Dim college1 As College
Each member is accessed via the
variable
name dot member name
college1.name = "Harvard"
Example 2
Structure College
Dim name As String
Dim state As String
Dim yearFounded As Integer
End Structure
Dim college1, college2, collegeOlder As College
Private Sub btnFirst_Click(...) Handles btnFirst.Click
Dim prompt As String
college1.name = InputBox("Enter name of college.")
college1.state = InputBox("Enter state.")
prompt = ”Enter year the first college was founded."
college1.yearFounded = CInt(InputBox(prompt))
End Sub
Structure Members Data Types

 Integer, String, Double, etc.
 Another User Defined Type
 Arrays
– Must not specify range
– Range must be set using ReDim
Another Example
Structure College
Dim name As String
Dim state As String
Dim yearFounded As Integer
End Structure
Structure Person
Dim first_name As String
Dim last_name As String
Dim dob As Date
Dim first_college As College
End Structure
Dim students(30) As Person

```
To top