In this example,we’ll learn How to find smallest element in a matrix.
Here is source code of the C# Program to Find Smallest Element in a Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
Source Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace cce { class Program { int[,] x; Program() { x = new int[,] { { 40, 12, 41 }, { 42, 70, 33 } }; } void printarray() { Console.WriteLine("Elements in the Given Matrix : "); for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { Console.Write(x[i, j] + "\t"); } Console.WriteLine("\n"); } } int max() { int small = x[0, 0]; for (int i = 0; i < 2; i++) { for (int j = 0; j < 3; j++) { if (small > x[i, j]) { small = x[i, j]; } } } return small; } public static void Main() { Program obj = new Program(); obj.printarray(); Console.WriteLine("Smallest Element : {0}", obj.max()); Console.ReadLine(); } } } |
Output: