How to perform a sum of individual column of 2d array and send the result to 2d or 1d array or datagridview

Answered How to perform a sum of individual column of 2d array and send the result to 2d or 1d array or datagridview

  • 12 มีนาคม 2555 12:05
     
      มีโค้ด

    I have a 2d array of data that I have performed the sum of the individual column of the 2d array. I want to send the results to a datagridview  and also will like to have the result as a 1d or 2d array.

    Below is sample of my code

    Dim Array1(,) as double
    Array1(0, 0) = 5
    	        Array1(0, 1) = 7
    	        Array1(0, 2) = 3
    	        Array1(0, 3) = 9
    	        Array1(0, 4) = 12
    	        Array1(1, 0) = 4
    	        Array1(1, 1) = 8
    	        Array1(1, 2) = 9
    	        Array1(1, 3) = 13
    	        Array1(1, 4) = 4
    	        Array1(2, 0) = 0
    	        Array1(2, 1) = -1
    	        Array1(2, 2) = -7
    	        Array1(2, 3) = 13
    	        Array1(2, 4) = 8
    	        Array1(3, 0) = 4
    	        Array1(3, 1) = 4
    	        Array1(3, 2) = 4
    	        Array1(3, 3) = 4
    	        Array1(3, 4) = 0
    
    Dim Array_Colsum As Double
            For sCol = 0 To Array1.GetUpperBound(1) - 1
                For sRow = 0 To Array1.GetUpperBound(0) - 1
                    Array_Colsum += Array1(sRow, sCol)
                Next
                dgvOmultiplier.Rows.Add(Array_Colsum)
                '   ListBox1.Items.Add(Array_Colsum)
                ' MsgBox(Array_Colsum)
                Array_Colsum = 0
    
            Next
    

    Thanks in advance

ตอบทั้งหมด

  • 12 มีนาคม 2555 12:37
     
      มีโค้ด

    Hi,

    Are you using the 2nd index in the array for the column or the 1st one?

    In other words is the 1st number your row number or your column number?

     Most people would put the row first then the column.

    If you want to put the result into an array do you want it in

    a new array or at the bottom of a column in the same array?

    Add one Button to a Form to try this please.>>

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim Array1(3, 4) As Double
            Array1(0, 0) = 5
            Array1(0, 1) = 7
            Array1(0, 2) = 3 'Here the 2nd number is a 2
            Array1(0, 3) = 9
            Array1(0, 4) = 12
            Array1(1, 0) = 4
            Array1(1, 1) = 8
            Array1(1, 2) = 9 'Here the 2nd number is a 2
            Array1(1, 3) = 13
            Array1(1, 4) = 4
            Array1(2, 0) = 0
            Array1(2, 1) = -1
            Array1(2, 2) = -7 'Here the 2nd number is a 2
            Array1(2, 3) = 13
            Array1(2, 4) = 8
            Array1(3, 0) = 4
            Array1(3, 1) = 4
            Array1(3, 2) = 4 'Here the 2nd number is a 2
            Array1(3, 3) = 4
            Array1(3, 4) = 0
    
            'To get add the values where the 2nd number is say 2 then.>>
            Dim sum As Double
            For index As Integer = 0 To Array1.GetUpperBound(0)
                sum += Array1(index, 2)
            Next
    
            'Result = 3 + 9 + (-7) + 4 = 12 - 7 + 4 = 5 + 4 = 9
            'result = 9 >>
            MessageBox.Show("Sum = " & sum.ToString)
    
        End Sub
    
    End Class




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

  • 12 มีนาคม 2555 15:34
     
     

    Hi John,

    Thanks for your reply.

    I am using the first number as row and the second number as column.

    I want to put the results in a new array.

    Thanks

  • 12 มีนาคม 2555 15:47
     
     คำตอบ มีโค้ด
          Dim RowSums As Double()
          Dim ColumnSums As Double()
    
          Dim RowCount = Array1.GetUpperBound(0) - 1
          Dim ColumnCount = Array1.GetUpperBound(1) - 1
    
          ReDim RowSums(RowCount - 1)
          ReDim ColumnSums(ColumnCount - 1)
    
          For row = 0 To RowCount - 1
             Dim sum As Double = 0
    
             For col = 0 To ColumnCount - 1
                sum += Array1(row, col)
             Next
    
             RowSums(row) = sum
          Next
    
          For col = 0 To ColumnCount - 1
             Dim sum As Double = 0
    
             For row = 0 To RowCount - 1
                sum += Array1(row, col)
             Next
    
             ColumnSums(col) = sum
          Next
    
    
       End Sub
    


    Armin

    • ทำเครื่องหมายเป็นคำตอบโดย Paul Anim 13 มีนาคม 2555 16:39
    •  
  • 13 มีนาคม 2555 16:40
     
     
    Thanks alot