/*
Veidoja: Gustavs Skripa
Grupa: 110
Praktiskais darbs: Stundu Darbs
izmantotais compiler: https://www.programiz.com
*/


using System;

class Program
{
    static void Main()
    {
        int rows = ReadPositiveInt("Ievadi rindu skaitu: ");
        int cols = ReadPositiveInt("Ievadi kolonnu skaitu: ");

        int[,] matrix = GenerateRandomMatrix(rows, cols);

        int choice;
        do
        {
            Console.WriteLine();
            Console.WriteLine("1 - Izdrukat matricu");
            Console.WriteLine("2 - Atrast lielako un mazako elementu");
            Console.WriteLine("3 - Aprekinat katras kolonnas summu");
            Console.WriteLine("4 - Galvenas diagonales summa");
            Console.WriteLine("5 - Blakusdiagonales summa");
            Console.WriteLine("6 - Transponet matricu");
            Console.WriteLine("7 - Meklet skaitli matrica");
            Console.WriteLine("0 - Iziet");
            Console.Write("Izvele: ");

            choice = ReadMenuChoice(0, 7);
            Console.WriteLine();

            switch (choice)
            {
                case 1:
                    PrintMatrix(matrix);
                    break;
                case 2:
                    Console.WriteLine("Max: " + FindMax(matrix));
                    Console.WriteLine("Min: " + FindMin(matrix));
                    break;
                case 3:
                    int[] sums = ColumnSums(matrix);
                    Console.WriteLine(string.Join(", ", sums));
                    break;
                case 4:
                    Console.WriteLine(MainDiagonalSum(matrix));
                    break;
                case 5:
                    Console.WriteLine(SecondaryDiagonalSum(matrix));
                    break;
                case 6:
                    matrix = Transpose(matrix);
                    break;
                case 7:
                    int number = ReadInt("Ievadi skaitli: ");
                    var result = FindNumber(matrix, number);
                    if (result.found)
                        Console.WriteLine(result.row + " " + result.col);
                    else
                        Console.WriteLine("Nav");
                    break;
            }

        } while (choice != 0);
    }

    static int ReadPositiveInt(string text)
    {
        int n;
        do { Console.Write(text); }
        while (!int.TryParse(Console.ReadLine(), out n) || n <= 0);
        return n;
    }

    static int ReadInt(string text)
    {
        int n;
        while (true)
        {
            Console.Write(text);
            if (int.TryParse(Console.ReadLine(), out n))
                return n;
        }
    }

    static int ReadMenuChoice(int min, int max)
    {
        int n;
        while (!int.TryParse(Console.ReadLine(), out n) || n < min || n > max)
            Console.Write("Ievadi skaitli " + min + "-" + max + ": ");
        return n;
    }

    static int[,] GenerateRandomMatrix(int rows, int cols)
    {
        Random rnd = new Random();
        int[,] matrix = new int[rows, cols];
        for (int r = 0; r < rows; r++)
            for (int c = 0; c < cols; c++)
                matrix[r, c] = rnd.Next(-100, 101);
        return matrix;
    }

    static void PrintMatrix(int[,] matrix)
    {
        for (int r = 0; r < matrix.GetLength(0); r++)
        {
            for (int c = 0; c < matrix.GetLength(1); c++)
                Console.Write(matrix[r, c] + " ");
            Console.WriteLine();
        }
    }

    static int FindMax(int[,] matrix)
    {
        int max = matrix[0, 0];
        foreach (int x in matrix) if (x > max) max = x;
        return max;
    }

    static int FindMin(int[,] matrix)
    {
        int min = matrix[0, 0];
        foreach (int x in matrix) if (x < min) min = x;
        return min;
    }

    static int[] ColumnSums(int[,] matrix)
    {
        int cols = matrix.GetLength(1);
        int rows = matrix.GetLength(0);
        int[] sums = new int[cols];
        for (int c = 0; c < cols; c++)
            for (int r = 0; r < rows; r++)
                sums[c] += matrix[r, c];
        return sums;
    }

    static int MainDiagonalSum(int[,] matrix)
    {
        int size = Math.Min(matrix.GetLength(0), 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 rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        int size = Math.Min(rows, cols);
        int sum = 0;
        for (int i = 0; i < size; i++)
            sum += matrix[i, cols - 1 - i];
        return sum;
    }

    static int[,] Transpose(int[,] matrix)
    {
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);
        int[,] t = new int[cols, rows];
        for (int r = 0; r < rows; r++)
            for (int c = 0; c < cols; c++)
                t[c, r] = matrix[r, c];
        return t;
    }

    static (bool found, int row, int col) FindNumber(int[,] matrix, int number)
    {
        for (int r = 0; r < matrix.GetLength(0); r++)
            for (int c = 0; c < matrix.GetLength(1); c++)
                if (matrix[r, c] == number)
                    return (true, r, c);
        return (false, -1, -1);
    }
}
