Search This Blog

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

C# PROGRAM TO PERFORM SUBTRACTION ON MATRICES USING ARRAYS

Implementing Subtraction of Matrices Using 2D-Arrays:

using System;


namespace MatrixSubtractionProgram
{
    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 SUBTRACTION 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 SUBTRACTING Matrix2 FROM Matrix1
                    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();
        }
    }
}

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 Outputs of the above program are shown below:

Output:

  
For C Program :

C PROGRAM TO PERFORM SUBTRACTION ON MATRICES



 More posts: 

C# PROGRAM FOR ADDITION OF MATRICES USING ARRAYS


9 comments:

  1. to awesome information it pretty good about c# dot net.
    best training institute in chennai

    ReplyDelete
  2. Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
    Digital Marketing online training

    full stack developer training in pune

    full stack developer training in annanagar

    full stack developer training in tambaram

    full stack developer training in velachery

    ReplyDelete
  3. Nice information, valuable and excellent design, as share good stuff with good ideas and concepts, lots of great information and inspiration, both of which I need, thanks to offer such a helpful information here.
    python training institute in chennai
    python training in chennai
    python training in chennai

    ReplyDelete
  4. Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
    Blueprism training in annanagar

    Blueprism training in velachery

    Blueprism training in marathahalli

    ReplyDelete
  5. From your discussion I have understood that which will be better for me and which is easy to use. Really, I have liked your brilliant discussion. I will comThis is great helping material for every one visitor. You have done a great responsible person. i want to say thanks owner of this blog.
    Data science training in tambaram
    Data Science training in anna nagar
    Data Science training in chennai
    Data science training in Bangalore
    Data Science training in marathahalli
    Data Science training in btm

    ReplyDelete
  6. I found this informative and interesting blog so i think so its very useful and knowledge able.I would like to thank you for the efforts you have made in writing this article.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training

    ReplyDelete
  7. Searching for the Oracle Training in Chennai? Then come to Infycle for the best software training in Chennai. Infycle Technologies is one of the best Oracle training institute in Chennai, which offers various programs in Oracle such as Oracle PLSQL, Oracle DBA, etc., in complete hands-on practical training from professionals in the field. Along with that, the interviews will be arranged for the candidates and 200% placement assurance will be given here. To have the words above in your life, call 7502633633 to Infycle Technologies and grab a free demo to know more.Top Oracle DBA Training in Chennai

    ReplyDelete
  8. Data Science training in Chennai | Infycle Technologies

    Infycle Technologies is the best software training center in Chennai, providing amazing Data Science training in Chennai which will be realistic and taught by industry experts. Aside from practical training preparation, mock interviews will be arranged for students so that they can confidently face the interviews. All of this will be packed up by full placement assurance with the best salary package from top companies. For having a free demo session, call 7502633633.
    Best training center in Chennai

    ReplyDelete