C #-digit group and diagonal matrix array

Two-dimensional array:

        public static void Main(string[] args)
        {

            int[,] a = new int[3, 3];
            Random rom = new Random();
            for (int x = 0; x < a.GetLength(0); x++)
            {
                for (int y = 0; y < a.GetLength(1); y++)
                {
                    a[x, y] = rom.Next(31);
                    Console.Write("{0} ", a[x, y]);
                }
                Console.WriteLine("");
            }
        }

 // Matrix diagonal and

  public static void Main(string[] args)
        {

            int[,] a= new int[3, 3];

            Random rnd = new Random();
            int a1 = 0;
            int a2 = 0;
            for (int x = 0; x < a.GetLength(0); x++)
            {
                for (int y = 0; y < a.GetLength(1); y++)
                {
                    a[x, y] = rnd.Next(21);
                    Console.Write("{0}  ", a[x, y]);
                    if (x == y)
                    {
                        a1 += a[x, y]; //统计正对角线元素之和
                    }
                    if (x + y == a.GetLength(0) - 1)
                    {
                        a2 + = a [x, y ]; // sum of diagonal elements of the sub Statistics 
                    } 
                } 
                Console.WriteLine ( ""); 
            } 
            Console.WriteLine ( "n diagonal elements of the sum of: {0}", a1 ); 
            (Console.WriteLine "and the sub-diagonal elements of: {0}", A2); 
        }

 

 

 

   

Guess you like

Origin www.cnblogs.com/yijieyufu/p/11902017.html