Multiplication of two matrixes using 2D Array in c#,
Enter the first matrix and then display it. Secondly, enter the second matrix and then display it. In the result by apply 2D arrays show multiplication of 2 matrixes.
code:
Console.WriteLine("Enter no of rows");
int r1 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter no of columns");
int c1 = int.Parse(Console.ReadLine());
int[,] array1 = new int[r1,c1];
Console.WriteLine("\t\tmatrix Martix");
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
Console.Write("enter value at [{0},{1}]:",i,j);
array1[i, j] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine();
}
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
Console.Write(array1[i, j] + " ");
}
Console.WriteLine();
}
Console.WriteLine("\t\tSecond Martix");
Console.WriteLine("Enter no of rows");
int r2 = int.Parse(Console.ReadLine());
Console.WriteLine("Enter no of columns");
int c2 = int.Parse(Console.ReadLine());
int[,] array2 = new int[r2, c2];
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
{
Console.Write("enter value at [{0},{1}]:", i, j);
array2[i, j] = Convert.ToInt32(Console.ReadLine());
}
Console.WriteLine();
}
for (int i = 0; i < r2; i++)
{
for (int j = 0; j < c2; j++)
{
Console.Write(array2[i,j]+" ");
}
Console.WriteLine();
}
if (c1!=r2)
{
Console.WriteLine("Matrix Multiplication Is Not Possible");
}
else
{
int[,] arrray3 = new int[r1, c1];
for (int i = 0; i < arrray3.GetLength(0); i++)
{
for (int j = 0; j < arrray3.GetLength(1); j++)
{
for (int k = 0; k < arrray3.GetLength(1); k++)
{
arrray3[i, j] += array1[i, k] * array2[k, j];
}
}
}
Console.WriteLine();
for (int i = 0; i < r1; i++)
{
for (int j = 0; j < c1; j++)
{
Console.Write(arrray3[i, j] + " ");
}
Console.WriteLine();
}
}
Comments
Post a Comment