|
-
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.
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
|