Friday 1 December 2017

C# Program to Find Smallest Element in a Matrix


Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class arrsampl
{
        int[,]x;
        arrsampl()
        {
            x = new int[,] { { 11, 2, 61 }, { 42, 50, 3 } };
        }
        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()
        {
            arrsampl obj = new arrsampl();
            obj.printarray();
            Console.WriteLine("Smallest Element : {0}", obj.max());
            Console.ReadLine();
        }
 }

Output:

Elements in the Given Matrix :
11  2  61
42  50  3
Smallest Element : 2



More C# Programs:



















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...