Chapt6 Part2 MultiDArray
Chapt6 Part2 MultiDArray
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
intNewValues = intOldValues
◼ A loop must be used to copy individual elements from one array to another
◼ Related data in multiple arrays can be accessed using the same subscript
' 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
◼ 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
◼ You can change the number of elements in an array at runtime, using the ReDim statement
◼ 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
◼ 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
For intCount = 0 To 3
strNames(intCount) = InputBox("Enter name " &
(intCount.ToString())
Next
Return strNames
End Function
◼ 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
◼ 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
END OF CHAPTER 6…
ARRAYS