using System;
using System.Text.RegularExpressions;
class HelloWorld
{
    static void PrintMatrix(int[,] matrix)
    {
        for(int y = 0; y<matrix.GetLength(0); y++)
        {
            Console.Write(" +");
            for (int x = 0; x<matrix.GetLength(1); x++){Console.Write("-------+");}
            Console.WriteLine();
            for (int x = 0; x<matrix.GetLength(1); x++)
            {
                Console.Write(" | " + matrix[y,x] + "\t");
            }
            Console.WriteLine(" |");
            
        }
        Console.Write(" +");
        for (int x = 0; x<matrix.GetLength(1); x++){Console.Write("-------+");}
        Console.WriteLine();
    }
    static int FindMax(int[,] matrix)
    {
        int max = matrix[0,0];
        for(int y = 0; y<matrix.GetLength(0); y++)
        {
            for (int x = 0; x<matrix.GetLength(1); x++)
            {
                if(matrix[y,x] > max)
                {
                    max = matrix[y,x];
                }
            }
        }
        return max;
    }
    static int FindMin(int[,] matrix)
    {
        int min = matrix[0,0];
        for(int y = 0; y<matrix.GetLength(0); y++)
        {
            for (int x = 0; x<matrix.GetLength(1); x++)
            {
                if(matrix[y,x] < min)
                {
                    min = matrix[y,x];
                }
            }
        }
        return min;
    }
    static int[] ColumnSums(int[,] matrix)
    {
        int[] summas = {0,0,0,0,0};
        for(int y = 0; y<matrix.GetLength(0); y++)
        {
            for (int x = 0; x<matrix.GetLength(1); x++)
            {
                summas[x] += matrix[y,x];
            }
        }
        return summas;
    }

    static int MainDiagonalSum(int[,] matrix)
    {
        int size = 0;
        if(matrix.GetLength(0)<matrix.GetLength(1)){size = matrix.GetLength(0);}
        else{size = matrix.GetLength(1);}
        int sum = 0;
        for(int i = 0; i<size; i++)
        {
            sum += matrix[i,i];
        }
        return sum;
    }
    static int SecondaryDiagonalSum(int[,] matrix)
    {
        int size = 0;
        if(matrix.GetLength(0)<matrix.GetLength(1)){size = matrix.GetLength(0);}
        else{size = matrix.GetLength(1);}
        int sum = 0;
        for(int i = 0; i<size; i++)
        {
            sum += matrix[matrix.GetLength(0)-1-i,i];
        }
        return sum;
    }
    static int[] FindNumber(int[,] matrix, int number)
    {
        int[] c = {-1,-1};
        for(int y = 0; y<matrix.GetLength(0); y++)
        {
            for (int x = 0; x<matrix.GetLength(1); x++)
            {
                if(matrix[y,x]==number)
                {
                    c[0] = y; 
                    c[1] = x;
                }
            }
        }
        return c;
    }
    static void Main()
    {
        Random rnd = new Random();
        int sizeX = 0;
        int sizeY = 0;
        bool derSK = false;
        while(!derSK)
        {
            Console.Write("Ievadiet kolonu skaitu: ");
            sizeX = Int32.Parse(Console.ReadLine());
            Console.Write("Ievadiet rindu skaitu: ");
            sizeY = Int32.Parse(Console.ReadLine());
            if(sizeX>0 && sizeY>0){derSK = true;}
        }
        int[,] matrica = new int [sizeY, sizeX];
        for(int y = 0; y<matrica.GetLength(0); y++)
        {
            for (int x = 0; x<matrica.GetLength(1); x++)
            {
                matrica[y,x] = rnd.Next(-100, 101);
            }
        }

        bool cikls = true;
        while(cikls)
        {
            Console.WriteLine("1 – Izdrukāt matricu");
            Console.WriteLine("2 – Atrast lielāko un mazāko elementu");
            Console.WriteLine("3 – Aprēķināt katras kolonnas summu");
            Console.WriteLine("4 – Atrast galvenās diagonāles summu");
            Console.WriteLine("5 – Atrast blakusdiagonāles summu");
            Console.WriteLine("6 – Transponēt matricu");
            Console.WriteLine("7 – Meklēt skaitli matricā (lietotājs ievada skaitli)");
            Console.WriteLine("0 – Iziet");
            switch(Int32.Parse(Console.ReadLine()))
            {
                case 1:
                    PrintMatrix(matrica);
                    break;
                case 2:
                    Console.WriteLine("Lielākais skaitlis: " + FindMax(matrica));
                    Console.WriteLine("Mazākais skaitlis: " + FindMin(matrica));
                    break;
                case 3:
                    int[] summas = ColumnSums(matrica);
                    for(int i = 0; i<summas.GetLength(0); i++)
                    {
                        Console.WriteLine($"{i+1} kolonas summa ir {summas[i]}");
                    }
                    break;
                case 4:
                    Console.WriteLine("Diagonāles summa: " + MainDiagonalSum(matrica));
                    break;
                case 5:
                    Console.WriteLine("Diagonāles summa: " + SecondaryDiagonalSum(matrica));
                    break;
                case 6:
                    
                    break;
                case 7:
                    Console.Write("Ievadtie skaitli meklēšanai: ");
                    int[] c = FindNumber(matrica, Int32.Parse(Console.ReadLine()));
                    if(c[0] == -1)
                    {
                        Console.WriteLine("Skaitlis nav atrasts");
                    }
                    else
                    {
                        Console.WriteLine($"Skaitļa koordinātes ir {c[0]}, {c[1]}");
                    }
                    break;
                case 0:
                    cikls = false;
                    break;
                
            }
        }

    }

    
}