using System;
namespace ArrayTasks
{
class Program
{
static void Main(string[] args)
{
int rows = 5;
int cols = 6;
int[,] matrix = InputMatrix(rows, cols);
Console.WriteLine("\nВведенный массив:");
PrintMatrix(matrix);
int[] resultArray = GetMaxAbsByColumns(matrix);
Console.WriteLine("\nОдномерный массив (наибольшие по модулю элементы столбцов):");
PrintArray(resultArray);
}
static int[,] InputMatrix(int rows, int cols)
{
int[,] matrix = new int[rows, cols];
Console.WriteLine($"Введите элементы массива {rows}x{cols}:");
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write($"Элемент [{i},{j}]: ");
while (!int.TryParse(Console.ReadLine(), out matrix[i, j]))
{
Console.Write("Ошибка! Введите целое число: ");
}
}
}
return matrix;
}
static int[] GetMaxAbsByColumns(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
int[] result = new int[cols];
for (int j = 0; j < cols; j++)
{
int maxAbs = Math.Abs(matrix[0, j]);
for (int i = 1; i < rows; i++)
{
int currentAbs = Math.Abs(matrix[i, j]);
if (currentAbs > maxAbs)
{
maxAbs = currentAbs;
}
}
result[j] = maxAbs;
}
return result;
}
static void PrintMatrix(int[,] matrix)
{
int rows = matrix.GetLength(0);
int cols = matrix.GetLength(1);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
Console.Write($"{matrix[i, j],5} ");
}
Console.WriteLine();
}
}
static void PrintArray(int[] array)
{
foreach (int element in array)
{
Console.Write($"{element} ");
}
Console.WriteLine();
}
}
}