Advertisement
filin

laba_4_z_4_new

Oct 20th, 2016
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.19 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_4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.Title = "удалить все столбцы, в которых первый элемент больше последнего";
  14.             Console.Write("Ведите натуральное число, n= ");
  15.             int n =EnterInt();
  16.             Console.Write("Ведите натуральное число, m= ");
  17.             int m = EnterInt();
  18.             int test = m;
  19.             int[,] arr = new int[n, m];
  20.             FillingArray(arr);
  21.             Display(arr);
  22.             Checking(arr, ref test);
  23.             Console.WriteLine("**************");
  24.             SetToZero(arr, test);
  25.             Display(arr);
  26.             Console.ReadKey();
  27.         }
  28.         static void FillingArray(int[,] arr)
  29.         {
  30.             Random Rand = new Random();
  31.             for (int i = 0; i < arr.GetLength(0); i++)
  32.             {
  33.                 for (int j = 0; j < arr.GetLength(1); j++)
  34.                 {
  35.                     arr[i, j] = Rand.Next(9);
  36.                 }
  37.             }
  38.         }
  39.         static void Display(int[,] arr)
  40.         {
  41.             for (int i = 0; i < arr.GetLength(0); i++)
  42.             {
  43.                 for (int j = 0; j < arr.GetLength(1); j++)
  44.                 {
  45.                     Console.Write("{0} ", arr[i, j]);
  46.                 }
  47.                 Console.WriteLine();
  48.             }
  49.         }
  50.         static void Checking(int[,] arr, ref int test)
  51.         {
  52.             int temp = arr.GetLength(1);
  53.             for (int j = 0; j < temp; j++)
  54.             {
  55.                 if (arr[0, j] > arr[arr.GetLength(0) - 1, j])
  56.                 {
  57.                     for (int i = 0; i < arr.GetLength(0); i++)
  58.                     {
  59.                         for (int k = j; k < temp - 1; k++)
  60.                         {
  61.                             Swap(ref arr[i, k], ref arr[i, k + 1]);
  62.                         }
  63.                     }
  64.                     j--;
  65.                     temp--;
  66.                     test--;
  67.                 }
  68.             }
  69.         }
  70.         static void SetToZero(int[,] arr, int test)
  71.         {
  72.             for (int i = 0; i < arr.GetLength(0); i++)
  73.             {
  74.                 for (int j = test; j < arr.GetLength(1); j++)
  75.                 {
  76.                     arr[i, j] = 0;
  77.                 }
  78.             }
  79.         }
  80.         static void Swap(ref int a, ref int b)
  81.         {
  82.             int temp = a;
  83.             a = b;
  84.             b = temp;
  85.         }
  86.         static int EnterInt()
  87.         {
  88.             int value;
  89.             bool result = false;
  90.             result = int.TryParse(Console.ReadLine(), out value);
  91.             if (result == false)
  92.             {
  93.                 do
  94.                 {
  95.                     Console.Write("Некорректные данные. Введите заново: ");
  96.                     result = int.TryParse(Console.ReadLine(), out value);
  97.                 }
  98.                 while (!result);
  99.             }
  100.             return value;
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement