Arrays... HELP!

ckeezer

Well-known member
Joined
Jan 16, 2006
Messages
100
Programming Experience
1-3
Alright. I am having a huge issue understanding arrays and why I would want to use them. I am in school and we have a project due that involves them. I have a set array , Array1(3,4)

Now that should give me 4 rows and 5 columns.... Right?

Now what I need to be able to do is to add all of the data in row1 and get the total for the entire array. This is what I am having a huge issue with.

Here are my values:
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

Do I need to setup different arrays or can I use the same array and just do the math?

Please note that I do not want the actual code to do this, I would never learn if anyone did that.. I just need to know what I am supposed to do to get the answers. If any of you can help.... I would really appreciate it.

Thanks,
Chuck
 
To add all data in row 1 you need to create a variable (TotalRow1 for insntace) to to hold the result and use a loop (For...Next) starting from index 0 to index 4 inside which you will add your array element item (Array1(0,Index)).

For the entire array you also neet to create a variable for you result that you fill feed using a for each...next loop. Since it's not very intuitive I am including the code (I am assuming Array1's type is Integer) :

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Total [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2] = 0
[/SIZE][SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] Element [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] Array1
Total += Element
[/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE]
 
As Bob Langlade's solution suggests, you pretty much always use a For or For Each loop to traverse an array. Keep in mind that programming structures usually mimic "objects" in the real world. Let's say you're sitting in class and your teacher wants to add up the total score for a test that you all took. They would start with the number zero and then work there way along the first row of desks and ask the person there for there score, adding it to the running total. Once they got to the end of the first row they would repeat that process for each row of desks. Do you see how this is basically using two nested loops (one for the desks in a row and one for the rows in the classroom) to traverse a 2D array (the students at the desks) and pretty much doing exactly what you want to do? You just need to think about the actual steps you would take to do it in the real world and translate them to lines of code. Also, I admire your stipulation that you want to write the code yourself.
 
Still Trying

Ok I am getting the hang of it... but I am not getting any results from the code that I have written. Can you tell me why? See code below:

Private Sub mnuPerformAction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPerformAction.Click
Dim RowIndex As Integer ' stores the row number
Dim ColumnIndex As Integer ' stores the column number
Dim MaxRowIndex As Integer = Array1.GetUpperBound(0) ' stores the highest row index
Dim MaxColumnIndex As Integer = Array1.GetUpperBound(1) 'stores the highest column index
Dim Sum As Integer = 0 ' stores the calculated sum
Dim Average As Single = 0.0 ' stores the calculated average
Dim Message As String ' accumulates the message we want to show the user
Dim Response As DialogResult ' stores the user’s choice of Yes or No when exiting

If mnuSumRows.Checked = True Then
Message = "Sum Rows" & ControlChars.NewLine
' loop through the rows
For RowIndex = 0 To MaxRowIndex
' loop through the columns
For ColumnIndex = 0 To MaxColumnIndex
' accumulate the Sum
Sum += Array1(RowIndex, ColumnIndex)
' continue the inner loop until all columns have been summed
Next
' accumulate the Sum in the message we want to display to the user
Message &= Sum & ControlChars.NewLine
' reset Sum so we can accumulate the next row
Sum = 0
' continue the outer loop until all rows have been summed
Next
End If
End Sub
 
Place a breakpoint on the first line of your code. To do this click in the left hand margin or click the line and press F9. When you run your code, execution will halt at that point and you can step through it one line at a time by pressing F10. You can then mouse over variables to see what they contain. You can also use the Autos, Locals and watch windows to see the values of variables and properties and evaluate any expression you like. Before you step to the next line each time, think about what you expect to happen and then when you step see if that does happen.

I can see what your issue is but I'll give you the opportunity to debug first. If you can't find the issue then post back. Here's a clue: you're trying to keep a running total as you examine each element. That running total should increase each time.
 
Thanks for the help

Private Sub mnuPerformAction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuPerformAction.Click

Dim RowIndex As Integer ' stores the row number
Dim ColumnIndex As Integer ' stores the column number
Dim MaxRowIndex As Integer = Array1.GetUpperBound(0) ' stores the highest row index
Dim MaxColumnIndex As Integer = Array1.GetUpperBound(1) 'stores the highest column index
Dim Sum As Integer = 0 ' stores the calculated sum
Dim Message As String ' accumulates the message we want to show the user
Dim Response As DialogResult ' stores the user’s choice of Yes or No when exiting
If mnuSumRows.Checked = True Then
Message = "Sum Rows:" & ControlChars.NewLine
' loop through the rows
For RowIndex = 0 To MaxRowIndex
' loop through the columns
For ColumnIndex = 0 To MaxColumnIndex
' accumulate the Sum
Sum += Array1(RowIndex, ColumnIndex)
' continue the inner loop until all columns have been summed
Next
' accumulate the Sum in the message we want to display to the user
Message &= Sum & ControlChars.NewLine
' reset Sum so we can accumulate the next row
Sum = 0
' continue the outer loop until all rows have been summed
Next
End If
 
Sorry... left some out.

I was not getting any results because I had not added a messagebox.show statment.

Message &= "Do you want to Continue?"
Response = MessageBox.Show(Message, "Results", MessageBoxButtons.YesNo, MessageBoxIcon.Information)
If Response = DialogResult.No Then
Me.Close()
EndIf

Now the code works just fine. Gives me a messagebox with the name of the function that I am doing (SumRows, Sum Columns, Avg Rows, Avg Columns), then performs the calculations and displays them each on their own lines in the same message box. See below:

C:\Documents and Settings\Chuck\Desktop\untitled.jpg
 

Attachments

  • untitled.JPG
    untitled.JPG
    19.3 KB · Views: 87
So you do want individual values for each row. I assumed that the issue was that you wanted a single value for the whole array and it was not working because you were resetting the Sum variable. All's well that ends well I guess.
 
Got it

Sorry I did not specify that clearly. I needed the total for each row, displayed on a line of its own... not the total of all rows. Thanks again for all the help. I learned alot from you all and thank you for also not giving me the answers.

Chuck
 
Back
Top