Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

Slide 1 ___________________________________

IT 236 User Interface Development ___________________________________


Lecture 7
___________________________________
Eric J. Schwabe
School of CTI, DePaul University ___________________________________
Spring 2008
___________________________________
___________________________________
___________________________________

Slide 2 ___________________________________
Tonight: ___________________________________
 Functions ___________________________________
 Arrays
 Lab Session 3 (Room 819, 7:30pm-9:00pm) ___________________________________
___________________________________
___________________________________
___________________________________

Slide 3 ___________________________________
Functions ___________________________________
 A function is a piece of code that is called ___________________________________
with some input value(s) and returns a single
output value
 Example (to find the ceiling of a number): ___________________________________
Private Function Ceiling (ByVal x As Double) As Int

___________________________________
Dim result As Double
result = CInt(-Int(-x))
Return result
End Function

 Ceiling(5.4) will return a result of 6 ___________________________________


___________________________________
Slide 4 ___________________________________
Function Definition Syntax ___________________________________
PrivateFunction FunctionName(ByVal
varName As VarType , ...) As ReturnType ___________________________________
...body of function...

___________________________________
Return expression
End Function
 Function is called with FunctionName(input),
where input is of type VarType
 Return value will be the result of evaluating
___________________________________
expression, and will be of type ReturnType,
___________________________________
___________________________________

Slide 5 ___________________________________
When a function is called… ___________________________________
1. The arguments to the function are evaluated and ___________________________________
the values sent to the function
2. The values of the arguments are assigned to the
corresponding parameters ___________________________________
3. The body of the function is executed
4. Upon reaching a return statement, the return ___________________________________
expression is evaluated
5. The return value is sent back to the calling
procedure and replaces the function call ___________________________________
___________________________________

Slide 6 ___________________________________
Pizza Prices ___________________________________
 Which is the best deal? ___________________________________
6 inch, $2.15
8 inch, $3.95
 12 inch, $7.85
___________________________________
 16 inch, $10.50

 18 inch, $12.65
___________________________________
 Write a function to compute the cost per
square inch
 cost / area = cost / (diameter/2)^2 * 3.14159
___________________________________
___________________________________
Slide 7 ___________________________________
Arrays ___________________________________
 An array is a named collection of memory ___________________________________
locations
 To declare an array: ___________________________________
Dim arrayName(n) As varType
 varType is the type of value stored at each
location
___________________________________
 values are read/written using numerical
indices between 0 and n ___________________________________
___________________________________

Slide 8 ___________________________________
Arrays ___________________________________
 Visual Basic is unusual as arrayName(n) has ___________________________________
n+1 locations rather than n
 However, we will use only locations 0 ___________________________________
through n-1 in an array declared with size n
 Bounds checking: Always insure that an
index i is between 0 and n-1 before accessing
___________________________________
position i in an array
___________________________________
___________________________________

Slide 9 ___________________________________
For loops ___________________________________
 Can use a For loop to visit all elements in an ___________________________________
array
 Index variable runs from 0 to n-1 and ___________________________________
represents the current position in array
For i = 0 to n-1
// ...loop body...visit element i... ___________________________________
Next

___________________________________
___________________________________
Slide 10 ___________________________________
Bounds Checking ___________________________________
 Using If: ___________________________________
If (i >= 0) And (i <= n-1)

___________________________________
// do array access...
End If

 Using Case:
Select Case (i)
Case 0 To n-1 ___________________________________
// do array access...
End Select
___________________________________
___________________________________

Slide 11 ___________________________________
For Each loop ___________________________________
 Visits all elements of an array without using ___________________________________
indices:
For Each elementName As varType In arrayName
// ...loop body...visit one element...
___________________________________
Next

 Does not give you access to the current index ___________________________________


 Does not let you skip position n
___________________________________
___________________________________

Slide 12 ___________________________________
Example: ___________________________________
 When button is pressed: ___________________________________
 Prompt for six students’ exam scores
 Store them in an array ___________________________________
 Compute the average score
Display the scores and average in a list box

___________________________________
___________________________________
___________________________________
Slide 13 ___________________________________
Searching an Array ___________________________________
 Searching for a desired value in a… ___________________________________
 Unsorted array: linear search
 Check each element in turn, keeping track of if ___________________________________
(and in what position) the desired value is found
 Sorted array: binary search
 Check the middle element of the range being
___________________________________
searched, and if it is not the desired value, search
only the left or right half of the range further ___________________________________
___________________________________

Slide 14 ___________________________________
Linear Search ___________________________________
 Problem: Search A(0....n-1) for an element x ___________________________________
 Algorithm:
For each i from 0 to n-1: ___________________________________
If A(i) is x, remember the value i, and that the
element was found
(After the loop: If the element was found, then
___________________________________
return i; otherwise report that the element was
not there) ___________________________________
___________________________________

Slide 15 ___________________________________
Binary Search ___________________________________
 Problem: Search A(0...n-1) for an element x ___________________________________
 Algorithm:

___________________________________
Start by considering the entire range (0…n-1)
While there is anything left in the range i…j (and x hasn’t been found)
Look at the middle element of the range (position k = (i+j)\2)
If A(k) < x, then just consider the right half of the range
If A(k) > x, then just consider at the left half of the range
If A(k) = x, then remember k and record that the element is found
___________________________________
(After the loop: If the element was found, return k; otherwise report that
the element was not there)
___________________________________
___________________________________
Slide 16 ___________________________________
Two-Dimensional Arrays ___________________________________
 Arrays can have more than one dimension: ___________________________________
Dim arrayName(m, n) As varType
 Array accesses now require two indices: ___________________________________
arrayName(i, j)
where i is between 0 and m-1 and j is ___________________________________
between 0 and n-1

___________________________________
___________________________________

Slide 17 ___________________________________
Control Arrays ___________________________________
 We can also create arrays of controls: ___________________________________
Dim controlArray(n) As controlType
 Assign existing controls to array elements: ___________________________________
controlArray(n) = nameOfControl
 Controls in array now have two names, and ___________________________________
can be visited as a group using a For loop
 Control arrays must be form-level ___________________________________
___________________________________

Slide 18 ___________________________________
Generating Random Numbers ___________________________________
 To create a random number generator: ___________________________________
Dim generator As New Random
 To generate a random integer between 0 and ___________________________________
n (including both 0 and n as possibilities):
generator.Next(0, n+1) ___________________________________
 (Note that you must give n+1 as an argument
to include n in the range of possible values…)
___________________________________
___________________________________
Slide 19 ___________________________________
Next Time: ___________________________________
 (No class next Monday, 5/26…) ___________________________________
 Multiform projects
 Accessing databases ___________________________________
 Course review
___________________________________
(Assignment 6 has been posted, due 5/26 … but accepted late
without penalty until 6/2…) ___________________________________
___________________________________

You might also like