[HELP] Finding the averages on a 2D array (Diagonally)[Solved]

Posts 1–6 of 6 · Page 1 of 1
[HELP] Finding the averages on a 2D array (Diagonally)[Solved]
So i was able to figure out the average of each row and column, now i want to find the average diagonally.

Here is what i have so far.

[php] Private Sub arrayavg()
'6 by 6 (2D) array with random number in the range 1 to 10.
'Then determine the average of each row and each column.
'In addition, determine the average of each diagonal.

lstout.Items.Clear()

Dim square(6, 6) As Integer
Dim row, col As Integer
Dim rndnum As New Random 'Generates Random Numbers, I have it set 1-10
Dim sum As Integer = 0
Dim ave As Double = 0
Dim sum1 As Integer = 0
Dim sum2 As Integer = 0

For row = 1 To 6
sum = 0
For col = 1 To 6
square(row, col) = rndnum.Next(1, 10)
sum = sum + square(row, col)
Next
ave = sum / 6.0
lstout.Items.Add("ave for row " & row & " = " & Math****und(ave, 2))
Next

lstout.Items.Add(ControlChars.NewLine)

For col = 1 To 6
sum = 0
For row = 1 To 6
sum = sum + square(row, col)
Next
ave = sum / 6.0
lstout.Items.Add("ave for col " & col & " = " & Math****und(ave, 2))
Next


lstout.Items.Add(ControlChars.NewLine)

End Sub[/php]

I am absolutely clueless on how to get the average for the diagonal, help?

Note: the line :
[php]lstout.Items.Add("ave for col " & col & " = " & Math****und(ave, 2))[/php]

MATH****UND is MATH . ROUND.
Well, as long as the 2D array is a square, think about it logically. What defines a diagonal.

Here's your array:
Code:
X  X  X  X  X  X
X  X  X  X  X  X
X  X  X  X  X  X
X  X  X  X  X  X
X  X  X  X  X  X
X  X  X  X  X  X
A 6x6 2 dimensional matrix. Here's the diagonals:
Code:
O  X  X  X  X  X
X  O  X  X  X  X
X  X  O  X  X  X
X  X  X  O  X  X
X  X  X  X  O  X
X  X  X  X  X  O
Now consider the points in the matrix on the diagonal. (zero based arrays)
Code:
0,0
1,1
2,2
3,3
4,4
5,5
Pretty obvious pattern yeh?

So all you need to do is loop from 0 to the maximum and then call the value at the point (x,x) and add it to a running total. Here's an example:

[php]
Dim myMatrix(4,4) As Integer
Dim Sum As Integer = 0
If myMatrix.GetUpperBound(0) = myMatrix.GetUpperBound(1) Then 'test if the matrix is in fact a square'
For i As integer = 0 to myMatrix.GetUpperBound(0)
Sum += myMatrix(i,i)
Next i
Dim Average As Double = Sum/myMatrix.GetUpperBound(0)
MsgBox(Average)
End if
[/php]

Obviously this example wont actually work very well in practice as I never assigned any values to the array, but it gives you an idea.

[php]
Dim matrix(4, 4) As Integer
matrix(0, 0) = 3
matrix(1, 1) = 10
matrix(2, 2) = 10
matrix(3, 3) = 15
matrix(4, 4) = 4
If matrix.GetUpperBound(0) = matrix.GetUpperBound(1) Then
Dim sum As Integer = 0
For i As Integer = 1 To matrix.GetUpperBound(0)
sum += matrix(i, i)
Next
Dim Average As Double = sum / matrix.GetUpperBound(0)
MsgBox(Average)
End If
[/php]

There you go.
After much fiddling i figured out how to get the average on diagonals

Here is my code
'6 by 6 (2D) array with random number in the range 1 to 10.
'Then determine the average of each row and each column.
'In addition, extra credit - determine the average of each diagonal.
[php]

lstout.Items.Clear()

Dim square(6, 6) As Integer
Dim row, col As Integer
Dim rndnum As New Random
Dim sum As Integer = 0
Dim ave As Double
Dim sumd1, sumd2 As Integer
Dim aved2, aved1 As Double
Dim line As String = " "
For row = 1 To 6
sum = 0
For col = 1 To 6
square(row, col) = rndnum.Next(1, 10)
sum = sum + square(row, col)
Next
ave = sum / 6.0
lstout.Items.Add("ave for row " & row & " = " & Math****und(ave, 2))
Next
lstout.Items.Add(ControlChars.NewLine)
For col = 1 To 6
sum = 0
For row = 1 To 6
sum = sum + square(row, col)
Next
ave = sum / 6.0
lstout.Items.Add("ave for col " & col & " = " & Math****und(ave, 2))
Next

lstout.Items.Add(ControlChars.NewLine)

For row = 1 To 6
For col = 1 To 6
line = line + Str(square(row, col)) & " "
If col Mod 6 = 0 Then
lstout.Items.Add(line)
line = " "
End If
Next
Next


sumd1 = 0
sumd2 = 0
For row = 1 To 6
For col = 1 To 6
If row = col Then
sumd1 = sumd1 + square(row, col)
'lstout.Items.Add(square(row, col))
End If
If row + col = 7 Then
lstout.Items.Add(square(row, col))
'sumd2 = sumd2 + square(row, col)
End If
Next
Next
aved1 = sumd1 / 6.0
aved2 = sumd2 / 6.0
lstout.Items.Add("diag 1 ave = " & Math****und(aved1, 2))
lstout.Items.Add("diag 2 ave = " & Math****und(aved2, 2))[/php]
Watch the bumps.
Quote Originally Posted by Lolland View Post
Watch the bumps.
Why? It's only 7 days old, he's sharing a solution to his initial problem which could be beneficial to other users reading it. It's not like he's just bumping it with "cool man" or some shit.

EDIT: After looking at your code, I devised a much simpler way of calculating both diagonals.

[php]
'define a 3x3 2D Integer Array (remembering that all arrays are zero based of course!)'
Dim matrix(2, 2) As Integer

'add values to the matrix (or square, same shit)'
matrix(0, 0) = 1
matrix(1, 0) = 2
matrix(2, 0) = 3
matrix(0, 1) = 4
matrix(1, 1) = 5
matrix(2, 1) = 6
matrix(0, 2) = 7
matrix(1, 2) = 8
matrix(2, 2) = 9

Dim DiagSum1, DiagSum2 As Integer
Dim DiagAvg1, DiagAvg2 As Double

'calculate the sum of both diagonals here'
For i As Integer = 0 To matrix.GetUpperBound(0)
DiagSum1 += matrix(i, i)
DiagSum2 += matrix(matrix.GetUpperBound(0) - i, i)
Next

'get the average of the two diagonals'
DiagAvg1 = DiagSum1 / matrix.GetUpperBound(0)
DiagAvg2 = DiagSum2 / matrix.GetUpperBound(0)
[/php]
@Above, I'm fine with bumps like this, I just reminding him (or her) that 1 week is max, most newer members don't know.

Anyways, marked solved.
Posts 1–6 of 6 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Talk with us