|
-
Aug 11th, 2008, 02:48 AM
#1
Thread Starter
New Member
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
-
Aug 11th, 2008, 03:02 AM
#2
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 11th, 2008, 06:30 AM
#3
Thread Starter
New Member
Re: Array 2*2 I need sum of row,colum, etc
Hi,
how can i make sum for column and rows
thx
-
Aug 11th, 2008, 07:50 AM
#4
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Aug 11th, 2008, 09:23 AM
#5
Thread Starter
New Member
Plz see my simple code
Hi,
Friend your kinly look at my code
to make sum for colum and rowm
Private Sub Command1_Click()
Dim x(2, 2) As Integer
Dim sum As Integer
For i = 1 To 2
For j = 1 To 2
x(i, j) = InputBox("enter")
sum = sum + x(i, 1)
Next j
Next i
Print sum
End Sub
===============
I want sum for column,and ow to make for row
I beginner i have odd question,can work in my code
===============
thx
-
Aug 11th, 2008, 10:11 AM
#6
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
-
Aug 11th, 2008, 11:17 AM
#7
Thread Starter
New Member
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
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
-
Aug 11th, 2008, 04:42 PM
#8
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|