CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays...

8
CSC 162 Visual Basic I Programming

Transcript of CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays...

Page 1: CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays –Individual Elements Sorting –Bubble Sort.

CSC 162

Visual Basic I Programming

Page 2: CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays –Individual Elements Sorting –Bubble Sort.

Array Parameters and Sorting• Array Parameters

– Entire Arrays– Individual Elements

• Sorting– Bubble Sort

Page 3: CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays –Individual Elements Sorting –Bubble Sort.

Array Parameters

• When arrays are passed to procedures, they must be passed ByRef.– Visual Basic makes this a requirement since copying

large arrays could take up too much room in memory.

Page 4: CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays –Individual Elements Sorting –Bubble Sort.

Array Parameters

• Array parameters are not specified with a size, since they are passed ByRef.

Private Sub PrintNums(ByRef intNums() As Integer)

Dim intCounter As Integer

For intCounter = LBound(intNums) To UBound(intNums)

Print intNums(intCounter)

Next intCounter

End Sub

Page 5: CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays –Individual Elements Sorting –Bubble Sort.

Array Parameters

• When arrays are passed into a procedure, the size is also not specified, since they are passed ByRef.

Dim intValues(1 To 100) As Integer

.

.

.

Call PrintNums(intValues())

Page 6: CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays –Individual Elements Sorting –Bubble Sort.

Array Parameters• However, individual elements of the array are passed just

like any single value.

Private Sub PrintValue(ByVal intValue As Integer)

Print "The selected value is " & intValue

End Sub

Dim intNumbers(1 To 50) As Integer

.

.

.

Call PrintValue(intNumbers(20))

Page 7: CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays –Individual Elements Sorting –Bubble Sort.

Bubble Sort• Arrays can be sorted in two ways:

– Ascending (1 100, A Z, etc.)

– Descending (100 1, Z A, etc.)

• Array functions:– Starting index (lower bound):LBound()– Ending index (upper bound): UBound()

Page 8: CSC 162 Visual Basic I Programming. Array Parameters and Sorting Array Parameters –Entire Arrays –Individual Elements Sorting –Bubble Sort.

Lab AssignmentWrite a program that will do the following: 1. Put 20 random (integer) numbers with a range of 1 to 100

into an array of size 20. 2. Sort the array in descending order.*3. Display the entire sorted array in a ListBox.

* Use the Bubble Sort algorithm, but put it into a sub procedure called DescendingSort.

Programming Assignment 5Due Monday, November 10 / Tuesday, November 11Page 241 #6.18Page 241 #6.19