# Lecture 7 Processing with Array

Document Sample

```					Lecture 7: Processing with Array

Daniel Chen
Processing with Arrays
•   Often use For-Next Loop
•   Sum and Average
•   Largest number
•   Smallest number
•   Find a given number
•   Sort an Array
Average
intSum = 0
For intCounter= 0 to intNumValues-1
intSum = intSum + intA(intCounter)
Next
sngAvg=intSum / intNumValues
Example (SumAvg)
Find Largest
Begin procedure
Set largest value to first item in list
Repeat beginning with second item to largest item
if item in list> largest value then
largest value=item in list
end decision
End Repeat
Display largest value
End procedure
Practice
Find Smallest
(What will be the pseudo code?)
Find Smallest
(What will be the pseudo code?)
Begin Procedure
Set smallest value to first item in list
Repeat beginning with second item to largest item
if item in list< smallest value then
smallest value=item in list
end decision
End Repeat
Display smallest value
End Procedure
Example
Multiple Aligned Arrays
• Arrays are homogeneous
– All the data in AN array is of the same type
• When you have heterogeneous data, you
often need more than one array
– An integer array for Score
– A currency array for salary
– A string array for Name
Example (Find a number)
• Input data from a file including name and
number (no duplicated numbers)
– Note use of quotation marks
– Need to have no extra returns
– Remember to Close
Pseudo Code
Begin procedure to find a number
Input a NumberToFind
Set Flag to False
Repeat for each number in number list
if NumberToFind = a number in number list then
Flag=True
Save index of the number in number list
end decision
End repeat
If Flag=True then
use saved index to display information related to that number
Else
display information that number not on the list
End decision
End Procedure
The MsgBox Function
• We’ve used MsgBox as a Sub
• It didn’t have a value
• Now we use it as a function
intVariable=MsgBox(message, buttons, title)
• Unlike the InputBox, you don’t get input
• You do get find out what button was clicked
• And, of course, to choose which buttons are used
– It returns an integer which matches up against some
already defined vb-names (see page 234; Table 6-3)
Example: Get the number
• Based on Find a Number
• Click a name in the listbox to get his/her
number
A new property of List Box
• The ListIndex property tells which line is
clicked
• It’s –1 if nothing is clicked
• If you set it to-1, it unclicks
• The listbox is numbered the same way as
the two arrays
Tips on Array Processing
• Because arrays are often used throughout a problem, it is
often true that their Dim statement be at the form-level
• Dim an array large enough, though you may (often) use
less than you allocated
• It is also common that the “used” size of the array is stored
in a variable Dim-ed at the form-level
• String values are best contained in quotation marks in a file
– To take care of the blanks in a string
Multiple Forms
• formname.Hide
• formname.Show
– These are methods
Searching in Strings

* Searching for strings using a loop requires that you use
UCase() or LCase() to convert all strings to the same case.
* To search for a sub-string within a string, you use the lnStr
function:
Instr (string to search, search string)
For example, InStr(“Go Dogs”, “D “) returns 4 because “D” is in
the 4th position. If the character does not exist, InStr()
returns a zero.
*NOTE: InStr() is case sensitive!!!
Quiz
• Instr(“Thompson”, “mp”)
– Equals ?
• InStr(“Thompson”, “MP”)
– Equals ?
• InStr(Ucase(“Thompson”),Ucase(“MP”))
– Equals ?
Quiz
• Instr(“Thompson”, “mp”)
– Equals 4
• InStr(“Thompson”, “MP”)
– Equals 0
• InStr(Ucase(“Thompson”),Ucase(“MP”))
– Equals 4
Example (Get the Name)
• To find a near match on name
• Report the number for that name
Two-Dimensional Arrays
• When you are working with a table instead of a
list, you have a 2-dimensional array.
• 2-D arrays use two subscripts with the first
subscript referring to the row value and the second
subscript referring to the column value.
• Nested For-next loops are often used to work with
2-D arrays with the inner loop matching the
columns and the outer loop matching the rows.
Using the Step Commands for
Debugging

Step into
Stepout of
The Step Commands
Step Over

If you select Step Into, you can “step” through the
code, line by line and note the result of the code in the
various windows--Watch, Locals, or Immediate.

```
DOCUMENT INFO
Categories:
Tags:
Stats:
 views: 10 posted: 3/4/2012 language: pages: 23