How do I sum elements in a 2D array using VB.NET?
How would you code this so that it adds up the first column, and then the second column?
Dim intSales(,) As Integer = {{100000, 150000}, _
{90000, 120000}, _
{75000, 210000}, _
{88000, 50000}, _
{125000, 220000}, _
{63000, 80000}}
For intColumn As Integer = 0 To intSales.GetUpperBound(1)
For intRow As Integer = 0 To intSales.GetUpperBound(0)
' wha开发者_运维知识库t do I put here?
Next intRow
Next intColumn
Does this make sense? I'd be happy to clarify, if necessary.
Map out your values and think about it then come back with some sample code if you still need help. To get your started I will say that the first arrays index will be your "row". The second arrays index will be your "column" in that row. So you could access 120000(second row, second column) with something like intSales[1][1]
(remember, array indexes are zero based).
Dim s0 As Integer = 0
Dim s1 As Integer = 0
For i As Integer = 0 To intSales.GetLength(0) - 1
s0 += intSales(i, 0)
s1 += intSales(i, 1)
Next
You're already on the right track by creating two nested for
loops. You will need to loop through the inner array for each loop of the outside array.
I think the primary thing you're missing is that you'll need to create a temporary variable outside of the for
loops to hold the summation value(s). For example, I'm declaring sumTotal
as an Integer
:
Dim intSales(,) As Integer = {{100000, 150000}, _
{90000, 120000}, _
{75000, 210000}, _
{88000, 50000}, _
{125000, 220000}, _
{63000, 80000}}
' Declare a temporary variable to hold the sum as you loop
Dim sumTotal As Integer = 0
For intColumn As Integer = 0 To intSales.GetUpperBound(1)
For intRow As Integer = 0 To intSales.GetUpperBound(0)
' Add the value at this specific point in the array
' to the existing value of the sumTotal variable
sumTotal += intSales(intRow, intColumn)
Next intRow
Next intColumn
Notice that I'm using the special "plus-equal-to" (+=
) operator here. You may not have learned about that yet, but it's equivalent to the following code, just a little easier to read:
sumTotal = sumTotal + intSales(intRow, intColumn)
Running that code puts a value of 1,371,000 in the sumTotal
variable, which I suppose is correct. ;-)
It's been suggested that perhaps you wanted to obtain an individual result for each column of the array. In that case, you'll need to declare two temporary variables, one for each column. Then, you'll want to loop through each element in the rows of the first column, totaling the values as shown above into the first temporary variable. To obtain the results of the second column, you'll simply loop through each element in the rows of that column, summing the values just as you did for the first column.
This is merely a variation on a theme—once you understand how to loop through arrays, and sum values into a temporary variable, you have sufficient information to tackle this easily.
The only thing I would add is purely stylistic: The coding guidelines for VB.NET generally recommend against prefixing your variable names with type identifiers. In this case, you've used int
to indicate that a variable is of type Integer
.
The thing is, though, Visual Studio already gives you that information in a tooltip when you hover your mouse over the variable, and VB.NET's compiler is smart enough to throw up an error if you try to assign a value of one type to a value of a different, incompatible type. Adding the type information to the name of the variable doesn't actually give you any additional information, and it just clutters up the names, making things harder to read.
This was a recommended practice under earlier, incompatible versions of Visual Basic (back in 1998), but it's been changed to keep up with the modern evolution of the language. You're certainly free to ignore it, if you prefer, as this is purely stylistic advice that won't affect how your program runs, but I just thought I'd make the suggestion before someone else leaves you a nasty comment.
精彩评论