Saturday 21 March 2015

Matrices

Definition:

 A matrix is an ordered rectangular array of numbers, symbols, expressions and functions.The numbers or functions are called elements or the entries of matrix.
It's also the example of nested loop in programming.

usually matrices are denoted by capital letters. The following are some examples of matrix :

Matrix In C#

Example of Matrix



Question: Program to Show the 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.

Code Snippet: 

  1. using System;  
  2. public class MatAdd  
  3. {  
  4.     public static void Main(string[] args)  
  5.     {  
  6.         int row, col, i, j;  
  7.   
  8.         Console.Write("Enter the no. of row for matrixA: ");  
  9.         row=Convert.ToInt32(Console.ReadLine());  
  10.   
  11.         Console.Write("Enter the no. of col for matrixA: ");  
  12.         col = Convert.ToInt32(Console.ReadLine());  
  13.   
  14.         int[,] matA = new int[10,10];  
  15.   
  16.         Console.WriteLine("\nEnter the element in matrixA: ");  
  17.         for(i = 0; i<row; i++)  
  18.         {  
  19.             for(j=0; j<col; j++)  
  20.             {  
  21.                 Console.Write("Matrix A [{0},{1}]: "i, j);  
  22.                 matA[i,j] = Convert.ToInt32(Console.ReadLine());  
  23.             }  
  24.   
  25.                 Console.WriteLine();  
  26.         }  
  27.   
  28.         int[,] matB = new int[10,10];  
  29.   
  30.         Console.WriteLine("\nEnter the element in matrixB: ");  
  31.         for(i = 0; i<row; i++)  
  32.         {  
  33.             for(j=0; j<col; j++)  
  34.             {  
  35.                 Console.Write("Matrix B [{0},{1}]: "i, j);  
  36.                 matB[i,j] = Convert.ToInt32(Console.ReadLine());  
  37.             }  
  38.   
  39.                 Console.WriteLine();  
  40.         }  
  41.   
  42.         Console.WriteLine("Matrix A :");  
  43.         for(i = 0; i<row; i++)  
  44.         {  
  45.             for(j=0; j<col; j++)  
  46.                 Console.Write(" {0}", matA[i,j]);  
  47.   
  48.                 Console.WriteLine();  
  49.         }  
  50.   
  51.         Console.WriteLine("Matrix B :");  
  52.         for(i = 0; i<row; i++)  
  53.         {  
  54.             for(j=0; j<col; j++)  
  55.                 Console.Write(" {0}", matB[i,j]);  
  56.   
  57.                 Console.WriteLine();  
  58.         }  
  59.           
  60.         int[,] matC = new int[10,10];  
  61.         for(i = 0; i<row; i++)  
  62.         {  
  63.             for(j=0; j<col; j++)  
  64.                 matC[i,j] = matA[i,j] + matB[i,j];  
  65.   
  66.         }  
  67.   
  68.         Console.WriteLine("Addition of matrix is: ");  
  69.   
  70.         for(i = 0; i<row; i++)  
  71.         {  
  72.             for(j=0; j<col; j++)  
  73.                 Console.Write(" {0:D2}", matC[i,j]);  
  74.   
  75.                 Console.WriteLine();  
  76.         }  
  77.   
  78.         Console.ReadLine();  
  79.           
  80.     }  
  81. }

Description:

In the above code, There are two input of matrix, Matrix A and Matrix B, when we Enter the element of the two matrices Matrix C is formed by the resultant sum of element of the two matrices.

Screenshot of Output:

Output of matrix addition
 ** Screenshot of Addition of matrix


Question: Program to show the 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 MatSub  
  3. {  
  4.     public static void Main(string[] args)  
  5.     {  
  6.         int row, col, i, j;  
  7.   
  8.         Console.Write("Enter the no. of row for matrixA: ");  
  9.         row=Convert.ToInt32(Console.ReadLine());  
  10.   
  11.         Console.Write("Enter the no. of col for matrixA: ");  
  12.         col = Convert.ToInt32(Console.ReadLine());  
  13.   
  14.         int[,] matA = new int[10,10];  
  15.   
  16.         Console.WriteLine("\nEnter the element in matrixA: ");  
  17.         for(i = 0; i<row; i++)  
  18.         {  
  19.             for(j=0; j<col; j++)  
  20.                 {  
  21.                     Console.Write("Matrix A[{0},{1}]: ", i, j);  
  22.                     matA[i,j] = Convert.ToInt32(Console.ReadLine());  
  23.                 }  
  24.   
  25.                 Console.WriteLine();  
  26.         }  
  27.   
  28.         int[,] matB = new int[10,10];  
  29.   
  30.         Console.WriteLine("\nEnter the element in matrixB: ");  
  31.         for(i = 0; i<row; i++)  
  32.         {  
  33.             for(j=0; j<col; j++)  
  34.                 {  
  35.                     Console.Write("Matrix B[{0},{1}]: ", i, j);  
  36.                     matB[i,j] = Convert.ToInt32(Console.ReadLine());  
  37.                 }  
  38.   
  39.                 Console.WriteLine();  
  40.         }  
  41.   
  42.         Console.WriteLine("Matrix A :");  
  43.         for(i = 0; i<row; i++)  
  44.         {  
  45.             for(j=0; j<col; j++)  
  46.                 Console.Write(" {0}", matA[i,j]);  
  47.   
  48.                 Console.WriteLine();  
  49.         }  
  50.   
  51.         Console.WriteLine("Matrix B :");  
  52.         for(i = 0; i<row; i++)  
  53.         {  
  54.             for(j=0; j<col; j++)  
  55.                 Console.Write(" {0}", matB[i,j]);  
  56.   
  57.                 Console.WriteLine();  
  58.         }  
  59.           
  60.         int[,] matC = new int[10,10];  
  61.         for(i = 0; i<row; i++)  
  62.         {  
  63.             for(j=0; j<col; j++)  
  64.                 matC[i,j] = matA[i,j] - matB[i,j];  
  65.   
  66.         }  
  67.   
  68.         Console.WriteLine("Subtraction of matrix is: ");  
  69.   
  70.         for(i = 0; i<row; i++)  
  71.         {  
  72.             for(j=0; j<col; j++)  
  73.                 Console.Write(" {0:D2}", matC[i,j]);  
  74.   
  75.                 Console.WriteLine();  
  76.         }  
  77.   
  78.         Console.ReadLine();  
  79.           
  80.     }  
  81. }  


Description:

In the above code, There are two input of matrix, Matrix A and Matrix B, when we Enter the element of the two matrices Matrix C is formed by the resultant subtraction of element of the two matrices.

Screenshot of Output:
Output of matrix subtraction
**Output of Matrix Subtraction


**Note: Program are working on cmd. prompt also

Must See:
How and Why Run C# in cmd