Write a program in C# Sharp for multiplication of two square Matrices ?

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

Write a program in C# Sharp for multiplication of two square Matrices ?
Test Data :
Input the rows and columns of first matrix : 2 2
Input the rows and columns of second 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
Input elements in the second matrix :
element - [0],[0] : 5
element - [0],[1] : 6
element - [1],[0] : 7
element - [1],[1] : 8
Expected Output :
The First matrix is :
1 2
3 4
The Second matrix is :
5 6
7 8
The multiplication of two matrix is :
19 22
43 50

الأجوبة

using System;  
public class Exercise21  
{  
    public static void Main()
{
        int i,j,k,r1,c1,r2,c2,sum=0;
      	int[,] arr1 = new int[50,50];
		int[,] brr1 = new int[50,50];
		int[,] crr1 = new int[50,50];
       Console.Write("\n\nMultiplication of two Matrices\n");
       Console.Write("----------------------------------\n");  
  Console.Write("\nInput the number of rows and columns of the first matrix :\n");	   
  Console.Write("Rows : ");
  r1 = Convert.ToInt32(Console.ReadLine());
  Console.Write("Columns : ");  
  c1 = Convert.ToInt32(Console.ReadLine());
  Console.Write("\nInput the number of rows of the second matrix :\n");  
  Console.Write("Rows : ");
  r2 = Convert.ToInt32(Console.ReadLine());
  Console.Write("Columns : ");   
  c2 = Convert.ToInt32(Console.ReadLine());  
  if(c1!=r2){
      Console.Write("Mutiplication of Matrix is not possible.");
      Console.Write("\nColumn of first matrix and row of second matrix must be same.");
  }
  else
      {
       Console.Write("Input elements in the first matrix :\n");
       for(i=0;i<r1;i++)
        {
            for(j=0;j<c1;j++)
            {
	           Console.Write("element - [{0}],[{1}] : ",i,j);
			   arr1[i,j] = Convert.ToInt32(Console.ReadLine());
            }
        }   
       Console.Write("Input elements in the second matrix :\n");
       for(i=0;i<r2;i++)
        {
            for(j=0;j<c2;j++)
            {
	           Console.Write("element - [{0}],[{1}] : ",i,j);
			   brr1[i,j] = Convert.ToInt32(Console.ReadLine());
            }
        }    
 	 Console.Write("\nThe First matrix is :\n");
  		for(i=0;i<r1;i++)
    		{
      		Console.Write("\n");
      		for(j=0;j<c1;j++)
          	Console.Write("{0}\t",arr1[i,j]);
    		} 
  	Console.Write("\nThe Second matrix is :\n");
  		for(i=0;i<r2;i++)
    		{
      		Console.Write("\n");
      		for(j=0;j<c2;j++)
      		Console.Write("{0}\t",brr1[i,j]);
    		}
//multiplication of matrix
      for(i=0;i<r1;i++)
          for(j=0;j<c2;j++)
           crr1[i,j]=0;
             for(i=0;i<r1;i++)    //row of first matrix
                 { 
                   for(j=0;j<c2;j++)    //column of second matrix
                     {  
                       sum=0;
                         for(k=0;k<c1;k++)
                           sum=sum+arr1[i,k]*brr1[k,j];
                           crr1[i,j]=sum;
                     }
                 }
  Console.Write("\nThe multiplication of two matrix is : \n");
  for(i=0;i<r1;i++)
     {
        Console.Write("\n");
        for(j=0;j<c2;j++)
         {
           Console.Write("{0}\t",crr1[i,j]);
         }
     }
  }
Console.Write("\n\n");
  }
}
هل كان المحتوى مفيد؟

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

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