## 2D arrays

5 pts.
Tags:
2D arrays
VB.NET
how can i find sums of each column in 2D arrays of vb ?

Thanks. We'll let you know when a new response is added.

The following Code snippet creates a two dimentional array of integers that contains five columns of ten digits. The digits in each column are set equal to the column index by the procedure named FillArray (The first column contains 0’s, the second contains 1’s and so on). The procedure named Sum demonstrates only one of many ways that the sum of the columns in a 2D array can be performed in vb.

‘ Project name: Sum Columns
‘ Project purpose: The project sums the columns of a two dimentional Array

Option Explicit On
Option Strict On

Public Class SumColumns

Private Const numRows As Integer = 10I
Private Const numColumns As Integer = 5I

Private arrayName(numRows – 1, numColumns – 1) As Integer

Private sumCol0 As Integer
Private sumCol1 As Integer
Private sumCol2 As Integer
Private sumCol3 As Integer
Private sumCol4 As Integer

Private Sub FillArray()
For columnIdx As Integer = 0 To numColumns – 1
For rowIdx As Integer = 0 To numRows – 1
arrayName(rowIdx, columnIdx) = columnIdx
Next
Next
End Sub

Private Sub Sum()

For row As Integer = 0 To numRows – 1
sumCol0 = sumCol0 + arrayName(row, 0)
sumCol1 = sumCol1 + arrayName(row, 1)
sumCol2 = sumCol2 + arrayName(row, 2)
sumCol3 = sumCol3 + arrayName(row, 3)
sumCol4 = sumCol4 + arrayName(row, 4)
Next

End Sub

Private Sub exitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles exitButton.Click
Me.Close()
End Sub

Private Sub sumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles sumButton.Click
Call Sum()
sum0Label.Text = sumCol0.ToString
sum1Label.Text = sumCol1.ToString
sum2Label.Text = sumCol2.ToString
sum3Label.Text = sumCol3.ToString
sum4Label.Text = sumCol4.ToString

End Sub

Private Sub SumColumns_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Call FillArray()

End Sub
End Class