Tuesday, 7 April 2015

Lower Triangular matrix

Question: Program to print the lower triangular matrix.

To print the Lower Triangle of the matrix first of all take the order of matrix it must be square, so, number of row and column oh the matrix is same, after that enter the element of the matrix after that print the matrix on the console.

Code Snippet:


  1. using System;  
  2. public class LowTrn  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int i, j, row, col;  
  7.         int[,] matA = new int[10,10];  
  8.           
  9.         Console.Write("\nEnter the no. of row: ");  
  10.         i=Convert.ToInt16(Console.ReadLine());  
  11.   
  12.         Console.Write("Enter the no. of col: ");  
  13.         j=Convert.ToInt16(Console.ReadLine());  
  14.   
  15.         Console.WriteLine("\nEnter the Element in Matrix: ");  
  16.         for(row=0; row<i; row++)  
  17.         {  
  18.             for(col = 0; col<j; 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("\nMatrix A: ");  
  28.         for(row=0; row<i; row++)  
  29.         {  
  30.             for(col = 0; col<j; col++)  
  31.             Console.Write("{0:D2} ", matA[row,col]);  
  32.   
  33.             Console.WriteLine();  
  34.         }  
  35.         Console.ReadLine();  
  36.   
  37.         Console.WriteLine("\nLower triangle of Matrix A:");  
  38.         for(row=0;row<i; row++)  
  39.         {  
  40.             for(col=0;col<j; col++)  
  41.             {  
  42.                   
  43.                 matA[row,col] = (row < col)? 0 : matA[row,col];  
  44.   
  45.                 Console.Write("{0} ",matA[row,col]);  
  46.             }  
  47.   
  48.             Console.WriteLine();  
  49.         }  
  50.   
  51.         Console.ReadLine();  
  52.           
  53.     }  
** Complete code

Logic:

  1. for(row=0;row<i; row++)  
  2.         {  
  3.             for(col=0;col<j; col++)  
  4.             {  
  5.                   
  6.                 matA[row,col] = (row < col)? 0 : matA[row,col];  
  7.   
  8.                 Console.Write("{0} ",matA[row,col]);  
  9.             }  
  10.   
  11.             Console.WriteLine(); 

Description:

In the above, if the number of column is greater than the row than 0 will be print other wise element of the matrix remain same as it is.


Screenshot:

image of Lower triangle of matrix
Lower triangle of matrix

Read Also:






Monday, 6 April 2015

Upper triangular matrix

Question: Program to print the upper triangular matrix.

To print the upper triangle matrix first of all take the order of matrix it must be square, so, number of row and column oh the matrix is same, after that enter the element of the matrix after that print the matrix on the console.

Code Snippet:

  1. using System;  
  2. public class UprTrn  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int i, j, row, col;  
  7.         int[,] matA = new int[10,10];  
  8.           
  9.         Console.Write("\nEnter the no. of row: ");  
  10.         i=Convert.ToInt16(Console.ReadLine());  
  11.   
  12.         Console.Write("Enter the no. of col: ");  
  13.         j=Convert.ToInt16(Console.ReadLine());  
  14.   
  15.         Console.WriteLine("\nEnter the Element in Matrix: ");  
  16.         for(row=0; row<i; row++)  
  17.         {  
  18.             for(col = 0; col<j; col++)  
  19.             matA[row,col]= Convert.ToInt16(Console.ReadLine());  
  20.   
  21.             Console.WriteLine();  
  22.         }  
  23.   
  24.         Console.WriteLine("\nMatrix A: ");  
  25.         for(row=0; row<i; row++)  
  26.         {  
  27.             for(col = 0; col<j; col++)  
  28.             Console.Write("{0:D2} ", matA[row,col]);  
  29.   
  30.             Console.WriteLine();  
  31.         }  
  32.   
  33.         Console.WriteLine("\nUpper triangle of Matrix A:");  
  34.         for(row=0;row<i; row++)  
  35.         {  
  36.             for(col=0;col<j; col++)  
  37.             {  
  38.                   
  39.                 matA[row,col] = (row<=col)? matA[row,col] : 0;  
  40.   
  41.                 Console.Write("{0} ",matA[row,col]);  
  42.             }  
  43.   
  44.             Console.WriteLine();  
  45.         }  
  46.   
  47.         Console.ReadLine();  
  48.           
  49.     }  
  50. } 
** Complete code

Logic:

To print the upper triangular matrix we check the no. of current row and column is the no. of current row is greater that the no. of column than 0 will be printed other wise matrix remain as it is.

  1. for(row=0;row<i; row++)  
  2.         {  
  3.             for(col=0;col<j; col++)  
  4.             {  
  5.                   
  6.                 matA[row,col] = (row<=col)? matA[row,col] : 0; 
  7.   
  8.                 Console.Write("{0} ",matA[row,col]);  
  9.             }  
  10.   
  11.             Console.WriteLine();  
  12.         }  


Screenshot:

Image of upper triangular matrix
Upper triangular matrix


Largest element in Matrix

Question: Program to find the largest element in the matrix.
To find the largest element of the matrix we simply take a variable temp and store the remporary value of matrix in it anf aftert every comparison we change them accourding to given condition.

First of all take a matrix, declare the order of matrix and after that enter the element in the matrix after that do the operation to find out the largest element in the matrix

Code Snippet:
  1. using System;  
  2. public class LarEle  
  3. {  
  4.     public static void Main()  
  5.     {  
  6.         int[,] matA = new int[10,10];  
  7.         int i, j, row, col;  
  8.   
  9.         Console.Write("\nEnter the no. of row: ");  
  10.         i=Convert.ToInt16(Console.ReadLine());  
  11.   
  12.           
  13.         Console.Write("Enter the no. of col: ");  
  14.         j=Convert.ToInt16(Console.ReadLine());  
  15.   
  16.         Console.WriteLine("\nEnter the element in Matrix: ");  
  17.         for(row=0; row<i; row++)  
  18.         {  
  19.             for(col=0; col<j; col++)  
  20.             matA[row,col]=Convert.ToInt32(Console.ReadLine());  
  21.               
  22.             Console.WriteLine();  
  23.         }  
  24.   
  25.         Console.WriteLine("\nMatrix A: ");  
  26.         for(row=0; row<i; row++)  
  27.         {  
  28.             for(col=0; col<j; col++)  
  29.             Console.Write(" {0}", matA[row,col]);  
  30.               
  31.             Console.WriteLine();  
  32.         }  
  33.   
  34.         int big = matA[0,0];  
  35.         for(row=0; row<i; row++)  
  36.         {  
  37.             for(col=0; col<j; col++)  
  38.                 if(big < matA[row,col])  
  39.                     big=matA[row,col];  
  40.         }  
  41.   
  42.         Console.Write("\nLargest element in matrix A is: {0}", big);  
  43.   
  44.         Console.ReadLine();  
  45.   
  46.           
  47.     }  
  48. }  

Screenshot of output:


Output of Program

Read Also:
Diagonal Exchange in C#