Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

vb6-array-examples

Uploaded by

cyrusjhon2004
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

vb6-array-examples

Uploaded by

cyrusjhon2004
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

'==========================================================

' VB6 Arrays - Comprehensive Examples and Applications


'==========================================================

'----------------------------------------------------------
' 1. Basic Array Declaration and Initialization
'----------------------------------------------------------
Private Sub DemoBasicArrays()
' Fixed-size array declaration
Dim numbers(4) As Integer ' Creates array with 5 elements (0 to 4)

' Initialize array elements


numbers(0) = 10
numbers(1) = 20
numbers(2) = 30
numbers(3) = 40
numbers(4) = 50

' Dynamic array declaration


Dim dynamicArray() As String
ReDim dynamicArray(2) ' Resize to 3 elements

' Initialize dynamic array


dynamicArray(0) = "Apple"
dynamicArray(1) = "Banana"
dynamicArray(2) = "Orange"

' Print elements


Dim i As Integer
For i = 0 To UBound(numbers)
Debug.Print "numbers(" & i & ") = " & numbers(i)
Next i
End Sub

'----------------------------------------------------------
' 2. Array Operations with Control Structures
'----------------------------------------------------------
Private Sub ArrayOperations()
Dim scores(9) As Integer
Dim i As Integer

' Initialize array using For loop


For i = 0 To 9
scores(i) = Int(Rnd * 100) + 1 ' Random numbers 1-100
Next i

' Calculate average using While loop


Dim total As Long
Dim count As Integer
count = 0

While count <= UBound(scores)


total = total + scores(count)
count = count + 1
Wend

Dim average As Double


average = total / (UBound(scores) + 1)
' Find passing and failing scores using If-Else
Dim passing As Integer
Dim failing As Integer

For i = 0 To UBound(scores)
If scores(i) >= 60 Then
passing = passing + 1
Else
failing = failing + 1
End If
Next i

' Output results


Debug.Print "Class Statistics:"
Debug.Print "Average Score: " & Format(average, "0.00")
Debug.Print "Passing Students: " & passing
Debug.Print "Failing Students: " & failing
End Sub

'----------------------------------------------------------
' 3. Multi-Dimensional Arrays
'----------------------------------------------------------
Private Sub MultiDimensionalArrayDemo()
' Declare 2D array for student grades (3 students, 4 subjects)
Dim grades(2, 3) As Integer
Dim student As Integer
Dim subject As Integer

' Initialize grades using nested loops


For student = 0 To 2
For subject = 0 To 3
grades(student, subject) = Int(Rnd * 40) + 60 ' Random grades 60-100
Next subject
Next student

' Calculate student averages


For student = 0 To 2
Dim studentTotal As Integer
For subject = 0 To 3
studentTotal = studentTotal + grades(student, subject)
Next subject

Debug.Print "Student " & (student + 1) & " Average: " & _
Format(studentTotal / 4, "0.00")
Next student
End Sub

'----------------------------------------------------------
' 4. Dynamic Array Manipulation
'----------------------------------------------------------
Private Sub DynamicArrayManipulation()
Dim numbers() As Integer
Dim size As Integer
size = 5

' Initial array size


ReDim numbers(size - 1)

' Fill array


Dim i As Integer
For i = 0 To UBound(numbers)
numbers(i) = i * 2
Next i

' Grow array while preserving data


ReDim Preserve numbers(UBound(numbers) + 3)

' Fill new elements


For i = size To UBound(numbers)
numbers(i) = i * 2
Next i

' Print all elements


For i = 0 To UBound(numbers)
Debug.Print "Element " & i & ": " & numbers(i)
Next i
End Sub

'----------------------------------------------------------
' 5. Array Searching and Sorting
'----------------------------------------------------------
Private Sub ArraySearchAndSort()
Dim numbers(9) As Integer
Dim i As Integer, j As Integer

' Initialize array with random numbers


For i = 0 To 9
numbers(i) = Int(Rnd * 100) + 1
Next i

' Bubble Sort implementation


Dim temp As Integer
For i = 0 To UBound(numbers) - 1
For j = i + 1 To UBound(numbers)
If numbers(i) > numbers(j) Then
' Swap elements
temp = numbers(i)
numbers(i) = numbers(j)
numbers(j) = temp
End If
Next j
Next i

' Linear Search implementation


Dim searchValue As Integer
Dim found As Boolean
searchValue = 50

For i = 0 To UBound(numbers)
If numbers(i) = searchValue Then
found = True
Debug.Print "Value " & searchValue & " found at index " & i
Exit For
End If
Next i

If Not found Then


Debug.Print "Value " & searchValue & " not found in array"
End If
End Sub

'----------------------------------------------------------
' 6. Array Utility Functions
'----------------------------------------------------------
Private Function FindMaxValue(arr() As Integer) As Integer
Dim maxVal As Integer
Dim i As Integer

maxVal = arr(0) ' Assume first element is maximum

For i = 1 To UBound(arr)
If arr(i) > maxVal Then
maxVal = arr(i)
End If
Next i

FindMaxValue = maxVal
End Function

Private Function FindMinValue(arr() As Integer) As Integer


Dim minVal As Integer
Dim i As Integer

minVal = arr(0) ' Assume first element is minimum

For i = 1 To UBound(arr)
If arr(i) < minVal Then
minVal = arr(i)
End If
Next i

FindMinValue = minVal
End Function

'----------------------------------------------------------
' 7. Array Application Example: Simple Inventory System
'----------------------------------------------------------
Private Type Product
Name As String
Quantity As Integer
Price As Double
End Type

Private Sub InventoryManagement()


Dim inventory() As Product
ReDim inventory(4) ' Create space for 5 products

' Initialize inventory


With inventory(0)
.Name = "Laptop"
.Quantity = 10
.Price = 999.99
End With

With inventory(1)
.Name = "Mouse"
.Quantity = 50
.Price = 29.99
End With

' Add more products...

' Display inventory report


Dim i As Integer
Dim totalValue As Double

Debug.Print "Inventory Report"


Debug.Print String(50, "-")
Debug.Print "Item" & Space(15) & "Quantity" & Space(8) & "Price" & Space(8) &
"Total"
Debug.Print String(50, "-")

For i = 0 To UBound(inventory)
If inventory(i).Name <> "" Then ' Check if product exists
Dim itemTotal As Double
itemTotal = inventory(i).Quantity * inventory(i).Price
totalValue = totalValue + itemTotal

Debug.Print inventory(i).Name & Space(19 - Len(inventory(i).Name)) & _


inventory(i).Quantity & Space(16 - Len(Str$
(inventory(i).Quantity))) & _
Format(inventory(i).Price, "$#,##0.00") & Space(8) & _
Format(itemTotal, "$#,##0.00")
End If
Next i

Debug.Print String(50, "-")


Debug.Print "Total Inventory Value: " & Format(totalValue, "$#,##0.00")
End Sub

You might also like