Monday 23 March 2015

Row, Column and Square Matrix in C#

Question: Program to check whether the matrix is row matrix or columns matrix or the square matrix.

Row Matrix: A matrix is said to be row matrix if it has only one row. A matrix of order 1 x n is the row matrix.

For example:
Row matrix Example

Column Matrix : A matrix is said to be column matrix if it has only one column. A matrix of order m x 1  is the column matrix. 

For example:

Column Matrix

Square Matrix: A matrix in which the no. of row is equals to the no. of column is said to be a Square matrix. Thus an m x n matrix is said to be square matrix if m = n and is known as square matrix of order n.

For example: 
Image of Square matrix
Code Snippet: 

  1. using System;  
  2. public class MatType  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int row, col;  
  7.         int[,] matA = new int[10,10];  
  8.         Console.Write("Enter the Row: ");  
  9.         row = Convert.ToInt32(Console.ReadLine());  
  10.   
  11.         Console.Write("Enter the Col: ");  
  12.         col = Convert.ToInt32(Console.ReadLine());  
  13.   
  14.         if(row == 1)  
  15.         {  
  16.             Console.WriteLine("It is a Row matrix.");  
  17.         }  
  18.         else if(col == 1)  
  19.         {  
  20.             Console.WriteLine("It is a Column matrix.");  
  21.         }  
  22.         else if(row == col)  
  23.         {  
  24.             Console.WriteLine("It is a Square matrix.");  
  25.         }  
  26.         else  
  27.         {  
  28.             Console.WriteLine("The given matrix is the simple matrix.");  
  29.         }  
  30.         Console.WriteLine("The matrix is: ");  
  31.   
  32.         for(int i = 1; i<=row; i++)  
  33.         {  
  34.             for(int j= 1 ; j<=col; j++)  
  35.             {  
  36.                 Console.Write("Matrix A[{0},{1}]", i, j);  
  37.                 matA[i,j] = Convert.ToInt32(Console.ReadLine());  
  38.             }  
  39.             Console.WriteLine();  
  40.         }  
  41.   
  42.         for(int i = 1; i<=row; i++)  
  43.         {  
  44.             for(int j= 1 ; j<=col; j++)  
  45.             {  
  46.                 Console.Write("{0} ", matA[i,j]);  
  47.             }  
  48.             Console.WriteLine();  
  49.         }  
  50.   
  51.         Console.ReadLine();  
  52.     }  
  53. }  
** Complete code

Description:

In the above code we simply check the condition for row matrix, column matrix and square matrix from line 14 to 29.

Screenshot of Output:

Output of screenshot


Read also:
Zero matrix in C#
Diagonal Matrix in C#.
Pattern programming in C#