C# Program to Find the Largest Element in a Matrix

This is a C# Program to find largest element in a matrix.

Problem Description

This C# Program Finds Largest Element in a Matrix.

Problem Solution

Here the largest element in the matrix is found and displayed.

Program/Source Code

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# Program to Find Largest Element in a Matrix
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
    class arrsampl
    {
        int[,]x;
        arrsampl()
        {
            x = new int[,] { { 12, 21, 63 }, { 40, 15, 6 } };
        }
        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 big = x[0, 0];
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    if (big < x[i, j])
                    {
                        big = x[i, j];
                    }
                }
            }
            return big;
        }
        public static void Main()
        {
            arrsampl obj = new arrsampl();
            obj.printarray();
            Console.WriteLine("Largest Element : {0}", obj.max());
            Console.ReadLine();
        }
    }
Program Explanation

In this C# program, we are creating the object variable ‘obj’ for ‘arrsampl’ class. The printarray() method is used to enter the coefficient element values of the array using a[i,j] variable. The max() method is used to find the smallest element in a matrix.

advertisement
advertisement

For loop is used to check each element values in the array by using if else condition statement. Check the value of ‘big’ variable is less than the value of x[i,j] variable. If the condition is true, then iterate the statement and assign the value of the x[i,j] variable to big variable. Print the largest element in a matrix.

Runtime Test Cases
 
Elements in the Given Matrix :
12 21 63
40 15 6
Largest Element : 63

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.