Wednesday 25 March 2015

Addition and subtraction of matrices

Question: Program to addition and subtraction of two matrix and sum of their diagonal.

Rules for matrix Addition:

 1. In matrix addition, if matrix A and matrix B are in the same order (row,column). Then, the sum of two matrix is possible. 

      Matrix C = Matrix A + Matrix B            for the same order i.e M x N.

2. Addition of matrices is an example of binary operation on the set of matrices of same order.
3. Let A and B are two matrices

  •       + B = B + A
  •        A + 0 = 0 + A = A
  •        0 + 0 = 0
Rules for Matrix Subtraction

1. In matrix subtraction, we follow the same rule, if matrix A and matrix B are in the same order (row,column). Then , the subtraction of two matrix is possible. 

          Matrix C = Matrix A - Matrix B            for the same order i.e M x N.

2. Subtraction of matrices is an example of binary operation on the set of matrices of same order.

Code Snippet:


  1. using System;  
  2. public class AddSub  
  3. {  
  4.     int m,n,row,col;  
  5.     int[,] matA = new int[10,10];  
  6.     int[,] matB = new int[10,10];  
  7.     int[,] matC = new int[10,10];  
  8.   
  9.     public AddSub(int i, int j)  
  10.     {  
  11.         m=i;  
  12.         n=j;  
  13.     }  
  14.       
  15.     public void readMat()  
  16.     {  
  17.         Console.WriteLine("Enter the Element of matrix A: ");  
  18.         for(row=0; row<m; row++)  
  19.         {  
  20.             for(col=0; col<n; col++)  
  21.             {  
  22.                 Console.Write("matA[{0},{1}] : ",row,col);  
  23.                 matA[row,col]=Convert.ToInt32(Console.ReadLine());  
  24.             }  
  25.   
  26.                 Console.WriteLine();  
  27.         }  
  28.   
  29.         Console.ReadLine();  
  30.   
  31.         Console.WriteLine("Enter the Element of matrix B: ");  
  32.         for(row=0; row<m; row++)  
  33.         {  
  34.             for(col=0; col<n; col++)  
  35.             {  
  36.                 Console.Write("matB[{0},{1}] : ",row,col);  
  37.                 matB[row,col]=Convert.ToInt32(Console.ReadLine());  
  38.             }  
  39.   
  40.                 Console.WriteLine();  
  41.         }  
  42.   
  43.         Console.ReadLine();  
  44.     }  
  45.   
  46.     public void printMat()  
  47.     {  
  48.         Console.WriteLine("Matrix A");  
  49.         for(row=0; row<m; row++)  
  50.         {  
  51.             for(col=0; col<m; col++)  
  52.                 Console.Write("{0} ",matA[row,col]);  
  53.                 Console.WriteLine();  
  54.         }  
  55.   
  56.         Console.WriteLine("Matrix B");  
  57.         for(row=0; row<m; row++)  
  58.         {  
  59.             for(col=0; col<m; col++)  
  60.                 Console.Write("{0} ",matB[row,col]);  
  61.                 Console.WriteLine();  
  62.         }  
  63.     }  
  64.   
  65.     public void Operation()  
  66.     {  
  67.         Console.Write("Type \"Add\" for Addition and \"Sub\" for Substraction : ");  
  68.         string response = Console.ReadLine();  
  69.                response = response.ToUpper();  
  70.                  
  71.   
  72.         switch(response)  
  73.         {  
  74.             case "ADD":  
  75.             {  
  76.                 for(row=0; row<m; row++)  
  77.                 {  
  78.                     for(col=0; col<n; col++)  
  79.                     {  
  80.                         matC[row,col] += matA[row,col]+matB[row,col];  
  81.                     }  
  82.                 }  
  83.   
  84.                 Console.WriteLine("Resultant is: ");  
  85.                 for(row=0; row<m; row++)  
  86.                 {  
  87.                     for(col=0; col<n; col++)  
  88.                     {  
  89.                         Console.Write("{0:D2} ", matC[row,col]);  
  90.                     }  
  91.                         Console.WriteLine();  
  92.                 }  
  93.                     Console.ReadLine();  
  94.                 break;  
  95.             }  
  96.   
  97.             case "SUB":  
  98.             {  
  99.                 for(row=0; row<m; row++)  
  100.                 {  
  101.                     for(col=0; col<n; col++)  
  102.                     {  
  103.                         matC[row,col] -= matA[row,col]-matB[row,col];  
  104.                     }  
  105.                 }  
  106.   
  107.                 Console.WriteLine("Resultant is: ");  
  108.                 for(row=0; row<m; row++)  
  109.                 {  
  110.                     for(col=0; col<n; col++)  
  111.                     {  
  112.                         Console.Write("{0:D2} ", matC[row,col]);  
  113.                     }  
  114.                         Console.WriteLine();  
  115.                 }  
  116.                     Console.ReadLine();  
  117.                 break;  
  118.             }  
  119.   
  120.             default:  
  121.             Console.WriteLine("Enter valid Word");  
  122.             break;  
  123.         }  
  124.               
  125.     }  
  126.   
  127.     public void DiaSum()  
  128.     {  
  129.   
  130.         int res=0;  
  131.         Console.WriteLine("Sum of resultant diagonal is : ");  
  132.         for(row=0; row<m; row++)  
  133.         {  
  134.             for(col=0; col<n; col++)  
  135.             {  
  136.                 if(row==col)  
  137.                 res += matC[row,col];  
  138.             }  
  139.         }  
  140.   
  141.         Console.WriteLine("{0}", res);  
  142.     }  
  143.   
  144.     public static void Main()  
  145.     {  
  146.         int i,j;  
  147.   
  148.         Console.Write("Enter the no. of row: ");  
  149.         i=Convert.ToInt32(Console.ReadLine());  
  150.   
  151.         Console.Write("Enter the no. of col: ");  
  152.         j=Convert.ToInt32(Console.ReadLine());  
  153.   
  154.         AddSub AS = new AddSub(i,j);  
  155.   
  156.         AS.readMat();  
  157.         AS.printMat();  
  158.         AS.Operation();  
  159.         AS.DiaSum();  
  160.   
  161.         Console.ReadLine();  
  162.     }  
  163. }
** Complete Code

Description:
First of all we declare some variable m, n, row, col of type int and three matrices matA, matB, matC. After declaring variables and create constructor we create four methods readMat(), printMat(), Operation(), DiaSum(). readMat() method are use to entering the elements on matrix A and matrix B according to their order declare in main() method. Method printMat() use to print the matrix A and matrix B on the console after that Operation method is call for doing operation of addition and subtraction.

1. readMat()

  1. public void readMat()  
  2.     {  
  3.         Console.WriteLine("Enter the Element of matrix A: ");  
  4.         for(row=0; row<m; row++)  
  5.         {  
  6.             for(col=0; col<n; col++)  
  7.             {  
  8.                 Console.Write("matA[{0},{1}] : ",row,col);  
  9.                 matA[row,col]=Convert.ToInt32(Console.ReadLine());  
  10.             }  
  11.   
  12.                 Console.WriteLine();  
  13.         }  
  14.   
  15.         Console.ReadLine();  
  16.   
  17.         Console.WriteLine("Enter the Element of matrix B: ");  
  18.         for(row=0; row<m; row++)  
  19.         {  
  20.             for(col=0; col<n; col++)  
  21.             {  
  22.                 Console.Write("matB[{0},{1}] : ",row,col);  
  23.                 matB[row,col]=Convert.ToInt32(Console.ReadLine());  
  24.             }  
  25.   
  26.                 Console.WriteLine();  
  27.         }  
  28.   
  29.         Console.ReadLine();  
  30.     }  


2. printMat()


  1.   public void printMat()  
  2.     {  
  3.         Console.WriteLine("Matrix A");  
  4.         for(row=0; row<m; row++)  
  5.         {  
  6.             for(col=0; col<m; col++)  
  7.                 Console.Write("{0} ",matA[row,col]);  
  8.                 Console.WriteLine();  
  9.         }  
  10.   
  11.         Console.WriteLine("Matrix B");  
  12.         for(row=0; row<m; row++)  
  13.         {  
  14.             for(col=0; col<m; col++)  
  15.                 Console.Write("{0} ",matB[row,col]);  
  16.                 Console.WriteLine();  
  17.         }  
  18.     }  

3.Operation()

  1.  public void Operation()  
  2.     {  
  3.         Console.Write("Type \"Add\" for Addition and \"Sub\" for Substraction : ");  
  4.         string response = Console.ReadLine();  
  5.                response = response.ToUpper();  
  6.                  
  7.   
  8.         switch(response)  
  9.         {  
  10.             case "ADD":  
  11.             {  
  12.                 for(row=0; row<m; row++)  
  13.                 {  
  14.                     for(col=0; col<n; col++)  
  15.                     {  
  16.                         matC[row,col] += matA[row,col]+matB[row,col];  
  17.                     }  
  18.                 }  
  19.   
  20.                 Console.WriteLine("Resultant is: ");  
  21.                 for(row=0; row<m; row++)  
  22.                 {  
  23.                     for(col=0; col<n; col++)  
  24.                     {  
  25.                         Console.Write("{0:D2} ", matC[row,col]);  
  26.                     }  
  27.                         Console.WriteLine();  
  28.                 }  
  29.                     Console.ReadLine();  
  30.                 break;  
  31.             }  
  32.   
  33.             case "SUB":  
  34.             {  
  35.                 for(row=0; row<m; row++)  
  36.                 {  
  37.                     for(col=0; col<n; col++)  
  38.                     {  
  39.                         matC[row,col] -= matA[row,col]-matB[row,col];  
  40.                     }  
  41.                 }  
  42.   
  43.                 Console.WriteLine("Resultant is: ");  
  44.                 for(row=0; row<m; row++)  
  45.                 {  
  46.                     for(col=0; col<n; col++)  
  47.                     {  
  48.                         Console.Write("{0:D2} ", matC[row,col]);  
  49.                     }  
  50.                         Console.WriteLine();  
  51.                 }  
  52.                     Console.ReadLine();  
  53.                 break;  
  54.             }  
  55.   
  56.             default:  
  57.             Console.WriteLine("Enter valid Word");  
  58.             break;  
  59.         }  
  60.               
  61.     }  

4. Diasum() : This method is use for print the sum of diagonal of resultant matrix C


  1. public void DiaSum()  
  2.     {  
  3.   
  4.         int res=0;  
  5.         Console.WriteLine("Sum of resultant diagonal is : ");  
  6.         for(row=0; row<m; row++)  
  7.         {  
  8.             for(col=0; col<n; col++)  
  9.             {  
  10.                 if(row==col)  
  11.                 res += matC[row,col];  
  12.             }  
  13.         }  
  14.   
  15.         Console.WriteLine("{0}", res);  
  16.     } 

Screenshot of output:

screenshot of output



Read also:-