Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Introduction to Computing Dr. Nadeem A Khan

2 Lecture 27

3 ► Read all examples and comments ► Practice on exercise problems for example: Exercise 7.1: 1-5, 7-11, 14,17,18, 25, 27, 31, 37 Exercise 7.2: 5-8, 11,12, 15,17,21,25 Exercise 7.3: 1,5,9,17,19,21,24,25-28,33,35,37 Exercise 7.4: 1-6, 21, 29,31 Exercise 7.5: 1-4, 8-13, 21-23 Chapter 7: Arrays (Schneider)

4 ► Value assignment: Dim names(1 To 3) as String Let names(1)=“Aslam” Let names(2)=“Khalid” Let names(3)=“Akbar” Picture1.Print names(3), names(2),names(1) Arrays

5 ► Range of an Array: Dim arrayName (1 to n) As varType Dim arrayName (m to n) As varType e.g: Dim names(1 To 3) as String Dim scores(10 To 20) as Single => Range of array need not begin with 1 Note:Dim value(5) As Integer =Dim(0 To 5) As Integer Arrays

6 ► Example1: Display the name of the student from a class of 5 who got the rank specified by the user Arrays

7 Dim student(1 To 5) As String Sub Form_Load ( ) Rem Fill data in array Let student(1)= “Samina” Let student(2)=“Akbar” Let student(3)=“Rizwan” Let student(4)=“Aslam” Let student(5)=“Iram” End Sub Arrays

8 Sub Command1_Click( ) Dim n As Integer Rem Access the student array Let n=Val(Text1.Text) Picture1.Cls If n<=5 Then Picture1.Print student(n); “ got rank”; n Else Picture1.Print “Rank”; n; “does not exist” End If End Sub Arrays

9 => For repeated usage it is efficient to declare a form-level/global array and assign data once in the Form-Load event Arrays

10 ► If the amount of data is not known in advance? Arrays

11 Solution 1: Declare an array that is large enough and use part of it Employ a counter variable: See example 2 of Section 7.2 Arrays

12 ► If the amount of data is not known in advance? Solution 2: Use ReDim (Dynamic Array) Arrays

13 ► ReDim Declaration: ReDim arrayName (1 to n ) As varType e.g: ReDim score(1 to numStudents) As Single => subscripts: variables and expressions => Can be placed only within a procedure Arrays

14 ► Declaration as a Local variable: ReDim arrayName (1 to n ) As varType within a procedure => Created and erased in memory each time the procedure is called procedure is calledArrays

15 ► Declaration as Form-Level variable:  Place as General declaration: Dim arrayName ( ) As varType  Place in one procedure ReDim arrayName (1 to n ) As varType =>only after ReDim has been executed: - its range is established - useable only after it Arrays

16 ► Example 2: Display the name of the student from a class of who got the rank specified by the user Arrays

17 ► Given in example 2:  The name of the students are in a file (CLASS.TXT) arranged according to their ranks  Class strength is given as the first item of the file Arrays

18 Dim student( ) As String Dim classStrength As Integer Sub Form_Load ( ) Dim i As Integer Rem Fill data in array Open “CLASS.TXT” For Input As #1 Input #1, classStrength ReDim student (1 To classStrength) As String For i=1 To classStrength Input #1, student(i) Next i Close #1 End Sub Arrays

19 Sub Command1_Click( ) Dim n As Integer Rem Access the student array Let n=Val(Text1.Text) Picture1.Cls If n<=classStrength Then Picture1.Print student(n); “ got rank”; n Else Picture1.Print “Rank”; n; “does not exist” End If End Sub Arrays

20 ► What if the class strength is not there as the first item? Arrays

21 ► What if the class strength is not there as the first item in the data file? Ans: Count the number of items in the file first and use it to dimension (ReDim) the array Arrays

22 ► Ordered array vsUnordered array E.g:  Ascending order: [each element]<=[next element] Using Arrays: Ordered Arrays

23 ► Ordered array vsUnordered array => Advantage: Efficient Searching Using Arrays: Ordered Arrays

24 ► Example: Request a name and inform if in the ordered list Using Arrays: Ordered Arrays

25 Dim nom(1 To 5) As String ‘General Declaration Sub Form_Load Rem Place the names in the array in ascending order Let nom(1) =“AKBAR” Let nom(2) =“ASLAM” Let nom(3) =“BUSHRA” Let nom(4) =“TONY” Let nom(5) =“ZAID” End Sub Using Arrays: Ordered Arrays

26 Sub Command1_Click Dim n As Integer, name2Find As String Let name2Find = Ucase(Trim(Text1.Text)) Let n=0 Do Let n=n+1 Loop Until (nom(n) >= name2Find) Or (n=5) If nom(n) =name2Find Then Picture1.Print “Found” Else Picture1.Print “Not found” End If End Sub

27 => Average search was half the array dimension Using Arrays: Ordered Arrays

28 Passing Arrays Between Procedure

29 Sub Command1_Click( ) ReDim score(1 To 5) As Integer Call FillArray(score( )) Picture1.Cls Picture1.Print “Average score is”: Sum(score( ))/5 End Sub Sub FillArray (s( ) As Integer) Let s(1)=85 Let s(2)=92 Let s(3)=75 Let s(4)=68 Let s(5)=84 End Sub Passing Arrays

30 Function Sum (s( ) As Integer) As Integer Dim total As Integer, index As Integer Rem Add up scores Let total=0 For index =1 To 5 Let total = total + s(index) Next index Sum = total End Function Passing Arrays

31 ► What if the dimension of the array is not known before hand? Ans: Passing on both the array as well its dimension as two arguments will be required Passing Arrays

32 End


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

Similar presentations


Ads by Google