Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Introduction to Computing Dr. Nadeem A Khan

2 Lecture 24

3 Assignment 5 has been issued

4 Chapter 7: Array Variables (Arrays)

5 ► Array:  A list of values can be assigned  Collection of variables of the same type Array Variables (Arrays)

6 ► Array Declaration: Dim arrayName (1 to n) As varType e.g: Dim names(1 To 3) as Strings Dim scores(1 To 10) as Single Array Variables (Arrays)

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

8 ► What is what? Dim scores(1 To 3) as Single scores( ): name of the array scores(1): first element scores(2): second element scores(3): third element 1, 2, 3: subscripts Array Variables (Arrays)

9 ► What are the ranges or dimensions? Dim scores(1 To 3) as Single Dim names(1 To 10) as String Array Variables (Arrays)

10 ► 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

11 ► 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

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

13 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

14 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

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

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

17 Solution 1: Declare an array that is large enough and use part of it Arrays

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

19 ► 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

20 ► 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

21 ► 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

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

23 ► 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

24 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

25 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

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

27 ► 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


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

Similar presentations


Ads by Google