Wednesday, August 8, 2012

matrix multiplication in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace matrix_multiplication
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, j;
            int[,] a = new int[2, 2];
            Console.WriteLine("Enter no for 2*2 matrix");
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    a[i, j] = int.Parse(Console.ReadLine());
                }
            }
            Console.WriteLine("First matrix is:");
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                  Console.Write(a[i,j]+"\t");
                }
                Console.WriteLine();
            }

          
            int[,] b = new int[2, 2];
            Console.WriteLine("Enter no for 2*2 matrix");
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    b[i, j] = int.Parse(Console.ReadLine());
                }
            }
            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[2, 2];
            for (i = 0; i < 2; i++)
            {
                for (j = 0; j < 2; 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 < 2; i++)
            {
                for (j = 0; j < 2; j++)
                {
                    Console.Write(c[i, j]+"\t");
                }
                Console.WriteLine();
            }

            Console.ReadKey();
        }
    }
}

output

Enter no for 2*2 matrix
8
7
6
0
First matrix is:
8       7
6       0
Enter no for 2*2 matrix
4
3
2
1
second matrix is:
4       3
2       1
Matrix multiplication is:
46      31
24      18

7 comments:

  1. Try to minimize it...........!

    ReplyDelete
  2. it's easy to understand

    ReplyDelete
  3. It would have been easier if the program was split into functional parts instead of one single function

    ReplyDelete
  4. //n numbers matrix multiplication

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace OOP_C_Sharp
    {
    class Matrix_mul
    {
    static void Main(string[] args)
    {
    //Matrix Multiplication using user input
    int Arow, row_column,Bcolumn;
    Console.WriteLine("How many Row & column will u take input for matrix multiplication?");
    Console.Write("\nEnter the first matrix Row numbers: ");
    Arow = int.Parse(Console.ReadLine());
    Console.Write("Enter the first matrix Column numbers: ");
    row_column = int.Parse(Console.ReadLine());
    Console.WriteLine("\nEnter the 2nd matrix row numbers: {0}",row_column);
    Console.Write("Enter the 2nd Matrix column numbers: ");
    Bcolumn = int.Parse(Console.ReadLine());

    matrixMul m = new matrixMul();
    int[,] a = new int[Arow, row_column];
    int[,] b = new int[row_column, Bcolumn];
    int[,] res = new int[ Arow,Bcolumn ];

    Console.WriteLine("\nEnter the First Matrix value: ");
    m.matrixInput(a, Arow, row_column);


    Console.WriteLine("\nEnter the 2nd Matrix value: ");
    m.matrixInput(b, row_column, Bcolumn);

    Console.WriteLine("\nFirst Matrix are: ");
    m.matrixDisplay(a, Arow, row_column);

    Console.WriteLine("\n2nd Matrix are: ");
    m.matrixDisplay(b, row_column, Bcolumn);

    Console.WriteLine("\nThe Matrix multiplication (A.B) are: ");
    int i, j;
    for (i = 0; i < Arow; i++)
    {
    for (j = 0; j < Bcolumn; j++)
    {
    res[i, j] = 0;
    for (int k = 0; k < row_column; k++)
    res[i, j] += a[i, k] * b[k, j];
    }
    }
    m.matrixDisplay(res, Arow, Bcolumn);

    Console.ReadKey();
    }
    }
    class matrixMul
    {
    public void matrixInput(int[,] arr,int r,int c)
    {
    int i, j;
    for (i = 0; i < r; i++)
    {
    for (j = 0; j < c; j++)
    arr[i, j] = int.Parse(Console.ReadLine());
    }
    }

    public void matrixDisplay(int[,] arr,int r, int c)
    {
    int i, j;
    for (i = 0; i < r; i++)
    {
    for (j = 0; j < c; j++)
    Console.Write(arr[i,j]+"\t");
    Console.WriteLine();
    }
    }
    }
    }

    ReplyDelete
  5. why we are using int k and than loop?
    i cant understand ,plz explain me......

    ReplyDelete