Sunday 5 April 2015

Diagonal Exchange in Matrix

Question: Program to exchange the diagonal of a matrix.

image of diagonal exchange matrix
Diagonal Exchange of  matrix

In the diagonal exchange matrix we exchange the two diagonal of a single matrix. To do this first of all we check the no. of row and column of the matrix if the no. of row and column of the matrix is not equal then the diagonal exchange is not possible b'coz in the diagonal exchange matrix it is compulsory that the no. of row and column is equal, only square matrix are perform the diagonal exchange operation. So, after check whether the order of the matrix is equal or not we enter the element in the matrix after that diagonal exchange matrix operations are done.

Code Snippet:

  1. using System;  
  2. public class DiagExc  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int[,] matA = new int[10,10];  
  7.         int i, j, row, col, temp;  
  8.   
  9.         Console.Write("Enter the no. of row: ");  
  10.         i = Convert.ToInt32(Console.ReadLine());  
  11.   
  12.         Console.Write("Enter the no. of col: ");  
  13.         j = Convert.ToInt32(Console.ReadLine());  
  14.   
  15.         if(i != j)  
  16.         {  
  17.             Console.WriteLine("Diagonal exchange is not possible.");  
  18.         }  
  19.           
  20.         else  
  21.         {  
  22.   
  23.             Console.WriteLine("Enter the element of the matrix: ");  
  24.             for(row=0; row<i; row++)  
  25.             {  
  26.                 for(col=0; col<j; col++)  
  27.                 {  
  28.                     Console.Write("Matrix A [{0},{1}] : ", row, col);  
  29.                     matA[row,col]=Convert.ToInt32(Console.ReadLine());  
  30.                 }  
  31.   
  32.                 Console.WriteLine();  
  33.             }  
  34.             Console.ReadLine();  
  35.   
  36.             Console.WriteLine("Matrix A:");  
  37.             for(row=0; row<i; row++)  
  38.             {  
  39.                 for(col=0; col<j; col++)  
  40.                 Console.Write("{0} ", matA[row,col]);  
  41.   
  42.                 Console.WriteLine();  
  43.             }  
  44.   
  45.             Console.ReadLine();  
  46.   
  47.             for(row=0; row<i; row++)  
  48.             {  
  49.                 temp = matA[row,row];  
  50.                 matA[row,row]=matA[row,i-row-1];  
  51.                 matA[row,i-row-1] = temp;  
  52.             }  
  53.   
  54.             Console.WriteLine("Matrix after exchanging the diagonals: ");  
  55.   
  56.             for(row=0; row<i; row++)  
  57.             {  
  58.                 for(col=0; col<j; col++)  
  59.   
  60.                     Console.Write("{0} ",matA[row,col]);  
  61.   
  62.                     Console.WriteLine();  
  63.             }  
  64.   
  65.         }  
  66.   
  67.         Console.ReadLine();  
  68.   
  69.           
  70.     }  
** Complete code

Diagonal exchange operation:

  1. for(row=0; row<i; row++)  
  2.             {  
  3.                 temp = matA[row,row];  
  4.                 matA[row,row]=matA[row,i-row-1];  
  5.                 matA[row,i-row-1] = temp;  
  6.             }  



Screenshot: 

image of Screenshot of diagonal exchange matrix
Output of diagonal exchange matrix
Read Also: