Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arrays. What is an Array? Similar to a Matrix. Collection of data that needs similar processing. Example: Transpose of a matrix.

Similar presentations


Presentation on theme: "Arrays. What is an Array? Similar to a Matrix. Collection of data that needs similar processing. Example: Transpose of a matrix."— Presentation transcript:

1 Arrays

2 What is an Array? Similar to a Matrix. Collection of data that needs similar processing. Example: Transpose of a matrix

3 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

4 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 Row # 1 Row # 2 Row # 3 Column # 1Column # 2Column # 3

5 Why an Array? For Row = 1 To 3 Input #10, A(Row,1),A(Row,2),A(Row,3) Next Row For Colm = 1 To 3 Write #20, A(1,Colm),A(2,Colm),A(3,Colm) Next Colm

6 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) …

7 Using Arrays For Row = 1 To 3 For Column = 1 To 3 Input #10, A(Row,Column) Next Column Next Row For Row = 1 To 3 For Column = 1 To 3 Write #20, A(Column,Row) Next Column Next Row Order of Loops is same

8 Using Arrays For Row = 1 To 3 For Column = 1 To 3 Input #10, A(Row,Column) Next Column Next Row For Column = 1 To 3 For Row = 1 To 3 Write #20, A(Row,Column) Next Column Next Row Order of Loops is reversed

9 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

10 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

11 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 column Next row

12 Matrix Multiplication # Columns of A = # Rows of B For j=1 To 3 P(1, j) = P(1, j)+A(1,1)*B(1, j)+A(1, 2)*B(2, j) Next j

13 Matrix Multiplication For j=1 To 3 P(1, j) = P(1, j) + A(1,1)*B(1, j) + A(1, 2)*B(2, j) Next j For j=1 To 3 P(2, j) = P(2, j) + A(2,1)*B(1, j) + A(2, 2)*B(2, j) Next j For j=1 To 3 P(3, j) = P(3, j) + A(3,1)*B(1, j) + A(3, 2)*B(2, j) Next j

14 Matrix Multiplication 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 j Next 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 j Next i

15 Matrix Multiplication Assume that A is mxn matrix and B is nxp matrix  P is mxp matrix For i = 1 To m For j = 1 To p For k = 1 To n P(i,j) = P(i,j) + A(i,k)*B(k,j) Next k Next j Next i

16 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

17 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

18 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.

19 Bubble Sort For step = 1 To N-1 For index = 1 To N-run If x(index) > x(index+1) Then Temp = x(index) x(index) = x(index+1) x(index+1) = Temp End If Next index Next step

20 Bubble Sort Private Sub Command1_Click() Dim X(1 To 1000) As Integer Dim Count As Integer, UserData As String Dim N As Integer, Temp As Integer Picture1.Cls Picture2.Cls For 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

21 Bubble Sort N = Index - 1 For i = 1 To N Picture1.Print X(i) Next i

22 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 k Next step

23 Bubble Sort Picture2.Cls For j = 1 To N Picture2.Print X(j) Next j End Sub


Download ppt "Arrays. What is an Array? Similar to a Matrix. Collection of data that needs similar processing. Example: Transpose of a matrix."

Similar presentations


Ads by Google