filin

laba_4_z_2_new

Oct 20th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Z_2
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.Title = "Laba4_2";
  14.             Console.WriteLine("Упорядочить каждую строку матрицы по возрастанию.");
  15.             Console.Write("Ведите натуральное число больше нуля, n= ");
  16.             int n = Math.Abs(EnterInt());
  17.             Console.Write("Ведите натуральное число больше нуля, m= ");
  18.             int m = Math.Abs(EnterInt());
  19.             int[,] arr = new int[n, m];
  20.             FillingArray(arr);
  21.             DisplayArray(arr);
  22.             Console.WriteLine("*******************");
  23.             ArrangeAscending(arr);
  24.             DisplayArray(arr);
  25.             Console.ReadKey();
  26.         }
  27.         static void FillingArray(int[,] arr)
  28.         {
  29.             Random Rand = new Random();
  30.             for (int i = 0; i < arr.GetLength(0); i++)
  31.             {
  32.                 for (int j = 0; j < arr.GetLength(1); j++)
  33.                 {
  34.                     arr[i, j] = Rand.Next(9);
  35.                 }
  36.             }
  37.         }
  38.         static void DisplayArray(int[,] arr)
  39.         {
  40.             for (int i = 0; i < arr.GetLength(0); i++)
  41.             {
  42.                 for (int j = 0; j < arr.GetLength(1); j++)
  43.                 {
  44.                     Console.Write("{0} ", arr[i, j]);
  45.                 }
  46.                 Console.WriteLine();
  47.             }
  48.         }
  49.         static int Counting(int[,] arr)
  50.         {
  51.             int count = 0;
  52.             for (int i = 0; i < arr.GetLength(0); i++)
  53.             {
  54.                 for (int j = 0; j < i; j++)
  55.                 {
  56.                     if (arr[i, j] % 2 == 0)
  57.                     {
  58.                         count++;
  59.                     }
  60.                 }
  61.             }
  62.             return count;
  63.         }
  64.         static void ArrangeAscending(int[,] arr)
  65.         {
  66.             //for (int i = 0; i < arr.GetLength(0); i++)
  67.             //{
  68.             //    for (int k = 0; k < arr.GetLength(1); k++)
  69.             //    {
  70.             //        for (int j = 0; j < arr.GetLength(1) - 1; j++)
  71.             //        {
  72.             //            int temp;
  73.             //            if (arr[i, j] > arr[i, j + 1])
  74.             //            {
  75.             //                temp = arr[i, j];
  76.             //                arr[i, j] = arr[i, j + 1];
  77.             //                arr[i, j + 1] = temp;
  78.             //            }
  79.             //        }
  80.             //    }
  81.             //}
  82.             for (int i = 0; i < arr.GetLength(0); i++)
  83.             {
  84.                 for (int j = 1; j < arr.GetLength(1); j++)
  85.                 {
  86.                     int temp = arr[i, j];
  87.                     int x = j - 1;
  88.                     while (x >= 0 && arr[i, x] > temp)
  89.                     {
  90.                         arr[i, x + 1] = arr[i, x];
  91.                         x--;
  92.                     }
  93.                     arr[i, x + 1] = temp;
  94.                 }
  95.             }
  96.         }
  97.         static int EnterInt()
  98.         {
  99.             int value;
  100.             bool result = false;
  101.             result = int.TryParse(Console.ReadLine(), out value);
  102.             if (result == false)
  103.             {
  104.                 do
  105.                 {
  106.                     Console.Write("Некорректные данные. Введите заново: ");
  107.                     result = int.TryParse(Console.ReadLine(), out value);
  108.                 }
  109.                 while (!result);
  110.             }
  111.             return value;
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment