|
-
Dec 12th, 2010, 03:05 PM
#1
Thread Starter
New Member
[RESOLVED] Sum column in multidimensional array
* I posted this in VB forum at first by accident instead of .Net*
I am trying to create a console application that computes the row and column sum and prints the elements with the resulting sums for each row and column.
Module Module1
Sub Main()
Dim i, j As Integer
Dim array1 As Integer()() = New Integer(3)() {}
array1(0) = New Integer() {3, 6, 4, 5, 32}
array1(1) = New Integer() {2, 1, 2, 7, 8}
array1(2) = New Integer() {4, 5, 6, 9, 2}
array1(3) = New Integer() {3, 8, 1, 3, 9}
Dim rowsum As Integer = 0
Dim colsum As Integer = 0
For i = 0 To array1.GetUpperBound(0)
Console.Write("Elements in row " & i + 1 & " are ")
For j = 0 To array1(i).GetUpperBound(0)
Console.Write(array1(i)(j) & ", ")
rowsum += array1(i)(j)
Next
Console.Write("The sum is {0}.", rowsum)
Console.WriteLine()
rowsum = 0
Next
For j = 0 To array1.GetUpperBound(0)
For i = 0 To array1(j).GetUpperBound(0)
colsum += array1(i)(j)
Next
Console.Write("Column sums are " & colsum & ", ")
Console.WriteLine()
colsum = 0
Next
Console.ReadLine()
End Sub
End Module
My code currently prints:
Elements in row 1 are 3, 6, 4, 5, 32, The sum is 50.
Elements in row 2 are 2, 1, 2, 7, 8, The sum is 20.
Elements in row 3 are 4, 5, 6, 9, 2, The sum is 26.
Elements in row 4 are 3, 8, 1, 3, 9, The sum is 24.
Column sums are 50
Column sums are 70
Column sums are 96
Column sums are 120
My column sum code is currently showing the row sums.
I need the column sums to display in descending order. So i know it would require a sorted array, but i'm not sure how to go about doing that given i can't get the correct values to show up.
So I would need it to display as "Column sums are 12, 20, 13, 24, 51. " and then the next line would be "Column indexes are 1, 3, 2, 4, 5."
Can anyone help me?!
Last edited by hollyeva; Dec 12th, 2010 at 03:22 PM.
-
Dec 12th, 2010, 03:23 PM
#2
Re: Sum column in multidimensional array
try this:
vb Code:
Dim array1 As Integer()() = New Integer(3)() {}
array1(0) = New Integer() {3, 6, 4, 5, 32}
array1(1) = New Integer() {2, 1, 2, 7, 8}
array1(2) = New Integer() {4, 5, 6, 9, 2}
array1(3) = New Integer() {3, 8, 1, 3, 9}
Dim NL As String = Environment.NewLine
MsgBox("row 1 sum: " & array1(0).Sum & NL & "row 2 sum: " & array1(1).Sum & NL & "row 3 sum: " & array1(2).Sum & NL & "row 4 sum: " & array1(3).Sum)
Dim columnTotals(array1(0).GetUpperBound(0)) As Integer
For Each a In array1
For x As Integer = 0 To a.GetUpperBound(0)
columnTotals(x) += a(x)
Next
Next
MsgBox("column sums: " & String.Join(",", Array.ConvertAll(columnTotals, Function(i) i.ToString)))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 12th, 2010, 03:36 PM
#3
Thread Starter
New Member
Re: Sum column in multidimensional array
yes! that worked.
is there a way to display the columnTotals sorted though? and list the indexes that correspond with the totals?
thank you so much for your help!!
-
Dec 12th, 2010, 03:51 PM
#4
Re: Sum column in multidimensional array
vb Code:
Dim array1 As Integer()() = New Integer(3)() {}
array1(0) = New Integer() {3, 6, 4, 5, 32}
array1(1) = New Integer() {2, 1, 2, 7, 8}
array1(2) = New Integer() {4, 5, 6, 9, 2}
array1(3) = New Integer() {3, 8, 1, 3, 9}
Dim NL As String = Environment.NewLine
MsgBox("row 1 sum: " & array1(0).Sum & NL & "row 2 sum: " & array1(1).Sum & NL & "row 3 sum: " & array1(2).Sum & NL & "row 4 sum: " & array1(3).Sum)
Dim columnTotals(array1(0).GetUpperBound(0), 1) As String
For Each a In array1
For x As Integer = 0 To a.GetUpperBound(0)
columnTotals(x, 0) = (CInt(columnTotals(x, 0)) + a(x)).ToString
columnTotals(x, 1) = x.ToString
Next
Next
'bubble sort
For outer As Integer = columnTotals.GetUpperBound(0) To 0 Step -1
For inner = 0 To outer - 1
If CInt(columnTotals(inner, 0)) > CInt(columnTotals(inner + 1, 0)) Then
Dim temp() As String = {columnTotals(inner, 0), columnTotals(inner, 1)}
columnTotals(inner, 0) = columnTotals(inner + 1, 0)
columnTotals(inner, 1) = columnTotals(inner + 1, 1)
columnTotals(inner + 1, 0) = temp(0)
columnTotals(inner + 1, 1) = temp(1)
End If
Next
Next
Dim msg As String = ""
For x As Integer = 0 To columnTotals.GetUpperBound(0)
msg &= "index: " & columnTotals(x, 1) & ", value: " & columnTotals(x, 0) & NL
Next
MsgBox(msg)
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Dec 12th, 2010, 04:45 PM
#5
Thread Starter
New Member
Re: Sum column in multidimensional array
Tags for this Thread
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
|