Search This Blog

This website completely moved to new domain. For latest content, visit www.programmingposts.com

C# PROGRAM FOR PRODUCT OF MATRICES USING ARRAYS



C# PROGRAM FOR IMPLEMENTING PRODUCT OF MATRICES USING ARRAYS : 

using System;


namespace MatrixMultiplication
{
    class Program
    {
        static void Main(string[] args)
        {
            //using 2D-ARRAYS
            int RowSize = 5, ColSize = 5;
            int[,] Matrix1 = new int[RowSize, ColSize];
            int[,] Matrix2 = new int[RowSize, ColSize];
            int[,] ResultMatrix = new int[RowSize, ColSize];
            int i, j;


            try
            {
                Console.WriteLine("\n >>> PROGRAM To PRINT PRODUCT OF TWO MATRICES <<<");
                Console.Write("\n Enter the Size of a Matrix N*N (For Example:3*3) : ");
                string s = Console.ReadLine();
                string[] values = s.Split('*');
                RowSize = int.Parse(values[0]);
                ColSize = int.Parse(values[1]);

                if (RowSize > 5 || ColSize > 5)   //limiting the size of matrix
                {
                    Console.BackgroundColor = ConsoleColor.DarkRed; //changing background color to red
                    Console.WriteLine(" The Size Of Matrix should Be in Less Than 5 (limiting size of array)");
                    System.Console.ResetColor(); ///resetting color
                    Console.WriteLine("\n\n\t Press Enter key to exit....");
                    Console.ReadKey(); return;
                }

                else
                {

                    //Initializing all the elements to zero
                    for (i = 0; i < RowSize; i++)
                    {
                        for (j = 0; j < ColSize; j++)
                        {
                            Matrix1[i, j] = 0;
                            Matrix2[i, j] = 0;
                        }
                    }
                    //Reading elements of Matrix1
                    Console.WriteLine("\n Enter the elements of Matrix1({0}*{1})", RowSize, ColSize);
                    for (i = 0; i < RowSize; i++)
                    {
                        for (j = 0; j < ColSize; j++)
                        {
                            Console.Write(" Matrix1[{0},{1}] : ", i, j);
                            Matrix1[i, j] = Convert.ToInt32(Console.ReadLine());
                        }
                    }
                    //Reading elements of Matrix2
                    Console.WriteLine("\n Enter the elements of Matrix2({0}*{1})", RowSize, ColSize);
                    for (i = 0; i < RowSize; i++)
                    {
                        for (j = 0; j < ColSize; j++)
                        {
                            Console.Write(" Matrix2[{0},{1}] : ", i, j);
                            Matrix2[i, j] = Convert.ToInt32(Console.ReadLine());
                        }
                    }

                    //calculating ResultMatrix, by by multiplying Matrix1 and Matrix2
                    for (i = 0; i < RowSize; i++)
                    {
                        for (j = 0; j < ColSize; j++)
                        {
                            ResultMatrix[i, j] = Matrix1[i, j] * Matrix2[i, j];
                        }
                    }

                    //Printing Result Matrix
                    Console.Write("\n\n\t*** Result Matrix  ***\n\n\t");
                    for (i = 0; i < RowSize; i++)
                    {

                        for (j = 0; j < ColSize; j++)
                        {
                            if (ResultMatrix[i, j] < 10)
                            {
                                //Making number as 01,02,etc,.
                                Console.Write("  0" + Convert.ToString(ResultMatrix[i, j])); 
                            }
                            else
                            {
                                Console.Write("  " + Convert.ToString(ResultMatrix[i, j]));
                            }

                            if (j == ColSize - 1) { Console.Write("\n\t"); }

                        }
                    }
                }
            }
            catch  //to catch exceptions,suppose string entered as aRowSize or Colsize  of matrix
            {
                Console.BackgroundColor = ConsoleColor.DarkRed;
                Console.WriteLine("WARNING:only Number are allowed, Enter Correct Input");
                Console.ResetColor();
            }

            Console.WriteLine("\n\n\t Press Enter key to exit....");
            Console.ReadLine();
        }
    }
}
..
Output:
In the above program the code can be shortened by reducing the number of for loops or by using functions, but for making it easier to understand by the beginners, the program is made as simple as possible. Some of the different Sample Output of the above program is shown below:



For C Program: 

C PROGRAM TO PERFORM MATRIX MULTIPLICATION



More posts: 

C# PROGRAM FOR ADDITION OF MATRICES USING ARRAYS




4 comments:

  1. thank you for this. but I have a question what if I want to reverse the matrices in Matrix 2? for example you entered 3*2, next it finding elements for matrix1 then in the next step the value of 3*2 should be 2*3 in Matrix2, it is possible? Sorry for my bad English. Thank you in advance.

    ReplyDelete
    Replies
    1. try this, Declare 2 matrices, After entering elements of matrix1, loop its elements and fill matix2 as given below,
      for (i = 0; i < RowSize; i++)
      {
      for (j = 0; j < ColSize; j++)
      {
      Matrix2[j,i] = Matrix1[i, j];
      }
      }

      Delete
  2. It is nice article to improve knowledge.thank you.
    web programming tutorial
    welookups

    ReplyDelete