Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Z_2
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.Title = "Laba4_2";
- Console.WriteLine("Упорядочить каждую строку матрицы по возрастанию.");
- Console.Write("Ведите натуральное число больше нуля, n= ");
- int n = Math.Abs(EnterInt());
- Console.Write("Ведите натуральное число больше нуля, m= ");
- int m = Math.Abs(EnterInt());
- int[,] arr = new int[n, m];
- FillingArray(arr);
- DisplayArray(arr);
- Console.WriteLine("*******************");
- ArrangeAscending(arr);
- DisplayArray(arr);
- Console.ReadKey();
- }
- static void FillingArray(int[,] arr)
- {
- Random Rand = new Random();
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++)
- {
- arr[i, j] = Rand.Next(9);
- }
- }
- }
- static void DisplayArray(int[,] arr)
- {
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < arr.GetLength(1); j++)
- {
- Console.Write("{0} ", arr[i, j]);
- }
- Console.WriteLine();
- }
- }
- static int Counting(int[,] arr)
- {
- int count = 0;
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 0; j < i; j++)
- {
- if (arr[i, j] % 2 == 0)
- {
- count++;
- }
- }
- }
- return count;
- }
- static void ArrangeAscending(int[,] arr)
- {
- //for (int i = 0; i < arr.GetLength(0); i++)
- //{
- // for (int k = 0; k < arr.GetLength(1); k++)
- // {
- // for (int j = 0; j < arr.GetLength(1) - 1; j++)
- // {
- // int temp;
- // if (arr[i, j] > arr[i, j + 1])
- // {
- // temp = arr[i, j];
- // arr[i, j] = arr[i, j + 1];
- // arr[i, j + 1] = temp;
- // }
- // }
- // }
- //}
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int j = 1; j < arr.GetLength(1); j++)
- {
- int temp = arr[i, j];
- int x = j - 1;
- while (x >= 0 && arr[i, x] > temp)
- {
- arr[i, x + 1] = arr[i, x];
- x--;
- }
- arr[i, x + 1] = temp;
- }
- }
- }
- static int EnterInt()
- {
- int value;
- bool result = false;
- result = int.TryParse(Console.ReadLine(), out value);
- if (result == false)
- {
- do
- {
- Console.Write("Некорректные данные. Введите заново: ");
- result = int.TryParse(Console.ReadLine(), out value);
- }
- while (!result);
- }
- return value;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment