Results 1 to 5 of 5

Thread: [RESOLVED] Sum column in multidimensional array

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    5

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

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Sum column in multidimensional array

    try this:

    vb Code:
    1. Dim array1 As Integer()() = New Integer(3)() {}
    2. array1(0) = New Integer() {3, 6, 4, 5, 32}
    3. array1(1) = New Integer() {2, 1, 2, 7, 8}
    4. array1(2) = New Integer() {4, 5, 6, 9, 2}
    5. array1(3) = New Integer() {3, 8, 1, 3, 9}
    6. Dim NL As String = Environment.NewLine
    7. 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)
    8. Dim columnTotals(array1(0).GetUpperBound(0)) As Integer
    9. For Each a In array1
    10.     For x As Integer = 0 To a.GetUpperBound(0)
    11.         columnTotals(x) += a(x)
    12.     Next
    13. Next
    14. MsgBox("column sums: " & String.Join(",", Array.ConvertAll(columnTotals, Function(i) i.ToString)))

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    5

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

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Sum column in multidimensional array

    vb Code:
    1. Dim array1 As Integer()() = New Integer(3)() {}
    2. array1(0) = New Integer() {3, 6, 4, 5, 32}
    3. array1(1) = New Integer() {2, 1, 2, 7, 8}
    4. array1(2) = New Integer() {4, 5, 6, 9, 2}
    5. array1(3) = New Integer() {3, 8, 1, 3, 9}
    6. Dim NL As String = Environment.NewLine
    7. 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)
    8. Dim columnTotals(array1(0).GetUpperBound(0), 1) As String
    9. For Each a In array1
    10.     For x As Integer = 0 To a.GetUpperBound(0)
    11.         columnTotals(x, 0) = (CInt(columnTotals(x, 0)) + a(x)).ToString
    12.         columnTotals(x, 1) = x.ToString
    13.     Next
    14. Next
    15. 'bubble sort
    16. For outer As Integer = columnTotals.GetUpperBound(0) To 0 Step -1
    17.     For inner = 0 To outer - 1
    18.         If CInt(columnTotals(inner, 0)) > CInt(columnTotals(inner + 1, 0)) Then
    19.             Dim temp() As String = {columnTotals(inner, 0), columnTotals(inner, 1)}
    20.             columnTotals(inner, 0) = columnTotals(inner + 1, 0)
    21.             columnTotals(inner, 1) = columnTotals(inner + 1, 1)
    22.             columnTotals(inner + 1, 0) = temp(0)
    23.             columnTotals(inner + 1, 1) = temp(1)
    24.         End If
    25.     Next
    26. Next
    27. Dim msg As String = ""
    28. For x As Integer = 0 To columnTotals.GetUpperBound(0)
    29.     msg &= "index: " & columnTotals(x, 1) & ", value: " & columnTotals(x, 0) & NL
    30. Next
    31. MsgBox(msg)

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    5

    Re: Sum column in multidimensional array

    You are a lifesaver!

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
  •  



Click Here to Expand Forum to Full Width