How to find the sum of boundary elements of a matrix in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Private Shared Function getBoundarySum(ByVal matrix As Integer(,)) As Integer
        Dim rows As Integer = matrix.GetLength(0)
        Dim cols As Integer = matrix.GetLength(1)
        Dim sum As Integer = 0

        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                If i = 0 OrElse j = 0 OrElse i = rows - 1 OrElse j = cols - 1 Then
                    sum += matrix(i, j)
                End If
            Next
        Next

        Return sum
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim matrix As Integer(,) = { {1, 2, 3, 4},
									 {5, 6, 7, 8},
									 {9, 10, 11, 12} }

        Console.WriteLine(getBoundarySum(matrix))
    End Sub
End Class





' run:
'
' 65
'

 



answered Jun 17, 2023 by avibootz

Related questions

...