Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computing Dr. Nadeem A Khan. Lecture 28.

Similar presentations


Presentation on theme: "Introduction to Computing Dr. Nadeem A Khan. Lecture 28."— Presentation transcript:

1 Introduction to Computing Dr. Nadeem A Khan

2 Lecture 28

3 Last Assignment has been posted

4 Two Dimensional Arrays

5 ► Declaration formats: Dim arrayname(m1 To n1, m2 To n2) As varType ReDim arrayname(m1 To n1, m2 To n2) As varType => Scope (Global/ Form-level/ Local) established in the same way as for 1-D arrays 2-D Arrays

6 ► Declaration format examples: Private Dim rm(1 To 4, 1 To 4) As String Public Dim rm( ) As String Dim rm( ) As String ReDim rm(1 To 4, 1 To 4) ReDim rm(1 To 4, 3 To 4) As String 2-D Arrays

7 ► Value assignment: Dim student(1 To 2, 1 To 3) as String Let student(1, 3) = “Aslam” Let student(2, 1) = “Bushra” 2-D Arrays

8 ► Example 1: Program to store and access data from Table 7.9 Data is in the data file DISTANCE.TXT 2-D Arrays

9 Example 1 (Contd.) Dim rm(1 To 4, 1 To 4) As Single ‘General Declaration Sub Form_Load Dim row As Integer, col As Integer Open “DISTANCE.TXT” For Input As #1 For row=1 To 4 For col=1 To 4 Input #1, rm(row, col) Next col Next row Close #1 End Sub 2-D Arrays

10 Example 1 (Contd.) Sub Command1_Click( ) Dim row As Integer, col As Integer Let row=Val(Text1.Text) Let col = Val(Text2.Text) If (row>=1 And row =1 And col =1 And row =1 And col<=4) Then Call ShowMileage(rm(), row, col) Else MsgBox ”Range 1 to 4 only”,, “Error” End If Text1.SetFocus End Sub 2-D Arrays

11 Example 1 (Contd.) Sub ShowMileage (rm( ) As Single, row As Integer, _ col As Integer) col As Integer)Picture1.Cls Picture1.Print “The road mileage is:” rm(row, col) End Sub 2-D Arrays

12 Sorting

13 ► Sorting: Ordering an unordered array like the following: PebblesBarneyWilmaFredDino Sorting

14 ► Compare adjacent items and swap if out of order ► In n-1 passes the array of n elements will get sorted =>How will you swap values stored in a pair of variables? Bubble Sorting Algorithm

15 ► First Pass Pebbles Barney Barney Braney Barney Barney Pebbles Pebbles Pebbles Pebbles Wilma Wilma Wilma Fred Fred Fred Fred Fred Wilma Dino Dino Dino Dino DinoWilma Bubble Sorting

16 ► Second Pass Barney Barney Barney Braney Pebbles Pebbles Fred Fred FredFred Pebbles Dino Dino Dino Dino Pebbles Wilma Wilma Wilma Wilma Bubble Sorting

17 ► Third Pass Barney Barney Barney FredFred Dino DinoDino Fred Pebbles Pebbles Pebbles Wilma Wilma Wilma Bubble Sorting

18 ► Fourth Pass Barney Barney DinoDino FredFred Pebbles Pebbles Wilma Wilma Bubble Sorting

19 ► In each pass compare items at a gap and swap if out of order swap if out of order ► Halve the gap in the next passes if no swap in the current pass Initial gap: half the array length Int (array length/2) Final gap: adjacent neighbours Sorting: Shell Sort

20 ► First Pass: gap=2 Pebbles Pebbles Pebbles Pebbles Barney BarneyBarney Barney Wilma Wilma Wilma Dino Fred Fred Fred Fred Dino Dino Dino Wilma Shell Sorting

21 ► Second Pass: gap=2 Pebbles Dino Dino Dino Barney BarneyBarney Barney Dino Pebbles Pebbles Pebbles Fred Fred Fred Fred Wilma WilmaWilma Wilma Shell Sorting

22 ► Third Pass: gap=2 Dino Dino Dino Dino Barney BarneyBarney Barney Pebbles Pebbles Pebbles Pebbles Fred Fred Fred Fred Wilma WilmaWilma Wilma Shell Sorting

23 ► Fourth Pass: gap=1 Dino Barney Barney Barney Barney Barney DinoDino Dino Dino Pebbles Pebbles Pebbles Fred Fred Fred Fred Fred Pebbles Pebbles Wilma WilmaWilma Wilma Wilma Shell Sorting

24 ► Fifth Pass: gap=1 Barney Barney Barney Barney Barney Dino DinoDino Dino Dino Fred Fred Fred Fred Fred Pebbles Pebbles Pebbles Pebbles Pebbles Wilma WilmaWilma Wilma Wilma Shell Sort is complete (next gap=0) Shell Sorting

25 ► Number of comparisons in this example Bubble Sortvs Shell Sort 10 14 => Bubble sort outperformed ► Shell outperforms on long lists (30 or more items) Sorting

26 ► Sequential search  Search sequentially element by element from start ► Binary search  Repeated divide into halves retaining the half where the quary may lie Searching in a Sorted List

27 End


Download ppt "Introduction to Computing Dr. Nadeem A Khan. Lecture 28."

Similar presentations


Ads by Google