Home > OS >  Output an array.Find the largest element of the array. C#
Output an array.Find the largest element of the array. C#

Time:11-21

A 2D array of integers B(M, N) is set using a random number sensor. Display the array. Find the largest element of the array. Print its indexes.

I do not know what to do with m, n and how to find max

namespace Program

{
    class Program
    {
        static void Main(string[] args)
        {
        int m,n; Random rnd = new Random();
        m = 4; n = 4; int max;
        int[,] m1 = new int[m,n];

        for(int i=0;i<m;i  )
        {
            for(int j =0;j<n;j  ){
                m1[i,j] = rnd.Next(100);
                Console.Write(m1[i,j]   "\t");
            }
        }

CodePudding user response:

I shouldn't have done this, but I figured it may be an actual question of someone who actually tried.

namespace Program

{
    class Program
    {
        static void Main(string[] args)
        {
            Random rnd = new Random();
            
            var m = 4;
            var n = 4; 
            
            int max = 0;  // you don't need to say zero but it makes it more clear
            
            int[,] m1 = new int[m, n];

            for (int i = 0; i < m; i  )
            {
                for (int j = 0; j < n; j  )
                {
                    m1[i, j] = rnd.Next(100);
                    Console.Write(m1[i, j]   "\t");
                    
                    if (m1[i, j] > max)
                        max = m1[i, j];
                }
            }
        }
    }

CodePudding user response:

    int m,n; Random rnd = new Random();
    m = rnd.Next(10); n = rnd.Next(10); 
    int[,] m1 = new int[m,n];

    for(int i=0;i<m;i  )
    {
        for(int j =0;j<n;j  ){
            m1[i,j] = rnd.Next(100);
            Console.Write(m1[i,j]   "\t");
        }
    }
    int maks = m1[0,0];
    for (int i = 1; i < m; i  )
        {
            for (int j = 0; j < n; j  )
            {
                if (m1[i, j] > maks)
                    maks = m1[i, j];
            }
        }
    Console.WriteLine(maks);
  •  Tags:  
  • c#
  • Related