Загрузка данных


using System;
using System.Text;

namespace Lab3_Variant20
{
    class Program
    {
        static void Main()
        {
            Console.OutputEncoding = Encoding.UTF8;

            Console.Write("Введите размер квадратной матрицы n: ");
            int n;

            while (!int.TryParse(Console.ReadLine(), out n) || n <= 0)
            {
                Console.Write("Ошибка. Введите целое n больше 0: ");
            }

            int[,] a = new int[n, n];

            Console.WriteLine("Введите элементы матрицы {0}x{0}:", n);

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write("a[{0},{1}] = ", i + 1, j + 1);

                    while (!int.TryParse(Console.ReadLine(), out a[i, j]))
                    {
                        Console.Write("Ошибка. Введите целое число: ");
                    }
                }
            }

            Console.WriteLine("\nИсходная матрица:");

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    Console.Write("{0,6:D}", a[i, j]);
                }

                Console.WriteLine();
            }

            Console.WriteLine("\nПозиции минимальных элементов:");

            for (int j = 0; j < n; j++)
            {
                int count;

                int[] positions = FindMinPositions(n, j, ref a, out count);

                Console.Write("Столбец {0}: ", j + 1);

                for (int i = 0; i < count; i++)
                {
                    Console.Write("a[{0},{1}] ", positions[i], j + 1);
                }

                Console.WriteLine();
            }

            Console.ReadKey();
        }

        static int[] FindMinPositions(int size, int column, ref int[,] a, out int count)
        {
            int min = a[0, column];

            for (int i = 1; i < size; i++)
            {
                if (a[i, column] < min)
                {
                    min = a[i, column];
                }
            }

            int[] positions = new int[size];
            count = 0;

            for (int i = 0; i < size; i++)
            {
                if (a[i, column] == min)
                {
                    positions[count] = i + 1;
                    count++;
                }
            }

            return positions;
        }
    }
}