I need help here in a multiplication table program. The program asks the user through a text box the dimension of the two-dimensional array. When the dimension is retrieved, the program should print the multiplication table with the given dimension neatly in the form. The problem is, I do not know how to print the array neatly in a table format. It’s something like this as a sample output:
1 2 3 4 5
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Here’s my work.
Option Explicit
Dim maxNum As Integer
Dim multiplicationTable() As Integer
Dim x As Integer
Dim y As Integer
Private Sub cmdDisplay_Click()
cmdDisplay.Enabled = False
maxNum = Val(txtDimension.Text)
ReDim multiplicationTable(maxNum, maxNum) As Integer
For y = 1 To maxNum
For x = 1 To maxNum
multiplicationTable(x, y) = x * y
Next x
Next y
End Sub
What piece of code could make this program print the table neatly in the form?
This will print the table exactly as you show in your “neat” example. The width of each column is equal to the maximum number of digits in that column (plus one space delimiter). Some may think it would look neater with a uniform column width (=maximum number of digits in entire table) and the code could easily be modified to do this.