+ All Categories
Home > Documents > Introduction to Computing Dr. Nadeem A Khan. Lecture 27.

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

Date post: 20-Dec-2015
Category:
View: 218 times
Download: 0 times
Share this document with a friend
Popular Tags:
32
Introduction to Introduction to Computing Computing Dr. Nadeem A Khan Dr. Nadeem A Khan
Transcript

Introduction to Introduction to ComputingComputing

Dr. Nadeem A KhanDr. Nadeem A Khan

Lecture 27Lecture 27

►Read all examples and commentsRead all examples and comments

►Practice on exercise problems Practice on exercise problems for example:for example:Exercise 7.1: 1-5, 7-11, 14,17,18, 25, 27, 31, Exercise 7.1: 1-5, 7-11, 14,17,18, 25, 27, 31,

3737Exercise 7.2: 5-8, 11,12, 15,17,21,25Exercise 7.2: 5-8, 11,12, 15,17,21,25Exercise 7.3: 1,5,9,17,19,21,24,25-28,33,35,37Exercise 7.3: 1,5,9,17,19,21,24,25-28,33,35,37Exercise 7.4: 1-6, 21, 29,31Exercise 7.4: 1-6, 21, 29,31Exercise 7.5: 1-4, 8-13, 21-23 Exercise 7.5: 1-4, 8-13, 21-23

Chapter 7: Arrays Chapter 7: Arrays (Schneider) (Schneider)

►Value assignment: Value assignment:

Dim names(1 To 3) as StringDim names(1 To 3) as String

Let names(1)=“Aslam”Let names(1)=“Aslam”Let names(2)=“Khalid”Let names(2)=“Khalid”Let names(3)=“Akbar”Let names(3)=“Akbar”Picture1.Print names(3), Picture1.Print names(3),

names(2),names(1)names(2),names(1)

ArraysArrays

► Range of an Array:Range of an Array:

Dim Dim arrayName arrayName (1 to n) As (1 to n) As varTypevarTypeDim Dim arrayName arrayName (m to n) As (m to n) As varTypevarType

e.g:e.g:Dim names(1 To 3) as StringDim names(1 To 3) as StringDim scores(10 To 20) as SingleDim scores(10 To 20) as Single

=> Range of array need not begin with 1=> Range of array need not begin with 1

Note:Dim value(5) As Integer =Dim(0 To 5) As Note:Dim value(5) As Integer =Dim(0 To 5) As IntegerInteger

Arrays Arrays

► Example1: Example1:

Display the name of the student Display the name of the student from a class of 5 who got the rank from a class of 5 who got the rank specified by the userspecified by the user

ArraysArrays

Dim student(1 To 5) As String Dim student(1 To 5) As String

Sub Form_Load ( )Sub Form_Load ( )Rem Fill data in arrayRem Fill data in arrayLet student(1)= “Samina”Let student(1)= “Samina”Let student(2)=“Akbar”Let student(2)=“Akbar”Let student(3)=“Rizwan”Let student(3)=“Rizwan”Let student(4)=“Aslam”Let student(4)=“Aslam”Let student(5)=“Iram”Let student(5)=“Iram”

End Sub End Sub

Arrays Arrays

Sub Command1_Click( )Sub Command1_Click( )Dim n As IntegerDim n As IntegerRem Access the student arrayRem Access the student arrayLet n=Val(Text1.Text)Let n=Val(Text1.Text)Picture1.ClsPicture1.ClsIf n<=5 ThenIf n<=5 Then

Picture1.Print student(n); “ got rank”; n Picture1.Print student(n); “ got rank”; n ElseElse

Picture1.Print “Rank”; n; “does not exist”Picture1.Print “Rank”; n; “does not exist”End IfEnd IfEnd Sub End Sub

ArraysArrays

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

Arrays Arrays

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

ArraysArrays

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

Solution 1: Declare an array that is large Solution 1: Declare an array that is large enoughenough

and use part of itand use part of it

Employ a counter variable: See example 2 Employ a counter variable: See example 2 of Section 7.2of Section 7.2

ArraysArrays

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

Solution 2: Use Solution 2: Use ReDimReDim

(Dynamic Array)(Dynamic Array)

ArraysArrays

►ReDim Declaration:ReDim Declaration:

ReDim ReDim arrayName arrayName (1 to n ) As (1 to n ) As varType varType

e.g:e.g:ReDim score(1 to numStudents) As Single ReDim score(1 to numStudents) As Single

=> subscripts: variables and expressions=> subscripts: variables and expressions=> Can be placed only within a procedure => Can be placed only within a procedure

Arrays Arrays

►Declaration as a Local variable:Declaration as a Local variable:

ReDim ReDim arrayName arrayName (1 to n ) As (1 to n ) As varType varType

within a procedurewithin a procedure

=> Created and erased in memory each => Created and erased in memory each time thetime the

procedure is called procedure is called

Arrays Arrays

►Declaration as Form-Level variable:Declaration as Form-Level variable:

Place as General declaration: Place as General declaration: Dim Dim arrayName arrayName ( ) As ( ) As varType varType

Place in one procedure Place in one procedure ReDim ReDim arrayName arrayName (1 to n ) As (1 to n ) As

varType varType

=>only after ReDim has been executed:=>only after ReDim has been executed:- its range is established- its range is established- useable only after it- useable only after it

Arrays Arrays

► Example 2: Example 2:

Display the name of the student Display the name of the student from a class of who got the rank from a class of who got the rank specified by the userspecified by the user

ArraysArrays

► Given in example 2:Given in example 2:

The name of the students are in a file The name of the students are in a file (CLASS.TXT) arranged according to (CLASS.TXT) arranged according to their rankstheir ranks

Class strength is given as the first Class strength is given as the first item of the fileitem of the file

ArraysArrays

Dim student( ) As String Dim student( ) As String Dim classStrength As IntegerDim classStrength As Integer

Sub Form_Load ( )Sub Form_Load ( )Dim i As Integer Dim i As Integer Rem Fill data in arrayRem Fill data in arrayOpen “CLASS.TXT” For Input As #1Open “CLASS.TXT” For Input As #1Input #1, classStrengthInput #1, classStrengthReDim student (1 To classStrength) As StringReDim student (1 To classStrength) As StringFor i=1 To classStrengthFor i=1 To classStrength

Input #1, student(i)Input #1, student(i)Next iNext iClose #1Close #1

End Sub End Sub

Arrays Arrays

Sub Command1_Click( )Sub Command1_Click( )Dim n As IntegerDim n As IntegerRem Access the student arrayRem Access the student arrayLet n=Val(Text1.Text)Let n=Val(Text1.Text)Picture1.ClsPicture1.ClsIf n<=classStrength ThenIf n<=classStrength Then

Picture1.Print student(n); “ got rank”; n Picture1.Print student(n); “ got rank”; n ElseElse

Picture1.Print “Rank”; n; “does not exist”Picture1.Print “Rank”; n; “does not exist”End IfEnd IfEnd Sub End Sub

ArraysArrays

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

ArraysArrays

► What if the class strength is not there as the What if the class strength is not there as the first item in the data file?first item in the data file?

Ans: Count the number of items in the file Ans: Count the number of items in the file first and use it to dimension (ReDim) the first and use it to dimension (ReDim) the array array

ArraysArrays

► Ordered array Ordered array vsvs Unordered arrayUnordered array

E.g:E.g:

Ascending order:Ascending order:

[each element]<=[next element][each element]<=[next element]

Using Arrays: Ordered ArraysUsing Arrays: Ordered Arrays

► Ordered array Ordered array vsvs Unordered arrayUnordered array

=> Advantage: Efficient Searching=> Advantage: Efficient Searching

Using Arrays: Ordered ArraysUsing Arrays: Ordered Arrays

► Example:Example:

Request a name and inform if in the ordered Request a name and inform if in the ordered listlist

Using Arrays: Ordered ArraysUsing Arrays: Ordered Arrays

Dim nom(1 To 5) As String ‘General DeclarationDim nom(1 To 5) As String ‘General Declaration

Sub Form_LoadSub Form_LoadRem Place the names in the array in ascending orderRem Place the names in the array in ascending orderLet nom(1) =“AKBAR”Let nom(1) =“AKBAR”Let nom(2) =“ASLAM”Let nom(2) =“ASLAM”Let nom(3) =“BUSHRA”Let nom(3) =“BUSHRA”Let nom(4) =“TONY”Let nom(4) =“TONY”Let nom(5) =“ZAID”Let nom(5) =“ZAID”End SubEnd Sub

Using Arrays: Ordered ArraysUsing Arrays: Ordered Arrays

Sub Command1_ClickSub Command1_ClickDim n As Integer, name2Find As StringDim n As Integer, name2Find As StringLet name2Find = Ucase(Trim(Text1.Text))Let name2Find = Ucase(Trim(Text1.Text))Let n=0Let n=0

DoDoLet n=n+1Let n=n+1

Loop Until (nom(n) >= name2Find) Or (n=5)Loop Until (nom(n) >= name2Find) Or (n=5)

If nom(n) =name2Find ThenIf nom(n) =name2Find ThenPicture1.Print “Found”Picture1.Print “Found”

ElseElsePicture1.Print “Not found”Picture1.Print “Not found”

End IfEnd IfEnd SubEnd Sub

=> Average search was half the array => Average search was half the array dimensiondimension

Using Arrays: Ordered ArraysUsing Arrays: Ordered Arrays

Passing Arrays Between Passing Arrays Between Procedure Procedure

Sub Command1_Click( )Sub Command1_Click( )ReDim score(1 To 5) As IntegerReDim score(1 To 5) As IntegerCall FillArray(score( ))Call FillArray(score( ))Picture1.ClsPicture1.ClsPicture1.Print “Average score is”: Sum(score( Picture1.Print “Average score is”: Sum(score( ))/5))/5

End SubEnd Sub

Sub FillArray (s( ) As Integer)Sub FillArray (s( ) As Integer)Let s(1)=85Let s(1)=85Let s(2)=92Let s(2)=92Let s(3)=75Let s(3)=75Let s(4)=68Let s(4)=68Let s(5)=84Let s(5)=84

End SubEnd Sub

Passing Arrays Passing Arrays

Function Sum (s( ) As Integer) As IntegerFunction Sum (s( ) As Integer) As IntegerDim total As Integer, index As IntegerDim total As Integer, index As IntegerRem Add up scoresRem Add up scoresLet total=0Let total=0For index =1 To 5For index =1 To 5Let total = total + s(index)Let total = total + s(index)Next indexNext indexSum = totalSum = total

End FunctionEnd Function

Passing Arrays Passing Arrays

► What if the dimension of the array is not What if the dimension of the array is not known before hand?known before hand?

Ans: Passing on both the array as well its Ans: Passing on both the array as well its dimension as two arguments will be requireddimension as two arguments will be required

Passing ArraysPassing Arrays

End End


Recommended