Users Online

· Guests Online: 124

· Members Online: 0

· Total Members: 188
· Newest Member: meenachowdary055

Forum Threads

Newest Threads
No Threads created
Hottest Threads
No Threads created

Latest Articles

C# Program to Find the Smallest Element in a Matrix

C# Program to Find the Smallest Element in a Matrix

 

 

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

Problem Description

This C# Program Finds Smallest Element in a Matrix.

Problem Solution

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

Program/Source Code

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.

/*
 * C# Program to Find Smallest Element in a Matrix
 */
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();
        }
 }
 Program Explanation

This C# Program, we are creating the object variable ‘obj’ for arrsampl class. The printarray() function 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. For loop is used to check each element values in the array. If else condition statement is used to check the value of ‘small’ variable is greater than the value of ‘x[i,j]’ variable.

If the condition is true, then execute the statement and assign the value of ‘x[i,j]’ variable to small variable. Print the smallest element in a matrix.

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

 

Comments

No Comments have been Posted.

Post Comment

Please Login to Post a Comment.

Ratings

Rating is available to Members only.

Please login or register to vote.

No Ratings have been Posted.
Render time: 1.11 seconds
10,853,935 unique visits