Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding together rows 1

Status
Not open for further replies.

mancroft

Programmer
Oct 26, 2002
267
GB
Adding together rows

Hello

I am looking for an elegant (not brute-force) solution to this problem.

There are a number of rows with values let us say

Row 1 4
Row 2 5
Row 3 6
Row 4 7

What I need to be able to do (presumably with a loop) is for each row to add together the values of all the other rows so the answer will be:

Row 1 18
Row 2 17
Row 3 16
Row 4 15

What code will do this?

Thank you.



 
Are you after something like this:

Code:
  Private RowData(3) As Integer

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    'set up the data
    RowData(0) = 4
    RowData(1) = 5
    RowData(2) = 6
    RowData(3) = 7
    'display results in a textbox
    TextBox1.Clear()
    For a As Integer = 0 To RowData.Length - 1
      Dim x As Integer = 0   'to hold result for this iteration
      For b As Integer = 0 To RowData.Length - 1
        If a <> b Then x += RowData(b)
      Next
      TextBox1.Text += x.ToString + Environment.NewLine
    Next

  End Sub

Hope this helps.

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top