/*
Veidoja: Lana Lobza
Grupa: 110
Stundas Darbs 11.12.2025
Izmantotais Compiler: www.onlinegdb.com
*/
using System;
class MatricesTool
{
static void Main()
{

int rows = ReadPositiveInt("Ievadi rindu skaitu: ");
int cols = ReadPositiveInt("Ievadi kolonnu skaitu: ");

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

while (true)
{
Console.WriteLine();
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");

int choice = ReadMenuChoice();
Console.WriteLine();

switch (choice)
{
case 1:
PrintMatrix(matrix);
break;
case 2:
int max = FindMax(matrix);
int min = FindMin(matrix);
Console.WriteLine($"Lielākais elements: {max}");
Console.WriteLine($"Mazākais elements: {min}");
break;
case 3:
int[] sums = ColumnSums(matrix);
Console.WriteLine("Kolonnu summas:");
for (int c = 0; c < sums.Length; c++)
Console.WriteLine($"Kolonna {c}: {sums[c]}");
break;
case 4:
int mainDiag = MainDiagonalSum(matrix);
Console.WriteLine($"Galvenās diagonāles summa: {mainDiag}");
break;
case 5:
int secDiag = SecondaryDiagonalSum(matrix);
Console.WriteLine($"Blakusdiagonāles summa: {secDiag}");
break;
case 6:
matrix = Transpose(matrix);
Console.WriteLine("Matrica transponēta (jauns izmērs). Tagad izmantotā matrica ir transponētā.");
break;
case 7:
int number = ReadIntWithPrompt("Ievadi meklējamo skaitli: ");
var found = FindNumber(matrix, number);
if (found.found)
Console.WriteLine($"Skaitlis {number} atrasts rindā {found.row}, kolonnā {found.col}.");
else
Console.WriteLine($"Skaitlis {number} nav atrasts matricā.");
break;
case 0:
return;
default:
break;
}

}

}

static int ReadPositiveInt(string prompt)
{
int value;
while (true)
{
Console.Write(prompt);
string input = Console.ReadLine();
if (!int.TryParse(input, out value))
{
Console.WriteLine("Lūdzu ievadi veselskaitli.");
continue;
}
if (value <= 0)
{
Console.WriteLine("Skaitlim jābūt pozitīvam (vairāk nekā 0). Mēģini vēlreiz.");
continue;
}
return value;
}

}

static int ReadMenuChoice()
{
while (true)
{
Console.Write("Izvēlies darbību (0-7): ");
string s = Console.ReadLine();
if (!int.TryParse(s, out int choice))
{
Console.WriteLine("Nederīga izvēle — ievadi skaitli no 0 līdz 7.");
continue;
}
if (choice < 0 || choice > 7)
{
Console.WriteLine("Izvēle ārpus diapazona. Ievadi no 0 līdz 7.");
continue;
}
return choice;
}

}

static int ReadIntWithPrompt(string prompt)
{
while (true)
{
Console.Write(prompt);
string s = Console.ReadLine();
if (int.TryParse(s, out int v)) return v;
Console.WriteLine("Lūdzu ievadi derīgu veselskaitli.");
}

}

//Matricas ģenerēšana
static int[,] GenerateRandomMatrix(int rows, int cols, int minVal, int maxVal)
{
var rnd = new Random();
int[,] m = new int[rows, cols];
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
m[r, c] = rnd.Next(minVal, maxVal + 1);
return m;
}

//Nepieciešamās metodes
static void PrintMatrix(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
Console.WriteLine($"Matricas izmērs: {rows} x {cols}");
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
{
Console.Write($"{matrix[r, c],6} ");
}
Console.WriteLine();
}

}

static int FindMax(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
int max = matrix[0, 0];
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
if (matrix[r, c] > max) max = matrix[r, c];
return max;
}

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

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

static int MainDiagonalSum(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
int n = Math.Min(rows, cols);
int sum = 0;
for (int i = 0; i < n; i++) sum += matrix[i, i];
return sum;
}

static int SecondaryDiagonalSum(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
int n = Math.Min(rows, cols);
int sum = 0;
// Blakusdiagonāles indeksu aprēķins ir atkarīgs no matricas formas.
// Ja matrica nav kvadrātiska, izmanto elementus, kuru rindu un kolonnu indeksi apmierina r + c = n - 1.
// Ja rindas un kolonnas atšķiras, izmanto augšējo kreiso n × n apakšlaukumu.
for (int i = 0; i < n; i++)
{
int r = i;
int c = (n - 1) - i;
sum += matrix[r, c];
}
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)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
if (matrix[r, c] == number) return (true, r, c);
return (false, -1, -1);
}

}