Sunday 22 March 2015

Zero Matrix or null matrix

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

A matrix is said to be zero matrix if its all elements are zero.

Image of Zero Matrix



Code Snippet:

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

Description:

In the above code first of all we ask user to take input of row and columns and after that user enter the element after entering elements we print the matrix and after the matrix print we go for the logic behind to find put whether the matrix is zero matrix or not. 

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

In the above code we check if any of the element of the matrix is not equal to zero than the IsZero which is a bool variable declare at Line 7 of the original code is changed to false. 
After the logical operation we print the final output whether the matrix is zero or not.

  1. if(IsZero == true)  
  2.         {  
  3.             Console.WriteLine("Zero Matrix");  
  4.         }  
  5.         else  
  6.         {  
  7.             Console.WriteLine("Not Zero Matrix");  
  8.         } 


Screenshot of the output:

output of zero matrix