How to display transpose of a matrix in C#

1 Answer

0 votes
using System;

class Program
{
    static void Main() {
        int[,] matrix = {     
                        {1, 2, 3, 5},  
                        {4, 5, 6, 1},  
                        {7, 8, 9, 0}  
                    };  

        int rows = matrix.GetLength(0);  
        int cols = matrix.GetLength(1);  
 
        for (int i = 0; i < cols; i++) {
            for (int j = 0; j < rows; j++) {
                Console.Write(matrix[j, i] + " ");
            }
            Console.WriteLine();
        }
    }
}




/*
run:
   
1 4 7 
2 5 8 
3 6 9 
5 1 0 
   
*/

 


 



answered Jan 13, 2022 by avibootz

Related questions

1 answer 161 views
1 answer 179 views
1 answer 182 views
1 answer 305 views
1 answer 310 views
1 answer 145 views
...