How to find the number of sorted rows in a matrix with VB.NET

1 Answer

0 votes
Imports System

Public Class Program
    Private Shared Function isRowSorted(ByVal matrix As Integer(,), ByVal row As Integer) As Boolean
        Dim cols As Integer = matrix.GetLength(1)

        For i As Integer = 1 To cols - 1
            If matrix(row, i - 1) > matrix(row, i) Then
                Return False
            End If
        Next

        Return True
    End Function

    Public Shared Sub Main(ByVal args As String())
        Dim matrix As Integer(,) = {
        		{ 1,   2,   3,   4,  0},
        		{-5,  -4,   0,   8,  9},
        		{ 2, 100,   8, 100,  3},
        		{ 1,   7, 100,   9,  6},
        		{ 9,  10,  11,  12, 13}
		}
	    Dim rows As Integer = matrix.GetLength(0)
        Dim count As Integer = 0

        For i As Integer = 0 To rows - 1
            If isRowSorted(matrix, i) Then
                count += 1
            End If
        Next

        Console.WriteLine("Total sorted rows: " & count)
    End Sub
End Class



' run:
'
' Total sorted rows: 2
'

 



answered Jun 23, 2023 by avibootz
...