/*
Autors: Andrejs Šorecs
Grupa: 110
Praktiskais darbs: stundas darbs 11.12.2025
Izmantotais Compilar: Visual Studio app.net framework
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2d_gruts
{
    internal class Program
    {
        static void Main()
        {
            int rinda = ReadPositiveInt("Ievadi rindu skaitu: ");
            int kolona = ReadPositiveInt("Ievadi kolonu skaitu: ");
            int[,] matrica= Generatematrica(rinda, kolona);
            bool menu= true;
            do
            {
                Console.WriteLine();
                Console.WriteLine("1. Izdrukat matricu");
                Console.WriteLine("2. Atrast lielako un mazāko elementu");
                Console.WriteLine("3. Aprekinat katras kolonnas summu");
                Console.WriteLine("4. Atrast galvenas diagonales summu");
                Console.WriteLine("5. Atrast blakusdiagonales summu");
                Console.WriteLine("6. Transponēt matricu");
                Console.WriteLine("7. Meklet skaitli matrica");
                Console.WriteLine("0. Iziet");
                int umenu = Convert.ToInt32(Console.ReadLine());
                switch(umenu)
                {
                    case 1: printmatrix(matrica);
                        break;
                    case 2: FindMax(matrica);
                        Console.WriteLine();
                        FindMin(matrica);
                        break;
                    case 3: ColumnSums(matrica);
                        break;
                    case 4: MainDiagonales(matrica);
                        break;
                    case 5: Secondarydiagonales(matrica); 
                        break;
                    case 6: Transpose(matrica);
                        break;
                    case 7: Findnumber(matrica);
                        break;
                    case 0: menu=false;
                        break;
                }
            }
            while (menu);
        }
        static int ReadPositiveInt(string message)
        {
            int rk;
            bool rpi = true;
            do
            {
                Console.Write(message);
                string sk = Console.ReadLine();
                if (int.TryParse(sk, out rk) && rk > 0)
                {
                    rpi = false;
                }
            }
            while (rpi);
            return rk;
        }
        static int[,] Generatematrica(int rinda, int kolona)
        {
            int[,] matrica= new int[rinda, kolona];
            Random rmatric = new Random();
            for (int i=0; i<rinda; i++)
            {
                for (int j=0; j<kolona; j++)
                {
                    matrica[i, j]= rmatric.Next(-100,101);
                }
            }

            return matrica;
        }
        static void printmatrix(int[,] matrica)
        {
            
            for (int i=0;i<matrica.GetLength(0);i++)
            {
                for(int l=0; l<matrica.GetLength(1);l++)
                {
                    Console.Write("+-----");
                }
                Console.WriteLine("+");
                for(int j=0;j<matrica.GetLength(1);j++)
                {
                    Console.Write($"|{matrica[i,j].ToString().PadLeft(5)}");
                }
                Console.WriteLine("|");

            }
            for (int l = 0; l < matrica.GetLength(1); l++)
            {
                Console.Write("+-----");
            }
            Console.WriteLine("+");
        }
        static void FindMax (int[,] matrica)
        {
            int[] matrix = new int [matrica.GetLength(0)];
            int[] matrix1 = new int [matrica.GetLength(1)];
            for (int i=0; i < matrica.GetLength(0);i++)
            {
                for (int j = 0; j < matrica.GetLength(1); j++)
                {
                    matrix1[j] = matrica[i,j];
                }
                matrix[i] = matrix1.Max();
            }

            Console.Write(matrix.Max());
        
        }
        static void FindMin (int[,] matrica)
        {
            int[] matrix = new int[matrica.GetLength(0)];
            int[] matrix1 = new int[matrica.GetLength(1)];
            for (int i = 0; i < matrica.GetLength(0); i++)
            {
                for (int j = 0; j < matrica.GetLength(1); j++)
                {
                    matrix1[j] = matrica[i, j];
                }
                matrix[i] = matrix1.Min();
;            }

            Console.Write(matrix.Min());
        }
        static int ColumnSums (int[,] matrica)
        {
            
            for (int i = 0;i < matrica.GetLength(1);i++)
            {
                int suma = 0;
                for(int j = 0;j < matrica.GetLength(0);j++)
                {
                    suma += matrica[j,i];
                }
                Console.WriteLine($"Katras kolonas summa ir {i + 1}: {suma}");
            }
            return matrica.GetLength(0);
        }
        static void MainDiagonales(int[,] matrica)
        {
            int dig = Math.Min(matrica.GetLength(0), matrica.GetLength(1));
            int summa = 0;
            for (int i = 0; i < dig; i++)
            {
                summa += matrica[i, i];
            }
            Console.WriteLine($"diagonales summa ir: {summa}");
        }
        static void Secondarydiagonales(int[,] matrica)
        {
            int dig = Math.Min(matrica.GetLength(0), matrica.GetLength(1));
            int suma = 0;
            for (int i = 0; i < dig; i++)
            {
                suma += matrica[i, matrica.GetLength(1)-1 - i];
            }
            Console.WriteLine($"blakus diagonales summa ir : {suma}");
        }
        static void Transpose(int[,] matrica)
        {
            int[,] matrica2 = new int[matrica.GetLength(1),matrica.GetLength(0)];
            for (int i = 0;i<matrica.GetLength(1);i++)
            {
                for(int j = 0;j<matrica.GetLength(0);j++)
                {
                 matrica2[(int)i,j] = matrica[j,i];
                }
            }printmatrix(matrica2);
        }
        static void Findnumber(int[,] matrica)
        {
            Console.WriteLine("Ievadiet skaitli");
            string cp = Console.ReadLine();
            if(int.TryParse(cp,out int mekcp))
            {
                bool mekl = false;
                for (int i = 0; i<matrica.GetLength(0);i++)
                {
                    for( int j = 0; j<matrica.GetLength(1);j++)
                    {
                        if (matrica[i,j] ==mekcp)
                        {
                            Console.WriteLine($"Cipars {mekcp} atrasts pozicjia: Rinda [{i}], Kolona[{j}]");
                            mekl = true;
                        }
                    }
                }
                if(!mekl)
                {
                    Console.WriteLine("Cipar nebija atrasts");
                }
            }
        }
    }
}