In this example, i’ll show you How to find largest element in a matrix.
Here is source code of the C# Program to Find Largest Element in a Matrix. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.
C# 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 54 55 56 57 58 59 60 61 62 63 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace generate_matrix { class Program { static int max(int[,] x) { int big = x[0, 0]; for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { if (big < x[i, j]) { big = x[i, j]; } } } return big; } static void Main(string[] args) { Random rnd = new Random(); const int MATRIX_ROWS = 5; const int MATRIX_COLUMNS = 5; int[,] matrix = new int[MATRIX_ROWS, MATRIX_COLUMNS]; for (int i = 0; i < MATRIX_ROWS; i++) { for (int j = 0; j < MATRIX_COLUMNS; j++) { matrix[i, j] = rnd.Next(1,100); } } for (int i = 0; i < MATRIX_ROWS; i++) { for (int j = 0; j < MATRIX_COLUMNS; j++) { Console.Write(matrix[i,j] + "\t"); } Console.WriteLine(); } Console.WriteLine(); int big = max(matrix); Console.Write("Largest Element : " + big); Console.ReadKey(); } } } |
Output:
Great!
How do you double all values of the matrix and print them to the screen in matrix format? I can double them, but not in matrix form though..