Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,705 questions

55,464 answers

573 users

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 240 views
2 answers 275 views
2 answers 304 views
2 answers 240 views
2 answers 244 views
...