Вывести количество строк/столбцов матрицы, элементы которых монотонно возрастают/убывают - C#
Формулировка задачи:
Дана матрица размера N×M. Вывести количество строк1|столбцов2,
элементы которых монотонно возрастают3|убывают4.
Решение задачи: «Вывести количество строк/столбцов матрицы, элементы которых монотонно возрастают/убывают»
textual
Листинг программы
- class Program
- {
- static Random rnd = new Random();
- static void Main(string[] args)
- {
- do
- {
- int m, n;
- do
- {
- Console.WriteLine("Введите количество строк");
- int.TryParse(Console.ReadLine(), out m);
- Console.WriteLine("Введите количество Столбцов");
- int.TryParse(Console.ReadLine(), out n);
- }
- while (n <= 0 || m <= 0);
- int[,] ar = new int[m, n];
- int rowubiv = 0;//по убывапнию строк
- CreateArray(ar);
- ShowArray(ar);
- int countvozrast = AmountRow(ar,ref rowubiv);//по возрастанию строк
- Console.WriteLine($"Количество монотонно возрастающих строк {countvozrast}");
- Console.WriteLine($"Количество монотонно убывающих строк {rowubiv}");
- Console.ReadKey();
- } while (!(Console.ReadKey(true).Key == ConsoleKey.Escape));
- }
- static void ShowArray(int [,] ar )
- {
- for (int i = 0; i < ar.GetLength(0); i++)
- {
- for (int j= 0; j < ar.GetLength(1); j++)
- {
- Console.Write($"{ar[i,j]} ");
- }
- Console.WriteLine();
- }
- }
- static void CreateArray(int[,] ar)
- {
- for (int i = 0; i < ar.GetLength(0); i++)
- {
- for (int j = 0; j < ar.GetLength(1); j++)
- {
- ar[i, j] = rnd.Next(10);
- }
- }
- }
- static int AmountRow(int[,] ar,ref int rowubiv)
- {
- int count = 0;
- int countrowubiv = 0;
- int countrow = 0;
- for (int i = 0; i < ar.GetLength(0); i++)
- {
- for (int j = 0; j < ar.GetLength(1); j++)
- {
- if (j < ar.GetLength(1) - 1 && ar[i, j] <ar[i, j + 1])
- {
- count++;
- if (count == ar.GetLength(1) - 1)
- countrow++;
- }
- if (j < ar.GetLength(1) - 1 && ar[i, j] > ar[i, j + 1])
- {
- countrowubiv++;
- if (countrowubiv == ar.GetLength(1) - 1)
- rowubiv++;
- }
- }
- count = 0;
- countrowubiv = 0;
- }
- return countrow;
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д