Tuesday 31 March 2015

Interchange operation in Matrix


Row  Interchange:

Question: Program to interchange the row in the matrix.

In this question first of all we declare the order of the matrix and after that we enter the element in the matrix after that we print the matrix on the screen before going to the interchange operation after that user enter the no. of row1 and no. of row2 which S/he want to interchange after that we perform the interchange operation.

Code snippet: 

  1. using System;  
  2. public class IntRow  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int m,n, row, col;  
  7.         int[,] matA = new int[10,10];  
  8.   
  9.         Console.Write("\nEnter the no. of row: ");  
  10.         m=Convert.ToInt16(Console.ReadLine());  
  11.   
  12.         Console.Write("Enter the no. of col: ");  
  13.         n=Convert.ToInt16(Console.ReadLine());  
  14.   
  15.         Console.WriteLine("\nEnter the element in MatrixA: ");  
  16.         for(row=1; row<=m; row++)  
  17.         {  
  18.             for(col=1; col<=n; col++)  
  19.             {  
  20.                 Console.Write("Matrix A[{0},{1}]: ", row, col);  
  21.                 matA[row,col] = Convert.ToInt16(Console.ReadLine());  
  22.             }  
  23.   
  24.             Console.WriteLine();  
  25.         }  
  26.   
  27.         Console.WriteLine("MatrixA: ");  
  28.         for(row=1; row<=m; row++)  
  29.         {  
  30.             for(col=1; col<=n; col++)  
  31.             Console.Write("{0:D2} ",matA[row,col]);  
  32.   
  33.             Console.WriteLine();  
  34.         }  
  35.         Console.ReadLine();  
  36.   
  37.         Console.Write("\nEnter the no. of row you want to interchange: ");  
  38.         int i = Convert.ToInt16(Console.ReadLine());  
  39.       
  40.         Console.Write("Enter the no. of row you want to interchange with: ");  
  41.         int j = Convert.ToInt16(Console.ReadLine());  
  42.   
  43.         for(int k=1; k<=n; k++)  
  44.         {  
  45.             int temp = matA[i,k];  
  46.             matA[i,k] = matA[j,k];  
  47.             matA[j,k] = temp;  
  48.         }  
  49.   
  50.         Console.WriteLine("\nMatrixA after interchange of row:{0} and row {1}: ", i, j);  
  51.         for(row=1; row<=m; row++)  
  52.         {  
  53.             for(col=1; col<=n; col++)  
  54.             Console.Write("{0:D2} ",matA[row,col]);  
  55.   
  56.             Console.WriteLine();  
  57.         }  
  58.   
  59.         Console.ReadLine();  
  60.     }  
  61. }  

Row interchange operation:

  1. for(int k=1; k<=n; k++)  
  2.         {  
  3.             int temp = matA[i,k];  
  4.             matA[i,k] = matA[j,k];  
  5.             matA[j,k] = temp;  
  6.         } 

in the above we take a third variable temp and simply interchange the element of the desired row by the using of temp variable.

Screenshot of the output:

Row Exchange Output

Column Interchange:

Question: Program to interchange column of a matrix.

In this question first of all we declare the order of the matrix and after that we enter the element in the matrix after that we print the matrix on the screen before going to the interchange operation after that user enter the no. of col1 and no. of col2 which S/he want to interchange after that we perform the interchange operation.

Code Snippet:

  1. using System;  
  2. public class IntCol  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int m,n, row, col;  
  7.         int[,] matA = new int[10,10];  
  8.   
  9.         Console.Write("\nEnter the no. of row: ");  
  10.         m=Convert.ToInt16(Console.ReadLine());  
  11.   
  12.         Console.Write("Enter the no. of col: ");  
  13.         n=Convert.ToInt16(Console.ReadLine());  
  14.   
  15.         Console.WriteLine("\nEnter the element in MatrixA: ");  
  16.         for(row=1; row<=m; row++)  
  17.         {  
  18.             for(col=1; col<=n; col++)  
  19.             {  
  20.                 Console.Write("Matrix A[{0},{1}] :", row, col);  
  21.                 matA[row,col] = Convert.ToInt16(Console.ReadLine());  
  22.             }  
  23.   
  24.             Console.WriteLine();  
  25.         }  
  26.   
  27.         Console.WriteLine("MatrixA: ");  
  28.         for(row=1; row<=m; row++)  
  29.         {  
  30.             for(col=1; col<=n; col++)  
  31.             Console.Write("{0:D2} ",matA[row,col]);  
  32.   
  33.             Console.WriteLine();  
  34.         }  
  35.         Console.ReadLine();  
  36.   
  37.         Console.Write("\nEnter the no. of col you want to interchange: ");  
  38.         int i = Convert.ToInt16(Console.ReadLine());  
  39.       
  40.         Console.Write("Enter the no. of col you want to interchange with: ");  
  41.         int j = Convert.ToInt16(Console.ReadLine());  
  42.   
  43.         for(int k=1; k<=n; k++)  
  44.         {  
  45.             int temp = matA[k,i];  
  46.             matA[k,i] = matA[k,j];  
  47.             matA[k,j] = temp;  
  48.         }  
  49.   
  50.         Console.WriteLine("\nMatrixA after interchange of col:{0} and col:{1}: ", i, j);  
  51.         for(row=1; row<=m; row++)  
  52.         {  
  53.             for(col=1; col<=n; col++)  
  54.             Console.Write("{0:D2} ",matA[row,col]);  
  55.   
  56.             Console.WriteLine();  
  57.         }  
  58.   
  59.         Console.ReadLine();  
  60.     }  
  61. }  

Column interchange operation:

  1. for(int k=1; k<=n; k++)  
  2.         {  
  3.             int temp = matA[k,i];  
  4.             matA[k,i] = matA[k,j];  
  5.             matA[k,j] = temp;  
  6.         }  

in the above we take a third variable temp and simply interchange the element of the desired row by the using of temp variable.

Column Exchange Output


Diagonal Exchange:

Question: Program to interchange the diagonal of a matrix.

In this question first of all we check whether the order is same or not if the order is same than exchange operation is occur otherwise not. After that we enter the element in the matrix after that we print the matrix on the screen.

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.     }  
  71. }

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 of Output 1:

Screenshot of output1


Screenshot of Output 2:

Screenshot of output2