Array processing techniques : Copying,
parallel array, searching and sorting
CHAPTER 6:
Procedures and functions in arrays
ARRAY PROCESSING
AND
MULTIDIMENSIONAL
Store data in multidimensional array
ARRAY
ARRAY PROCESSING TECHNIQUE:
COPYING ONE ARRAY’S CONTENTS TO ANOTHER
◼ A single assignment statement
intNewValues = intOldValues
◼ Does not copy array values into another array
◼ Causes both array names to reference the same array in memory
◼ A loop must be used to copy individual elements from one array to another
For intCount = 0 To (intOldValues.Length-1)
intNewValues(intCount) = intOldValues(intCount)
Next
ARRAY PROCESSING TECHNIQUE:
PARALLEL ARRAYS
◼ Related data in multiple arrays can be accessed using the same subscript
Const intMAX As Integer = 3
Dim strWorkshops(intMAX) As String = {"Negotiating Skills","Lowering Stress",
"Teamwork", "Building Resumes"}
Dim decCosts(intMAX) As String = {500D, 450D, 720D, 250D}
ARRAY PROCESSING TECHNIQUE:
PARALLEL RELATIONSHIPS BETWEEN ARRAY, LIST BOX AND COMBO BOX
Example : ' A list box with names
lstPeople.Items.Add("Jean James") ' Index 0
lstPeople.Items.Add("Kevin Smith")' Index 1
lstPeople.Items.Add("Joe Harrison") ' Index 2
' An array with corresponding phone numbers
phoneNumbers(0) = "555-2987" ' Element 0
phoneNumbers(1) = "555-5656" ' Element 1
phoneNumbers(2) = "555-8897" ' Element 2
' Display the phone number for the selected person’s name.
If lstPeople.SelectedIndex > -1 And
lstPeople.SelectedIndex < phoneNumbers.Length Then
MessageBox.Show(phoneNumbers(lstPeople.SelectedIndex))
Else
MessageBox.Show("That is not a valid selection.")
End If
ARRAY PROCESSING TECHNIQUE:
SEARCHING ARRAYS
◼ The most basic method of searching ◼ The Pseudocode for a sequential
an array is the sequential search search is as follows:
◼ Uses a loop to examine elements in the array
found = False
◼ Compares each element with the search value
subscript = 0
◼ Stops when the value is found or the end of the Do While found is False and
array is reached subscript < array's length
If array(subscript) = searchValue Then
found = True
position = subscript
End If
subscript += 1
End While
ARRAY PROCESSING TECHNIQUE:
SORTING AN ARRAY
◼ Programmers often want to sort, or arrange the ◼ Here is the general format:
elements of an array in ascending order
◼ Values are arranged from lowest to highest
Array.Sort(ArrayName)
◼ Lowest value is stored in the first element
◼ ArrayName is the name of the array you want to sort
◼ Highest value is stored in the last element
◼ For example:
◼ To sort an array in ascending order
Dim intNumbers() As Integer =
◼ Use the Array.Sort method {7, 12, 1, 6, 3}
Array.Sort(intNumbers)
◼ After the statement executes, the array values are in the
following order
◼ 1, 3, 6, 7, 12
ARRAY PROCESSING TECHNIQUE:
SORTING AN ARRAY
◼ When you pass an array of strings to the
◼ For example:
Array.Sort method the array is sorted in
ascending order Dim strNames() As String =
◼ According to the Unicode encoding {"dan", "Kim", "Adam", "Bill"}
Array.Sort(strNames)
scheme
◼ Sort occurs in alphabetic order ◼ After the statement executes, the values in
◼ Numeric digits first the array appear in this order:
◼ Uppercase letters second
◼ "Adam", "Bill", "Kim", "dan"
◼ Lowercase letters last
DYNAMICALLY SIZING ARRAYS
◼ You can change the number of elements in an array at runtime, using the ReDim statement
ReDim [Preserve] Arrayname (UpperSubscript)
◼ Preserve is optional
◼ If used, the existing values of the array are preserved
◼ If not, the existing values are destroyed
◼ Arrayname is the name of the array being resized
◼ UpperSubscript is the new upper subscript
◼ Must be a positive whole number
◼ If smaller that it was, elements at the end are lost
DYNAMICALLY SIZING ARRAYS EXAMPLE
◼ You can initially declare an array with no size, as follows:
Dim dblScores() As Double
◼ Then prompt the user for the number of elements
◼ And resize the array based on user input
intNumScores = CInt(InputBox("Enter the number of test scores."))
If intNumScores > 0 Then
ReDim dblScores (intNumScores - 1)
Else
MessageBox.Show("You must enter 1 or greater.")
End If
PROCEDURES AND FUNCTIONS:
PASSING ARRAYS AS ARGUMENTS
◼ Procedures can be written to process the ' The DisplaySum procedure displays the
data in arrays ' sum of the elements in the argument array.
Sub DisplaySum(ByVal intArray() As Integer)
◼ Store data in an array Dim intTotal As Integer = 0 ' Accumulator
Dim intCount As Integer ' Loop counter
◼ Display an array’s contents
For intCount = 0 To (intArray.Length - 1)
◼ Sum or average the values in an array intTotal += intArray(intCount)
Next
◼ Usually such procedures accept an array as an MessageBox.Show("The total is " &
argument intTotal.ToString())
End Sub
◼ Pass the name of the array as the
argument to the procedure or function Dim intNumbers() As Integer =
{ 2, 4, 7, 9, 8, 12, 10 }
DisplaySum(intNumbers)
PROCEDURES AND FUNCTIONS:
PASSING ARRAYS BY VALUE AND BY REFERENCE
◼ Array arguments can be accessed and modified if passed ByVal or ByRef
◼ ByVal prevents an array from being assigned to another array
◼ ByRef allows an array to be assigned to another array
◼ After the ResetValues procedure executes
◼ If passed ByVal : intNumbers is unchanged and keeps the values {1, 2, 3, 4, 5}
◼ If passed ByRef :intNumbers will reference the newArray values {0, 0, 0, 0, 0}
Dim intNumbers() As Integer = { 1, 2, 3, 4, 5 }
ResetValues(intNumbers)
Sub ResetValues(ByVal intArray() As Integer)
Dim newArray() As Integer = {0, 0, 0, 0, 0}
intArray = newArray
End Sub
PROCEDURES AND FUNCTIONS:
RETURNING AN ARRAY FROM A FUNCTION
Example : ' Get three names from the user and return
' them as an array of strings.
Function GetNames() As String()
Const intMAX_SUBSCRIPT As Integer = 3
Dim strNames(intMAX_SUBSCRIPT) As String
Dim intCount As Integer
For intCount = 0 To 3
strNames(intCount) = InputBox("Enter name " &
(intCount.ToString())
Next
Return strNames
End Function
Dim strCustomers() As String
strCustomers = GetNames()
TWO-DIMENSIONAL
ARRAYS
◼ An array with one subscript is
called a one-dimensional array
◼ Useful for storing and working
with a single set of data
◼ A two-dimensional array is like
an array of arrays
◼ Used to hold multiple sets of
values
◼ Think of it as having rows and
columns of elements
DECLARING A TWO-DIMENSIONAL ARRAY
◼ A two-dimensional array declaration requires two sets of upper subscripts
◼ First upper subscript is for the rows
◼ Second upper subscript for the columns
Dim ArrayName (UpperRow,UpperColumn) As DataType
◼ ArrayName is the name of the array
◼ UpperRow is the value of the highest row subscript
◼ Must be a positive integer
◼ UpperColumn is the value of the highest column subscript
◼ Must be a positive integer
◼ DataType is the Visual Basic data type
◼ Example declaration with three rows and four columns: Dim dblScores (2, 3) As Double
PROCESSING DATA IN TWO-DIMENSIONAL ARRAYS
Const intMAX_ROW As Integer = 2
Const intMAX_COL As Integer = 3
Dim dblScores(intMAX_ROW, intMAX_COL) As Double
◼ Use named constants to specify the upper subscripts
PROCESSING DATA IN TWO-DIMENSIONAL ARRAYS
◼ The elements in row 0 are referenced as follows:
dblScores(0, 0) ' Element in row 0, column 0
dblScores(0, 1) ' Element in row 0, column 1
dblScores(0, 2) ' Element in row 0, column 2
dblScores(0, 3) ' Element in row 0, column 3
◼ The elements in row 1 are referenced as follows:
dblScores(1, 0) ' Element in row 1, column 0
dblScores(1, 1) ' Element in row 1, column 1
dblScores(1, 2) ' Element in row 1, column 2
dblScores(1, 3) ' Element in row 1, column 3
◼ The elements in row 2 are referenced as follows:
dblScores(2, 0) ' Element in row 2, column 0
dblScores(2, 1) ' Element in row 2, column 1
dblScores(2, 2) ' Element in row 2, column 2
dblScores(2, 3) ' Element in row 2, column 3
PROCESSING DATA IN TWO-DIMENSIONAL ARRAYS
◼ Example of storing a number in a single element dblScores(2, 1) = 95
◼ Example of prompting the user for input, once for each element
For intRow = 0 To intMAX_ROW
For intCol = 0 To intMAX_COL
dblScores(intRow, intCol) =
CDbl(InputBox("Enter a score."))
Next
Next
◼ Example of displaying all of the elements in the array
For intRow = 0 To intMAX_ROW
For intCol = 0 To intMAX_COL
lstOutput.Items.Add(dblScores(intRow, intCol).ToString())
Next
Next
IMPLICIT SIZING AND INITIALIZATION OF TWO-DIMENSIONAL
ARRAYS
◼ When providing an initialization list for a two-dimensional array, keep in mind that:
◼ You cannot provide the upper subscript numbers
◼ You must provide a comma to indicate the number of dimensions
◼ Values for each row are enclosed in their own set of braces
intNumbers(0, 0) is set to 1
intNumbers(0, 1) is set to 2
This statement declares an array intNumbers(0, 2) is set to 3
with three rows and three columns:
intNumbers(1, 0) is set to 4
Dim intNumbers(,) As Integer =
intNumbers(1, 1) is set to 5
{ {1, 2, 3} , intNumbers(1, 2) is set to 6
{4, 5, 6} , intNumbers(2, 0) is set to 7
{7, 8, 9} }
intNumbers(2, 1) is set to 8
intNumbers(2, 2) is set to 9
SUMMING THE COLUMNS OF A
TWO-DIMENSIONAL ARRAY
◼ The outer loop controls the intCol subscript
◼ The inner loop controls the intRow subscript
' Sum the columns.
For intCol = 0 To intMAX_COL
' Initialize the accumulator.
intTotal = 0
' Sum all rows within this column.
For intRow = 0 To intMAX_ROW
intTotal += intValues(intRow, intCol)
Next
' Display the sum of the column.
MessageBox.Show("Sum of column " &
intCol.ToString() & " is " & intTotal.ToString())
Next
THREE-DIMENSIONAL ARRAYS AND BEYOND
Dim intPages(2, 2, 3) As Decimal
◼ You can create arrays with up to 32 dimensions
◼ The following is an example of a three-dimensional array:
THREE-DIMENSIONAL ARRAYS AND BEYOND
◼ Arrays with more than three dimension are difficult to visualize
◼ Useful in some programming applications
◼ For example:
◼ A factory warehouse where cases of widgets are stacked on pallets, an array of four
dimensions can store a part number for each widget
◼ The four subscripts of each element can store:
◼ Pallet number
◼ Case number
◼ Row number
◼ Column number
◼ A five dimensional array could be used for multiple warehouses
END OF SUBTOPIC …
ARRAY PROCESSING AND
MULTIDIMENSIONAL ARRAY
END OF CHAPTER 6…
ARRAYS