Transpose of User Defined Matrix in C#
using System;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Row and Column Size for Matrix Separated By Single Space");
int rows, cols;
string strRowsCols = Console.ReadLine();
rows = int.Parse(strRowsCols.Substring(0, 1));
cols = int.Parse(strRowsCols.Substring(2, 1));
int j = 0;
int[,] UserMatrix = new int[rows, cols];
Console.WriteLine("Enter each Matrix Elements in Matrix Form");
for (int i = 0; i < rows; i++)
{
int[] intArrays = Array.ConvertAll(Console.ReadLine().Trim().Split(' '), a => int.Parse(a));
foreach (var k in intArrays)
{
UserMatrix[i, j] = k;
j = j + 1;
}
j = 0;
}
Console.WriteLine("Transpose Matrix");
int r = 0;
int c = 0;
int temp;
temp = rows;
rows = cols;
cols = temp;
int[,] TransposeMatrix = new int[rows, cols];
for (int k = 0; k < rows; k++)
{
for (int m = 0; m < cols; m++)
{
TransposeMatrix[k, m] = UserMatrix[r, c];
r = r + 1;
}
r = 0;
c = c + 1;
}
for (int k = 0; k < rows; k++)
{
for (int l = 0; l < cols; l++)
{
if (l < cols - 1)
Console.Write("{0} ", TransposeMatrix[k, l]);
else
Console.Write("{0}", TransposeMatrix[k, l]);
}
if (k < rows - 1)
Console.WriteLine();
}
Console.Read();
}
}
}
Test Case Output 1 :
Test Case Output 2 :
No comments:
Post a Comment