How to rotate a matrix 90 degrees clockwise in VB.NET

2 Answers

0 votes
Imports System
 
Class Program
    Private Shared Sub Rotate90Clockwise(ByVal matrix As Integer(,))
		Dim rows As Integer = matrix.GetLength(0)
		
        For i As Integer = 0 To rows - 1
            For j As Integer = i To rows - 1
                Dim temp As Integer = matrix(i, j)
                matrix(i, j) = matrix(j, i)
                matrix(j, i) = temp
            Next
        Next
 
        For i As Integer = 0 To rows - 1
            Dim j As Integer = 0, k As Integer = rows - 1
 
            While j < k
                Dim temp As Integer = matrix(i, j)
                matrix(i, j) = matrix(i, k)
                matrix(i, k) = temp
                j += 1
                k -= 1
            End While
        Next
    End Sub
 
    Private Shared Sub PrintMatrix(ByVal matrix As Integer(,))
		Dim rows As Integer = matrix.GetLength(0)

        For i As Integer = 0 To rows - 1
             For j As Integer = 0 To rows - 1
                Console.Write(matrix(i, j) & " ")
            Next
 
            Console.WriteLine()
        Next
    End Sub
 
    Public Shared Sub Main()
        Dim matrix As Integer(,) = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}}
 
        Console.WriteLine("Original Matrix:")
        PrintMatrix(matrix)
 
        Rotate90Clockwise(matrix)
 
        Console.WriteLine(Environment.NewLine & "Rotated Matrix:")
        PrintMatrix(matrix)
    End Sub
End Class


 
' run
'
' Original Matrix:
' 1 2 3 
' 4 5 6 
' 7 8 9 
' 
' Rotated Matrix:
' 7 4 1 
' 8 5 2 
' 9 6 3 
'

 



answered May 29, 2025 by avibootz
edited May 29, 2025 by avibootz
0 votes
Imports System
 
Class Program
    Private Shared Function Rotate90Clockwise(ByVal matrix As Integer(,)) As Integer(,)
		Dim rows As Integer = matrix.GetLength(0)
        Dim cols As Integer = matrix.GetLength(1)
		
        Dim rotated As Integer(,) = New Integer(3, 2) {}
 
        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                rotated(j, ROWS - 1 - i) = matrix(i, j)
            Next
        Next
 
        Return rotated
    End Function
 
    Private Shared Sub PrintMatrix(ByVal matrix As Integer(,))
        Dim rows As Integer = matrix.GetLength(0)
        Dim cols As Integer = matrix.GetLength(1)
 
        For i As Integer = 0 To rows - 1
            For j As Integer = 0 To cols - 1
                Console.Write(matrix(i, j) & " ")
            Next
 
            Console.WriteLine()
        Next
    End Sub
 
    Public Shared Sub Main()
        Dim matrix As Integer(,) = {
        {1, 2, 3, 4},
        {5, 6, 7, 8},
        {9, 10, 11, 12}}
 
        Console.WriteLine("Original Matrix:")
        PrintMatrix(matrix)
 
        Dim rotated As Integer(,) = Rotate90Clockwise(matrix)
 
        Console.WriteLine(Environment.NewLine & "Rotated Matrix:")
        PrintMatrix(rotated)
    End Sub
End Class
     
 
 
' run
'
' Original Matrix:
' 1 2 3 4 
' 5 6 7 8 
' 9 10 11 12 
' 
' Rotated Matrix:
' 9 5 1 
' 10 6 2 
' 11 7 3 
' 12 8 4 
'

 



answered May 29, 2025 by avibootz
edited May 29, 2025 by avibootz

Related questions

2 answers 189 views
2 answers 219 views
2 answers 244 views
2 answers 194 views
2 answers 180 views
...