INFS308 Spring 2006 - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

INFS308 Spring 2006

Description:

Use DIM statement to create, define dimension and type ... Searching two dimensional arrays requires nesting loops. ... changes in last dimension. Summary ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 25
Provided by: BenM1
Category:

less

Transcript and Presenter's Notes

Title: INFS308 Spring 2006


1
INFS308Spring 2006
  • University of Colorado
  • At
  • Colorado Springs
  • Ben Martz

2
Overview
  • General Discussion
  • Uses Structure Declaring
  • Table Look up
  • Searching
  • Sorting
  • Tutorial 17 18 Review

3
Arrays
intj
intd
intc
inte
intb
inti
intf
  • Arrays are short cuts for when you need a lot of
    the same type of variable.
  • Instead of ten integer variables for ten costs,
    an array can handle all ten

inta
inth
intg
intArray
4
Arrays
  • Each variable location in an array is called an
    element
  • Use DIM statement to create, define dimension and
    type
  • DIM intArray(9) as Integer creates the structure
    below
  • Treat as normal variables Reference using
    subscripts intArray(3) 114
  • Once created, MUST refer to using subscript

intArray
114
5
Arrays
intj
intd
intc
inte
  • Arrays assist in making computer programs more
    efficient.
  • Compare adding all the integers stored in the
    variables.

intb
inti
intf
inta
inth
intg
Sum inta intb intc intd inte intf
intg inth inti inj
intArray
For I 0 to 9 sum sum intarray(I) Next I
6
Arrays
  • Arrays come in different dimensions.
  • Two dimensional arrays
  • require two subscripts
  • are row dominant (rows are referenced first)
  • Example
  • DIM intSales( 2, 3) as integer

0 1 2 3
In this case, what is the value of IntSales
(1,2) ?
0 1 2
1402
7532
7
Arrays
  • Three dimensional arrays
  • require three subscripts
  • are row dominant
  • Example
  • DIM intSales (2, 3, 1) as integer

1402
So the number 2134 is found where? the
number 4521 is ______ the number 1402 is
______ the number 3425 is ______
4521
2134
3425
8
Coordinated Arrays
strName intSales
  • Dim strName (4) as String
  • Dim intSales (4) as Integer
  • So, if we know that Smith is in strName(1), we
    also know his or her sales is in intSales(1)

Jones Smith Frank Able Zean
1402 2301 0231 6762 0199
9
Creating Arrays
  • DIM statement
  • Array name Dimensions Bounds Type
  • DIM strName (2, 3) as string
  • Using
  • Any integer or integer variable can be used to
    subscript
  • Must stay within the bounds
  • Must have the correct number of subscripts
  • Must assign the correct type of data
  • Given DIM strName (2, 3) as string
  • Is strName (0,0) Jones valid
  • Is strName (1) Smith valid
  • Is strName (3,2) Larry valid
  • Is strName(2,3) 1402 valid

10
Table Lookup
  • Assume
  • DIM curTuition (1, 2) as currency
  • intRes is an integer (0 or 1) the represents a
    students residency status
  • intClass is an integer (0, 1 or 2) that
    represents a students class standing
  • The user interface asks the student for his or
    her residency and class status
  • Then
  • curTuition(intRes,intClass) would represent the
    tuition owe by that student

Class
0 1 2
0 1
Residency
11
Searching Arrays (unordered data)
  • Searching implies looking for a target.
  • A simple sequential search looks through the
    array for the target.

Given that the array is unordered, no obvious
ordering (ie. Alphabetical), a sequential search
is the best.
  • Target Q
  • Found False
  • For I 0 to 9
  • If target strArray(I) Then
  • found true
  • Exit For
  • end if
  • Next I

strArray
A F S V W Q Z X Y L
12
Searching Arrays (unordered data)
  • Searching two dimensional arrays requires nesting
    loops. Again a simple sequential search looks
    through the array for the target.
  • Target Q
  • Found False
  • For I 0 to 1
  • For J 0 to 9
  • If target strArray(I,J) Then
  • found true
  • Exit for
  • end if
  • Next J
  • if found true then exit for
  • Next I

strArray
A F S V W Q Z X Y L
O M N G I T K P B
H
13
Searching Arrays (ordered data)
  • If your data is ordered, you have a new way to
    determine whether or not the target is in the
    array. For example, in an ordered list if the
    current element is larger than the target (and
    the target is not found) then the target cannot
    be in the array.

Target 17 Found False For I 0 to 9 If
target strArray(I) Then Found true If
(found true) or (strArray(I) gt target) then
Exit For end if Next I
intArray
1 5 7 8 10 15 20 23 25 40
14
Searching Arrays (ordered data)
  • The characteristic of ordered data provides
    another search technique called the binary
    search.

Set Target Found False Lowerbound 1
upperbound 10 Do While (Found False) and
(Lowerbound ltgt Upperbound) then If
intArray((lowerbound upperbound)\2) target
then found true else if
intArray((lowerbound upperbound)\2) gt target
then upperbound ((lowerbound
upperbound)\2) - 1 else
lowerbound (( lowerbound upperbound)\2) 1
end if endif Loop
intArray
1 5 7 8 10 15 20 23 25 40
15
Searching Arrays (ordered data)
  • The characteristic of ordered data provides
    another search technique called the binary
    search.

Assume looking for 20
intArray
1 5 7 8 10 15 20 23 25 40
L
M
U
Pass 1 Pass 2 Pass 3
L
U
M
U
M
L
16
Searching Arrays (ordered data)
  • Binary vs Sequential
  • (Remember that Data must be ordered for Binary
    search)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
16
In four searches, a binary search can cover a
16 element array In five, it can cover a 32
element array. Compared to the sequential process
this is extremely efficient and especially so
with large numbers
1
4
3
2
Number of elements Worst
case search
Binary Search Sequential 16
4
16 32
5
32 64
6 64 1024
10
1024
See a pattern?
17
Sorting Arrays
  • Sorting is defined as rearranging the values in
    the elements of an array to make them ordered
  • The goal of a sorting algorithm should be to get
    at least one value in its correct position each
    time or pass through the array.
  • Efficient algorithms will stop when all elements
    are in the correct order (regardless if they have
    completed all possible passes)
  • Two common sorting algorithms
  • Selection Sort
  • Bubble Sort
  • VB.Net has intrinsic method array.sort(arrayname)

18
Selection Sort
  • For I 0 to 8
  • largest I
  • For J (I 1) to 9
  • if strArray(j) gt strArray(largest) then
  • largest j
  • end if
  • Nest j
  • swap (strArray(I), strArray(largest)
  • Next I

A F S V W Q Z X Y L
strArray
intPoslargest
I J
After 1st pass
Z F S V W Q A X Y L
strArray
19
Bubble Sort
  • For I 0 to 9
  • For J 0 to 8
  • if strArray(J) gt strArray(J1) then
  • swap (strArray(J), strArray(J1))
  • end if
  • Nest j
  • Next I

A F S V W Q Z X Y L
strArray
no no no
no
yes (q for w)
no (q for Z)
yes (z for X)

yes (z for y)

yes (z for l)
After 1st pass
A F S V Q W X Y L Z
strArray
20
Dynamic Arrays
  • VB allows a programming to redimension arrays
    during the run of the program.
  • Rules for Dynamic Arrays
  • Use empty dimension list intArray() as Integer
  • Use ReDim to assign dimensions
  • Redim intArray (intNumber, 2)
  • Use ReDim to change Bounds
  • Redim intArray ( (intNumber 10), 2)
  • Use Preserve keyword to save data in array
  • ReDim Preserve intArray(1 to intNumber, 2)
  • Preserve only allows changes in last dimension

21
Summary
  • Arrays are sets of variables know as elements
  • These elements operate just like simple variables
  • Arrays are created with a DIM statement that
    defines boundaries, dimensions and type
  • When referencing elements in an array, you must
    have the correct number of subscripts and their
    values must be within the bounds of the
    dimensions
  • The operation of looking through an array for a
    target is called searching
  • The operation of reorganizing the data in an
    array is called sorting
  • Binary searches are more efficient than
    sequential searches

22
Review
  • Given the statement
  • DIM intarray (10) as integer
  • What are the array bounds?
  • Initializer list
  • DIM intarray (9) as integer 3,4,3,4,5,1,1,2,3,4

23
Review
  • Describe a Combo Box
  • Dropdownstyle appearance
  • Dropdownlist list only
  • Datasource list of values can be array
  • Selectedvalue the value selected in combobox

24
Radiobutton Review
  • Mutually exclusive list
  • .Checked property is boolean
  • .CheckedChange event
Write a Comment
User Comments (0)
About PowerShow.com