0% found this document useful (0 votes)
3 views

vb6-array-types-continued

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)
3 views

vb6-array-types-continued

Uploaded by

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

' Example 1: Chess Board (Continued)

Private Sub MultiDimensionalExample1()


Dim chessBoard(7, 7) As String ' 8x8 board

' Initialize empty board


Dim row As Integer, col As Integer
For row = 0 To 7
For col = 0 To 7
chessBoard(row, col) = "--"
Next col
Next row

' Set up white pieces


chessBoard(7, 0) = "WR" ' White Rook
chessBoard(7, 1) = "WN" ' White Knight
chessBoard(7, 2) = "WB" ' White Bishop
chessBoard(7, 3) = "WQ" ' White Queen
chessBoard(7, 4) = "WK" ' White King
chessBoard(7, 5) = "WB" ' White Bishop
chessBoard(7, 6) = "WN" ' White Knight
chessBoard(7, 7) = "WR" ' White Rook

' Set up black pieces


chessBoard(0, 0) = "BR" ' Black Rook
chessBoard(0, 1) = "BN" ' Black Knight
chessBoard(0, 2) = "BB" ' Black Bishop
chessBoard(0, 3) = "BQ" ' Black Queen
chessBoard(0, 4) = "BK" ' Black Bishop
chessBoard(0, 5) = "BB" ' Black Knight
chessBoard(0, 6) = "BN" ' Black Rook
chessBoard(0, 7) = "BR" ' Black Rook

' Display the board


Debug.Print "Chess Board:"
Debug.Print String(33, "-")
For row = 0 To 7
Debug.Print "| ";
For col = 0 To 7
Debug.Print chessBoard(row, col) & " | ";
Next col
Debug.Print
Debug.Print String(33, "-")
Next row
End Sub

' Example 2: Student Grade Tracker


Private Sub MultiDimensionalExample2()
' Array structure: student, subject (Math, English, Science, History)
Dim grades(4, 3) As Integer
Dim students(4) As String
Dim subjects(3) As String

' Initialize student names


students(0) = "John"
students(1) = "Jane"
students(2) = "Bob"
students(3) = "Alice"
students(4) = "Charlie"
' Initialize subject names
subjects(0) = "Math"
subjects(1) = "English"
subjects(2) = "Science"
subjects(3) = "History"

' Assign random grades


Dim i As Integer, j As Integer
For i = 0 To 4 ' For each student
For j = 0 To 3 ' For each subject
grades(i, j) = Int(Rnd * 41) + 60 ' Random grade 60-100
Next j
Next i

' Display grade report


Debug.Print "Student Grade Report:"
Debug.Print "Student" & Space(10) & "Math English Science History Average"
Debug.Print String(60, "-")

For i = 0 To 4
Dim total As Integer
Debug.Print students(i) & Space(16 - Len(students(i)));

For j = 0 To 3
Debug.Print Format(grades(i, j), "00") & Space(6);
total = total + grades(i, j)
Next j

Debug.Print Format(total / 4, "0.0")


Next i
End Sub

' Example 3: Monthly Sales by Region


Private Sub MultiDimensionalExample3()
' Array structure: region, month
Dim sales(3, 11) As Currency ' 4 regions, 12 months
Dim regions(3) As String
Dim months(11) As String

' Initialize regions and months


regions(0) = "North"
regions(1) = "South"
regions(2) = "East"
regions(3) = "West"

months(0) = "Jan": months(1) = "Feb": months(2) = "Mar"


months(3) = "Apr": months(4) = "May": months(5) = "Jun"
months(6) = "Jul": months(7) = "Aug": months(8) = "Sep"
months(9) = "Oct": months(10) = "Nov": months(11) = "Dec"

' Generate random sales data


Dim i As Integer, j As Integer
For i = 0 To 3
For j = 0 To 11
sales(i, j) = (Rnd * 50000) + 10000 ' Random sales 10000-60000
Next j
Next i

' Display sales report


Debug.Print "Regional Monthly Sales Report"
Debug.Print "Region" & Space(8) & "Total Sales" & Space(4) & "Average"
Debug.Print String(50, "-")

For i = 0 To 3
Dim total As Currency
For j = 0 To 11
total = total + sales(i, j)
Next j

Debug.Print regions(i) & Space(12 - Len(regions(i))) & _


Format(total, "$#,##0") & Space(4) & _
Format(total / 12, "$#,##0")
Next i
End Sub

' Example 4: Seating Chart


Private Sub MultiDimensionalExample4()
' Array structure: row, seat
Dim seating(5, 9) As String ' 6 rows, 10 seats per row
Dim seatStatus(5, 9) As Boolean ' True if occupied

' Initialize all seats as empty


Dim row As Integer, seat As Integer
For row = 0 To 5
For seat = 0 To 9
seating(row, seat) = "---"
seatStatus(row, seat) = False
Next seat
Next row

' Simulate some reservations


seating(0, 4) = "A01": seatStatus(0, 4) = True
seating(2, 3) = "B12": seatStatus(2, 3) = True
seating(4, 7) = "C23": seatStatus(4, 7) = True

' Display seating chart


Debug.Print "Theater Seating Chart"
Debug.Print "SCREEN"
Debug.Print String(50, "-")

For row = 0 To 5
Debug.Print "Row " & (row + 1) & ": ";
For seat = 0 To 9
If seatStatus(row, seat) Then
Debug.Print "[" & seating(row, seat) & "] ";
Else
Debug.Print "[ ] ";
End If
Next seat
Debug.Print
Next row
End Sub

' Example 5: Inventory Management System


Private Sub MultiDimensionalExample5()
' Array structure: product, attributes (ID, Name, Quantity, Price, Reorder
Level)
Dim inventory(9, 4) As Variant
' Initialize sample inventory
Dim i As Integer
For i = 0 To 9
' Product ID
inventory(i, 0) = "P" & Format(i + 1, "000")

' Product Name (sample names)


Select Case i
Case 0: inventory(i, 1) = "Laptop"
Case 1: inventory(i, 1) = "Mouse"
Case 2: inventory(i, 1) = "Keyboard"
Case 3: inventory(i, 1) = "Monitor"
Case 4: inventory(i, 1) = "Printer"
Case Else: inventory(i, 1) = "Product " & (i + 1)
End Select

' Quantity
inventory(i, 2) = Int(Rnd * 100) + 1

' Price
inventory(i, 3) = (Rnd * 1000) + 10

' Reorder Level


inventory(i, 4) = 10
Next i

' Display inventory report


Debug.Print "Inventory Management Report"
Debug.Print "ID" & Space(8) & "Product" & Space(12) & "Qty" & Space(6) & _
"Price" & Space(8) & "Reorder Level"
Debug.Print String(60, "-")

For i = 0 To 9
Debug.Print inventory(i, 0) & Space(6) & _
Left(inventory(i, 1) & Space(18), 18) & _
Format(inventory(i, 2), "000") & Space(6) & _
Format(inventory(i, 3), "$#,##0.00") & Space(4) & _
inventory(i, 4)

' Check if reorder is needed


If CInt(inventory(i, 2)) <= CInt(inventory(i, 4)) Then
Debug.Print Space(5) & "*** REORDER NEEDED ***"
End If
Next i
End Sub

You might also like