Write a Program to find the transpose a matrix
For example
Input:
[1,2,3]
[4,5,6]
[7,8,9]
Output:
[1,4,7]
[2,5,8]
[3,6,9]
Solution:
using System;
namespace TransposedMatrix
{
class Program
{
static void Main()
{
int[,] a = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
var rows = a.GetLength(0);
var cols = a.GetLength(1);
var t = new int[cols, rows];
for (var i = 0; i < cols; i++)
{
for (var j = 0; j < rows; j++)
{
t[i, j] = a[j, i];
}
}
Console.WriteLine("Transposed matrix: ");
for (var i = 0; i < cols; i++)
{
for (var j = 0; j < rows; j++)
{
Console.Write(t[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
}
}