C# - Matrix - Transpose of a Matrix
C# - Matrix - Transpose of a Matrix
CODE
using System;
namespace SampleNS
{
class Program
{
public static void Main(string[] args)
{
Console.Write("Enter the number of rows of the matrix: ");
int rows = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter the number of columns of the matrix: ");
int cols = Convert.ToInt32(Console.ReadLine());
int[,] matrix = new int[rows, cols];
Console.Write("Enter the elements in the matrix...\n");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write("[{0}],[{1}] : ", i, j);
matrix[i, j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.Write("\nMatrix before Transpose:\n");
printMatrix(matrix, rows, cols);
//Find the transpose matrix
int[,] transposeMatrix = new int[cols, rows];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
transposeMatrix[j, i] = matrix[i, j];
}
}
Console.Write("\nMatrix after Transpose:\n");
printMatrix(transposeMatrix, cols, rows);
Console.ReadKey();
}
private static void printMatrix(int[,] matrix, int rows, int cols)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write("{0}\t", matrix[i, j]);
}
Console.WriteLine("");
}
}
}
}
Comments
Post a Comment