Monday 6 April 2015

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#