THANKS FOR WATCHING MY BLOG.IF ANY PROBLEM PLEASE COMMENT

Wednesday, 30 January 2013

Matrix multiplication – A sample program in C# using arrays

Description:



Here in this program, the number of rows and columns are taken as input for the given two matrix.  Arrays and For loop is used to get  the values for each row and columns of two matices . The total number of values are validated i.e. for a 3x2 matrix the total number of values are 6 (3 rows and 2 columns). The matrix multiplication is performed if the number of columns of the first matrix is equal to the number of rows of the second matrix. For loop is used to display the values in a matrix format. Try-catch (Exception) is used to handle the errors.



Sample code:



using System;

using System.Collections.Generic;

using System.Text;



namespace codeformatirxmultiplication

{

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                String u_a;

                string v_a;

                string u_b;

                string v_b;

                string m;

                string n;

                int[,] a = new int[4, 4];

                int[,] b = new int[4, 4];

                int[,] c = new int[4, 4];



                int i = 0;

                int j = 0;

                int k = 0;

                int l = 0;

                c[i, j] = 0;

                int no_of_rows_a = 0;

                int no_of_columns_a = 0;

                int no_of_rows_b = 0;

                int no_of_columns_b = 0;

                Console.WriteLine(" Enter the number of rows for  matrix A");

                u_a = Console.ReadLine();

                no_of_rows_a = Convert.ToInt32(u_a);

                Console.WriteLine(" Enter the number of columns for matrix A");

                v_a = Console.ReadLine();

                no_of_columns_a = Convert.ToInt32(v_a);

                Console.WriteLine(" Enter the number of rows for matrix B");

                u_b = Console.ReadLine();

                no_of_rows_b = Convert.ToInt32(u_b);

                Console.WriteLine(" Enter the number of columns for matrix B");

                v_b = Console.ReadLine();

                no_of_columns_b = Convert.ToInt32(v_b);

                Console.WriteLine(" The values of A matrix is :");

                for (i = 0; i < no_of_rows_a; i++)

                {

                    for (j = 0; j < no_of_columns_a; j++)

                    {

                        m = Console.ReadLine();

                        k = Convert.ToInt32(m);

                        a[i, j] = k;

                    }

                }

                Console.WriteLine(" The values of B matrix is :");

                for (i = 0; i < no_of_rows_b; i++)

                {

                    for (j = 0; j < no_of_columns_b; j++)

                    {

                        n = Console.ReadLine();

                        l = Convert.ToInt32(n);

                        b[i, j] = l;

                    }

                }



                Console.WriteLine(" The matrix format of A is ");



                for (i = 0; i < no_of_rows_a; i++)

                {

                    for (j = 0; j < no_of_columns_a; j++)

                    {

                        Console.Write(" {0}", a[i, j]);

                    }

                    Console.WriteLine("");

                }

                Console.WriteLine(" The matrix format of B is ");

                for (i = 0; i < no_of_rows_b; i++)

                    {

                        for (j = 0; j < no_of_columns_b; j++)

                        {

                            Console.Write(" {0}",b[i,j]);

                        }

                        Console.WriteLine("");

                }

                if (v_a == u_b)

                {

                    for (i = 0; i < no_of_rows_a; i++)

                    {

                        for (j = 0; j < no_of_columns_b; j++)

                        {

                            c[i, j] = (a[i, 0] * b[0, j]) + (a[i, 1] * b[1, j]);

                            Console.Write("  ");

                        }

                        Console.WriteLine("");

                    }

                }

                else

                {

                    Console.WriteLine("The matrix multiplication cannot be performed");



                }

                Console.WriteLine(" The resultant matrix is");

                for (i = 0; i < no_of_rows_a; i++)

                {

                    for (j = 0; j < no_of_columns_b; j++)

                    {

                        Console.Write(" {0}", c[i, j]);

                    }

                    Console.WriteLine();

                }

            }



            catch (Exception e)

            {

                Console.WriteLine("Exception occured: " + e.Message);

            }

        }



        }

   

    }



Sample Input :

Enter the number of rows for matrix A

3

 Enter the number of columns for matrix A

2

 Enter the number of rows for matrix B

2

 Enter the number of columns for matrix B

3

 The values of A matrix is :

2

3

5

1

10

2

 The values of B matrix is :

0

3

5

1

2

3

 The matrix format of A is

 2 3

 5 1

 10 2

 The matrix format of B is

 0 3 5

 1 2 3



Sample Output:



 The resultant matrix is

 3 12 19

 1 17 28
 2 34 56

No comments:

Post a Comment