Arrays. What is an Array? Similar to a Matrix. Collection of data that needs similar processing....
-
date post
22-Dec-2015 -
Category
Documents
-
view
222 -
download
0
Transcript of Arrays. What is an Array? Similar to a Matrix. Collection of data that needs similar processing....
What is an Array?
• Similar to a Matrix.
• Collection of data that needs similar processing.
• Example: Transpose of a matrix
1 3 4
7 2 9
6 8 5
1 7 6
3 2 8
4 9 5
Why an Array?
• Read a matrix from a file and write its transpose in another file
1,-3,4
-7,2,9
6,8,5
• Need to declare nine variables
• Write 3 Input & 3 Output statements
• Lot of complication in re-arrangement
• Program will fail for 4x4 matrix
Why an Array?
• An array allows us to use the same name for the data, but distinguish its elements by indices
Dim A(1 To 3, 1 To 3) As Integer
(1,1) (1,2) (1,3)
(2,1) (2,2) (2,3)
(3,1) (3,2) (3,3)
A A A
A A A
A A A
Row # 1
Row # 2
Row # 3
Column # 1 Column # 2 Column # 3
Why an Array?
For Row = 1 To 3Input #10, A(Row,1),A(Row,2),A(Row,3)
Next Row
For Colm = 1 To 3Write #20, A(1,Colm),A(2,Colm),A(3,Colm)
Next Colm
Array Storage
A(1,1) A(1,2) A(1,3)
A(2,1) A(2,2) A(2,3)
A(3,1) A(3,2) A(3,3)
A(1,1)A(1,2)A(1,3)A(2,1)A(2,2)A(2,3)A(3,1)A(3,2)A(3,3)A(1,1)
A(2,1)A(3,1)A(1,2)A(2,2)A(3,2)
…
Using Arrays
For Row = 1 To 3For Column = 1 To 3
Input #10, A(Row,Column)Next Column
Next RowFor Row = 1 To 3For Column = 1 To 3
Write #20, A(Column,Row)Next Column
Next Row
Order of Loops is same
Using Arrays
For Row = 1 To 3For Column = 1 To 3
Input #10, A(Row,Column)Next Column
Next RowFor Column = 1 To 3For Row = 1 To 3
Write #20, A(Row,Column)Next Column
Next Row
Order of Loops is reversed
Arrays
• Arrays can have multiple dimensions• Matrix is a 2-diemnsional array• One dimensional arrays
» Row Vector» Column Vector
• Three-dimensional Arrays• Multi-dimensional arrays
» 60 dimensions possible ????????
• Storage requirements» A(10,10,10,10,10) uses 100,000 x 2 bytes for integer
type data
Array Declaration
Dim ArrayOne(1 To 3, 1 To 7)– Two dimensional array of size 3 x 7
• Lower bound = 1, Upper bound = 3 and 7
Dim ArrayTwo(10,10)– Two dimensional array of size 11 x 11
• Lower bound = 0, Upper bound = 10
Option Base 1
Dim Array3(10,10)• 10 x 10 Array
Matrix Addition
• C = A + B– C, A, and B are the same size matrices (mxn)
For row = 1 To m For column = 1 To n C(row,column) = A(row,column) + B(row,column) Next columnNext row
Matrix Multiplication(1,1) (1,2) (1,3) (1,1) (1,2)
(1,1) (1,2) (1,3)(2,1) (2,2) (2,3) (2,1) (2,2)
(2,1) (2,2) (2,3)(3,1) (3,2) (3,3) (3,1) (3,2)
P P P A AB B B
P P P A AB B B
P P P A A
(1,1) (1,1)* (1,1) (1,2)* (2,1)P A B A B
# Columns of A = # Rows of B
(1,2) (1,1)* (1,2) (1,2)* (2,2)P A B A B (1,3) (1,1)* (1,3) (1,2)* (2,3)P A B A B
For j=1 To 3P(1 , j) = P(1 , j)+A(1,1)*B(1 , j)+A(1 , 2)*B(2 , j)
Next j
Matrix Multiplication(1,1) (1,2) (1,3) (1,1) (1,2)
(1,1) (1,2) (1,3)(2,1) (2,2) (2,3) (2,1) (2,2)
(2,1) (2,2) (2,3)(3,1) (3,2) (3,3) (3,1) (3,2)
P P P A AB B B
P P P A AB B B
P P P A A
For j=1 To 3P(1 , j) = P(1 , j) + A(1 ,1)*B(1 , j) + A(1 , 2)*B(2 , j)
Next jFor j=1 To 3
P(2 , j) = P(2 , j) + A(2 ,1)*B(1 , j) + A(2 , 2)*B(2 , j)Next jFor j=1 To 3
P(3 , j) = P(3 , j) + A(3 ,1)*B(1 , j) + A(3 , 2)*B(2 , j)Next j
Matrix Multiplication(1,1) (1,2) (1,3) (1,1) (1,2)
(1,1) (1,2) (1,3)(2,1) (2,2) (2,3) (2,1) (2,2)
(2,1) (2,2) (2,3)(3,1) (3,2) (3,3) (3,1) (3,2)
P P P A AB B B
P P P A AB B B
P P P A A
For i = 1 To 3 For j = 1 To 3
P(i , j) = P(i , j) + A(i ,1)*B(1 , j) + A(i , 2)*B(2 , j) Next jNext i
For i = 1 To 3 For j=1 To 3 For k=1 To 2
P(i , j) = P(i , j) + A(i ,k)*B(k , j) Next k Next jNext i
Matrix Multiplication
• Assume that A is mxn matrix and B is nxp matrix P is mxp matrix
For i = 1 To mFor j = 1 To p
For k = 1 To nP(i,j) = P(i,j) + A(i,k)*B(k,j)
Next kNext j
Next i
Sorting: Bubble Sort
Sort the elements of an array A shown below in ascending order. Use the bubble sort method. A=[7 3 2 4 10 9 0]
Solution• Step 1:
– Compare A(1)=7 and A(2)=3. Swap the numbers as they are not in ascending order. A=[3 7 2 4 10 9 0].
– Now compare A(2)=7 and A(3)=2 and swap A=[3 2 7 4 10 9 0].– Compare A(3)=7 and A(4)=4 A=[3 2 4 7 10 9 0]. – A(4)=7 and A(5)=10 No swapping.– Compare A(5)=10 and A(6)=9 A=[3 2 4 7 9 10 0]. – Compare A(6)=10 and A(7)=0 A=[3 2 4 7 9 0 10]. – Overall 6 comparisons were needed. 10 in correct place
Sorting: Bubble Sort
• Step 2: – The highest number in the list is already in its correct place.
• Exclude this number from any comparison
– Repeat step 1 but avoid any comparison with the last entry. This would result in A=[2 3 4 7 0 9 10]. 9 in correct place
– We needed 5 comparisons.
• Step 3: – Repeating 1 on the initial 5 numbers of the array gives
A=[2 3 4 0 7 9 10]. 7 in correct place – 4 comparisons were needed
Sorting: Bubble Sort
• Step 4: – A=[2 3 0 4 7 9 10]. 4 in correct place. – 3 comparisons
• Step 5: – A=[2 0 3 4 7 9 10]. 3 in correct place. – 2 comparisons.
• Step 6: – A=[0 2 3 4 7 9 10]. 2 and 0 in correct place. – 1 comparison.
Bubble Sort
For step = 1 To N-1For index = 1 To N-run
If x(index) > x(index+1) ThenTemp = x(index)x(index) = x(index+1)x(index+1) = Temp
End IfNext index
Next step
Bubble Sort
Private Sub Command1_Click()Dim X(1 To 1000) As IntegerDim Count As Integer, UserData As StringDim N As Integer, Temp As IntegerPicture1.ClsPicture2.ClsFor Index = 1 To 1000 UserData = InputBox("Number or End: ") If Left(UCase(UserData), 1) = "E" Then Exit For End If X(Index) = Val(UserData)Next Index
Bubble Sort
For step = 1 To N - 1 For Index = 1 To N - step If X(Index) > X(Index + 1) Then Temp = X(Index) X(Index) = X(Index + 1) X(Index + 1) = Temp End If Next Index Do While UCase(Left(InputBox("Enter Next for next step"), 1)) <> "N" Picture2.Print "Enter Next" Loop Picture2.Cls For k = 1 To N Picture2.Print X(k) Next kNext step