C# .Net Matrix Multiplication Program
This C#.net program is about Matrix Multiplication with output and source code.For matrix multiplication we take 2D arrays and perform multiplication operations on two arrays and store the result in third array. Matrix multiplication program is compiled and executed using microsoft visual studio and .net compiler
Matrix multiplication program Logic:
- First we declare two integer variable m and n to store the matrix dimensions entered by user
- Declare three two dimensional arrays to store matrix A, matrix B and matrix C
- Read matrix A using for loop and store it in array matrix a[m,n]
- Read matrix B using for loop and store it in array matrix b[m,n]
- Perform sum of matrix A and matrix B using for loop and store it in array matrix c[m,n].
- using c[i, j] += a[i, k] * b[k, j];
- show content of matrix a ,
- show content of matrix b and
- show content matrix c as multiplication of matrix a and matrix b
Matrix multiplication program Sourcecode
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MatrixMultiplicationProgram
{
class Program
{
static void Main(string[] args)
{
int i, j,m,n;
Console.WriteLine("Enter the Number of Rows and Columns : ");
m = Convert.ToInt32(Console.ReadLine());
n = Convert.ToInt32(Console.ReadLine());
int[,] a = new int[m, n];
Console.WriteLine("Enter the First Matrix");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
a[i, j] = int.Parse(Console.ReadLine());
}
}
int[,] b = new int[m, n];
Console.WriteLine("Enter the Second Matrix");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
b[i, j] = int.Parse(Console.ReadLine());
}
}
Console.WriteLine("First matrix is:");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(a[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("Second Matrix is :");
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
Console.Write(b[i, j] + "\t");
}
Console.WriteLine();
}
Console.WriteLine("Matrix Multiplication is :");
int[,] c = new int[m, n];
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
c[i, j] = 0;
for (int k = 0; k < 2; k++)
{
c[i, j] += a[i, k] * b[k, j];
}
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
Console.Write(c[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
Matrix Multiplication Program Output:
Enter the Number of Rows and Columns :
2
2
Enter the First Matrix
2
2
2
2
Enter the Second Matrix
2
2
2
2
First matrix is:
2 2
2 2
Second Matrix is :
2 2
2 2
Matrix Multiplication is :
8 8
8 8
0 comments:
Post a Comment