Write a program in C# Sharp to find transpose of a given matrix ?

  • برمجة سي شارب
  • برمجة

Write a program in C# Sharp to find transpose of a given matrix ?
Test Data :
Input the rows and columns of the matrix : 2 2
Input elements in the first matrix :
element - [0],[0] : 1
element - [0],[1] : 2
element - [1],[0] : 3
element - [1],[1] : 4
Expected Output :
The matrix is :
1 2
3 4
The Transpose of a matrix is :
1 3
2 4

الأجوبة

using System;  
public class Exercise22 
{  
    public static void Main()
      {
        int i,j,r,c;
      	int[,] arr1 = new int[50,50];
		int[,] brr1 = new int[50,50];
       Console.Write("\n\nTranspose of a Matrix :\n");
       Console.Write("---------------------------\n"); 
  Console.Write("\nInput the number of rows and columns of the first matrix :\n");	   
  Console.Write("Rows : ");
  r = Convert.ToInt32(Console.ReadLine());
  Console.Write("Columns : ");  
  c = Convert.ToInt32(Console.ReadLine());	   
       Console.Write("Input elements in the matrix :\n");
       for(i=0;i<r;i++)
        {
            for(j=0;j<c;j++)
            {
	           Console.Write("element - [{0}],[{1}] : ",i,j);
			   arr1[i,j]=Convert.ToInt32(Console.ReadLine());	 
            }
        } 
 	 Console.Write("\nThe First matrix is :\n");
  		for(i=0;i<r;i++)
    		{
      		Console.Write("\n");
      		for(j=0;j<c;j++)
          	Console.Write("{0}\t",arr1[i,j]);
    		}
  for(i=0;i<r;i++)
      {
      for(j=0;j<c;j++)
           {
                   brr1[j,i]=arr1[i,j];
            }
       }
      Console.Write("\n\nThe Transpose of a matrix is : ");
      for(i=0;i<c;i++)
	  {
      Console.Write("\n");
      for(j=0;j<r;j++)
	  {
           Console.Write("{0}\t",brr1[i,j]);
      }
  }
      Console.Write("\n\n");
	  }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...