C# Program to Find Transpose of a Matrix
Posted by Superadmin on August 15 2022 06:25:56

C# Program to Find Transpose of a Matrix

 

This is a C# Program to generate the transpose of a given matrix.

Problem Description

This C# Program Generates the Transpose of a Given Matrix.

Problem Solution

The transpose of a given matrix is formed by interchanging the rows and columns of a matrix.

Program/Source Code

Here is source code of the C# Program to Generate the Transpose of a Given Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Generate the Transpose of a Given Matrix
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace Program
{
    class Program
    {
        public static void Main(string[] args)
        {
            int m, n, i, j;
            Console.Write("Enter the Order of the Matrix : ");
            m = Convert.ToInt16(Console.ReadLine());
            n = Convert.ToInt16(Console.ReadLine());
            int[,] A = new int[10, 10];
            Console.Write("\nEnter The Matrix Elements : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    A[i, j] = Convert.ToInt16(Console.ReadLine());
                }
            }
            Console.Clear();
            Console.WriteLine("\nMatrix A : ");
            for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(A[i, j] + "\t");
 
                }
                Console.WriteLine();
            }
            Console.WriteLine("Transpose Matrix : ");
 
           for (i = 0; i < m; i++)
            {
                for (j = 0; j < n; j++)
                {
                    Console.Write(A[j, i] + "\t");
 
                }
                Console.WriteLine();
            }
            Console.Read();
        }
    }
}
Program Explanation

In this C# Program, we are reading the order of the matrix using ‘m’ and ‘n’ variables respectively. The transpose of a given matrix is formed by interchanging the rows and columns of a matrix. Using for loop we are entering the coefficient values of an element to the array variable A[i,j].

 

The transpose method is used to interchange the rows and columns. Using for loop initialize the value of ‘i’ variable as 0. Check the condition that the value of ‘i’ variable is less than the value of ‘m’ variable. If the condition is true then execute the iteration of the loop. Inside the loop another for loop is used because it’s a two dimensional matrix.

 

 

In that for loop initialize the value of ‘i’ variable as 0 and check the condition that the value of ‘i’ variable is less than the value of ‘n’ variable. If the condition is true then execute the iteration of the loop. Print the transpose value in the A[] array variable with base index ‘j’ and ‘i’ variables respectively.

Runtime Test Cases
 
Enter the Order of the Matrix : 2  2
Enter the Matrix Elements :
1 2
3 4
Matrix A :
1 2
3 4
Transpose Matrix :
1 3
2 4