1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace FindMaxAndMinMatrix2D
7{
8 class Program
9 {
10 static void Main(string[] args)
11 {
12 const int x = 3, y = 5;
13 int min, max;
14 int[,] arr = new int[x, y] {
15 { 10, 50, 13, 80, 40 },
16 { 1, 250, 65, 28, 15 },
17 { 12, 17, 45, 20, 6 }
18 };
19
20 //Declare two variables max and min to store maximum and minimum.
21 //Assume first array element as maximum and minimum both, say max = arr[0,0] and min = arr[0,0]
22 min = arr[0, 0];
23 max = arr[0, 0];
24
25 // Iterate through array to find maximum and minimum element in array.
26 //Inside loop for each array element check for maximum and minimum.
27 for (int i = 0; i < x; i++)
28 {
29 for (int j = 0; j < y; j++)
30 {
31 //Assign current array element to max, if (arr[i,j] > max)
32 if (arr[i, j] > max)
33 {
34 max = arr[i, j];
35 }
36
37 //Assign current array element to min if if (arr[i,j] < min)
38 if (arr[i, j] < min)
39 {
40 min = arr[i, j];
41 }
42 }
43 }
44
45 //Print Array Elements
46 Console.Write("Array Elements\n");
47 for (int i = 0; i < x; i++)
48 {
49 for (int j = 0; j < y; j++)
50 {
51 Console.Write(arr[i, j] + ", ");
52 }
53 Console.WriteLine();
54 }
55 Console.WriteLine();
56
57 //Print max and min number
58 Console.WriteLine("Maximum element:" + max);
59 Console.WriteLine("Minimum element:" + min);
60 Console.ReadLine();
61 }
62 }
63}