0
//This is my code,how do i find & print max value
namespace StartCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
           int [, ] a =  new int [4,4] {{2,3,4,5}, {34,56,25,67}, {22,44,55,77},{45,80,22,13}};

           foreach (int x in a)
               Console.WriteLine(x);



        }    
    }
}
///Thank you.
2
Contributors
3
Replies
24
Views
2 Years
Discussion Span
Last Post by ZER09
0

You can try this

 int[,] a = new int[4, 4] { { 2, 3, 4, 5 }, { 34, 56, 25, 67 }, { 22, 44, 55, 77 }, { 45, 80, 22, 13 } };
 Console.WriteLine(a.Cast<int>().Max());
0
//Thank you. Can you explain why if we use standard array,like this 
  int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

        int maxNum = numbers.Max();

        Console.WriteLine("The maximum number is {0}.", maxNum);


       // No need to use cast,but in case multidimensional array need to cast.
0

Beacause the Max() extension will not work on multidimentional array, so first you need to flatten it.

Then the Cast<T>() can do it for you.

Look at this.

foreach (var item in a.Cast<int>())
{
    Console.WriteLine(item);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.