Monday 23 March 2015

Scalar Matrix in C#

Question: Program to check whether the given matrix is scalar or not.

A Diagonal matrix is said to be scalar matrix if its diagonal elements are equals that is, A square matrix is said to be scalar if all non diagonal elements is equals to zero and all diagonal elements is equals to k, where k is some constant value.

For example:
Scalar Matrix in C#
The above three matrix are scalar matrix of order 1, 2, 3 respectively.

Code Snippet:

  1. using System;  
  2. public class Scalar  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int row, col, i, j;  
  7.         int[,] matA = new int[10,10];  
  8.         bool IsScalar = true;  
  9.   
  10.         Console.Write("\nEnter the row: ");  
  11.         row = Convert.ToInt32(Console.ReadLine());  
  12.         Console.Write("Enter the col: ");  
  13.         col = Convert.ToInt32(Console.ReadLine());  
  14.   
  15.         Console.WriteLine("\nEnter the element of Matrix: ");  
  16.         for(i=1; i<=row; i++)  
  17.         {  
  18.             for(j = 1; j<=col; j++)  
  19.             {  
  20.                 Console.Write("Matrix A[{0},{1}]: ", i, j);  
  21.                 matA[i,j] = Convert.ToInt32(Console.ReadLine());  
  22.             }  
  23.             Console.WriteLine();  
  24.         }  
  25.             Console.ReadLine();  
  26.   
  27.         Console.WriteLine("Matrix A: ");  
  28.         for(i=1; i<=row; i++)  
  29.         {  
  30.             for(j = 1; j<=col; j++)  
  31.             {  
  32.                 Console.Write("{0} ", matA[i,j]);  
  33.             }  
  34.             Console.WriteLine();  
  35.         }  
  36.             Console.ReadLine();  
  37.   
  38.         for(i=1; i<=row; i++)  
  39.         {  
  40.             for(j = 1; j<=col; j++)  
  41.             {  
  42.                 if( ( (i == j) && (matA[i,j] != matA[1,1]) ) || ( (i != j) && (matA[i,j]!= 0) ) )  
  43.                 {  
  44.                     IsScalar = false;  
  45.                     break;  
  46.                 }  
  47.             }  
  48.         }  
  49.   
  50.           
  51.         if(IsScalar==true)  
  52.         {  
  53.             Console.WriteLine("The given matrix is Scalar matrix");  
  54.         }  
  55.         else  
  56.         {  
  57.             Console.WriteLine("The given matrix is not a Scalar matrix");  
  58.         }  
  59.         Console.ReadLine();  
  60.   
  61.     }  
** Complete code

Description:

In the above code first of all we declare some variable row, col, i, j to read and write the matrix we take a 2-dimensional array of matA and a bool variable which is initialize to true.
First of all we enter the no. of row and column in Line 10 to 13 remember the no, of row and column must be equal. After that we enter the element of matrix and print the matrix.

  1. for(i=1; i<=row; i++)  
  2.         {  
  3.             for(j = 1; j<=col; j++)  
  4.             {  
  5.                 if( ( (i == j) && (matA[i,j] != matA[1,1]) ) || ( (i != j) && (matA[i,j]!= 0) ) )  
  6.                 {  
  7.                     IsScalar = false;  
  8.                     break;  
  9.                 }  
  10.             }  
  11.         } 

In the above piece of code we design the logic of scalar number if i and j equal to each other i.e (1,1) (2,2) (3,3) and if it is not equals to first element of the matrix placed at the position of (1,1) or if i and j not equals to each other and if elements of matrix not equals to 0 than ths IsScalar bool value changed to false.

  1. if(IsScalar ==true)  
  2.         {  
  3.             Console.WriteLine("The given matrix is Scalar matrix");  
  4.         }  
  5.         else  
  6.         {  
  7.             Console.WriteLine("The given matrix is not a Scalar matrix");  
  8.         }  

if IsScalar is remain true then the matrix is scalar matrix else "not scalar matrix" statement print on console.

Screenshot of Output:

Output of Scalar matrix


Read Also:-