Array 2*2 I need sum of row,colum, etc
Hi,
my array as below I need
'============
'array
'2 3
'4 5
following
'1-I need SUM of 2+5 'equity
'2-I need SUM of 3+4 'equity
'3-I need SUM of 2+4 'column and 'row 2+3
'4-I need SUM of 3+5 'column and row 2+5
'5-I need SUM of 4+3+5'uper triangle
'6-I need SUM of 2+4+3 down triangle
Re: Array 2*2 I need sum of row,colum, etc
ineed1 = myarray(0, 0) + myarray(1, 1) ' =7
ineed2 = myarray(0, 1) + myarray(1, 0) ' = 7
Re: Array 2*2 I need sum of row,colum, etc
Hi,
how can i make sum for column and rows
thx
Re: Array 2*2 I need sum of row,colum, etc
vb Code:
for i = lbound(myarray,1) to ubound(myarray,1)
mysum = mysum + myarray(i, 0) ' or myarray(0, i) for row or column 1
next
assuming myarray is zero based
to sum all the rows and columns you need to make a loop within for the second dimension, then store the totals in another array or whatever
Re: Array 2*2 I need sum of row,colum, etc
I built a form with a list box to show the results and a command button to trigger the required calculations:
Code:
Const arrSize = 1 'one less than actual
Private Sub Command1_Click()
Dim MyData(arrSize, arrSize), RowTot(arrSize), ColTot(arrSize)
'Sample Data Array
For I = 0 To arrSize
For J = 0 To arrSize
K = K + 1
MyData(I, J) = K
Next
Next
' Row Totals
For I = 0 To arrSize
For J = 0 To arrSize
RowTot(I) = RowTot(I) + MyData(I, J)
Next
List1.AddItem "Row" & Str$(I + 1) & " total = " & Str$(RowTot(I))
Next
' Column Totals
For J = 0 To arrSize
For I = 0 To arrSize
ColTot(J) = ColTot(J) + MyData(I, J)
Next
List1.AddItem "Column" & Str$(J + 1) & " total = " & Str$(ColTot(J))
Next
' Triangle sums
For I = 0 To arrSize
For J = 0 To arrSize
If I + J >= arrSize Then LRSum = LRSum + MyData(I, J)
If I + J <= arrSize Then ULSum = ULSum + MyData(I, J)
Next
Next
List1.AddItem "Lower Right Triangle total = " & Str$(LRSum)
List1.AddItem "Upper Left Triangle total = " & Str$(ULSum)
End Sub
good job read my question plz
Const arrSize = 1 'one less than actual
Private Sub Command1_Click()
Dim MyData(arrSize, arrSize), RowTot(arrSize), ColTot(arrSize)
'Sample Data Array
For I = 0 To arrSize
For J = 0 To arrSize
K = K + 1
MyData(I, J) = K ' what is purpose of the function:(
Next
Next
' Row Totals
For I = 0 To arrSize
For J = 0 To arrSize
RowTot(I) = RowTot(I) + MyData(I, J) 'why u used rowtot(i) I mean what job of I :cry:
Next
List1.AddItem "Row" & Str$(I + 1) & " total = " & Str$(RowTot(I))
Next
' Column Totals
For J = 0 To arrSize
For I = 0 To arrSize
ColTot(J) = ColTot(J) + MyData(I, J)
Next
List1.AddItem "Column" & Str$(J + 1) & " total = " & Str$(ColTot(J))
Next
' Triangle sums
For I = 0 To arrSize
For J = 0 To arrSize
If I + J >= arrSize Then LRSum = LRSum + MyData(I, J)
If I + J <= arrSize Then ULSum = ULSum + MyData(I, J)
Next
Next
List1.AddItem "Lower Right Triangle total = " & Str$(LRSum)
List1.AddItem "Upper Left Triangle total = " & Str$(ULSum)
End Sub
Re: Array 2*2 I need sum of row,colum, etc
"' what is purpose of the function?"
That's not really a function. It's a square matrix data array with dimensions arrSize by arrSize. I built values for the data array using the loops so that the matrix matched the original data that you supplied.
"'why u used rowtot(i) I mean what job of I"
That code is providing the sum. As I increments, the numbers are being added within each row of the matrix array while going from column to column.