Advertisement
OldBeliver

Array_02

Mar 14th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.25 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 CSharpArray2D_02
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int lineSize = 10;
  14.             int columnSize = 10;
  15.             int[,] matrixA = new int[lineSize, columnSize];
  16.             Random rand = new Random();
  17.             int lowerLimit = 0;
  18.             int upperLimit = 9;
  19.             int maxElement = int.MinValue;
  20.             int minElement;
  21.             int paintedElement = int.MinValue;
  22.             ConsoleColor color;
  23.             bool isOpen = true;
  24.             bool clone = false;
  25.                        
  26.             for (int i = 0; i < matrixA.GetLength(0); i++)
  27.             {
  28.                 for (int j = 0; j < matrixA.GetLength(1); j++)
  29.                 {
  30.                     matrixA[i, j] = rand.Next(lowerLimit, upperLimit);
  31.                 }                
  32.             }
  33.  
  34.             while (isOpen)
  35.             {
  36.                 Console.SetCursorPosition(0, 15);
  37.                 Console.WriteLine("Исходная матрица");
  38.                 for (int i = 0; i < matrixA.GetLength(0); i++)
  39.                 {
  40.                     for (int j = 0; j < matrixA.GetLength(1); j++)
  41.                     {
  42.                         if (matrixA[i,j] == paintedElement)
  43.                         {
  44.                             color = Console.BackgroundColor;
  45.                             Console.BackgroundColor = ConsoleColor.Red;
  46.                             Console.Write($" {matrixA[i, j]} ");
  47.                             Console.BackgroundColor = color;
  48.                         }
  49.                         else
  50.                         {
  51.                             Console.Write($" {matrixA[i, j]} ");
  52.                         }                        
  53.                     }
  54.                     Console.WriteLine();
  55.                 }                
  56.                 if (clone)
  57.                 {
  58.                     Console.WriteLine("\nПолученная матрица");
  59.                     for (int i = 0; i < matrixA.GetLength(0); i++)
  60.                     {
  61.                         for (int j = 0; j < matrixA.GetLength(1); j++)
  62.                         {
  63.                             if (matrixA[i,j] == maxElement)
  64.                             {
  65.                                 matrixA[i, j] = 0;
  66.                             }
  67.                             Console.Write($" {matrixA[i, j]} ");                            
  68.                         }
  69.                         Console.WriteLine();
  70.                     }
  71.                     clone = false;
  72.                 }
  73.  
  74.                 Console.SetCursorPosition(0, 0);
  75.                 Console.WriteLine("Наибольший и наименьший элемент массива");
  76.                 Console.WriteLine($"Сформирован массив {lineSize}x{columnSize}, заполненный числовыми значениями от {lowerLimit} до {upperLimit-1}");
  77.                 Console.WriteLine("\n1 - Найти наибольшее значение\n\n2 - Обнулить наибольшее занчение\n\n3 - Найти наименьшее значение\n\n5 - Настройки массива\n\n6 - Выход");
  78.                 Console.Write("\nВведите номер команды: ");
  79.  
  80.                 switch (Console.ReadLine())
  81.                 {
  82.                     case "1":
  83.                         maxElement = int.MinValue;
  84.                         for (int i = 0; i < matrixA.GetLength(0); i++)
  85.                         {
  86.                             for (int j = 0; j < matrixA.GetLength(1); j++)
  87.                             {
  88.                                 if(maxElement < matrixA[i, j])
  89.                                 {
  90.                                     maxElement = matrixA[i, j];
  91.                                 }
  92.                             }
  93.                         }
  94.                         paintedElement = maxElement;                        
  95.                         break;
  96.                     case "2":
  97.                         clone = true;
  98.                         break;
  99.                     case "3":
  100.                         minElement = int.MaxValue;
  101.                         for (int i = 0; i < matrixA.GetLength(0); i++)
  102.                         {
  103.                             for (int j = 0; j < matrixA.GetLength(1); j++)
  104.                             {
  105.                                 if(minElement > matrixA[i, j])
  106.                                 {
  107.                                     minElement = matrixA[i, j];
  108.                                 }
  109.                             }
  110.                         }
  111.                         paintedElement = minElement;
  112.                         break;
  113.                     case "5":
  114.                         Console.Clear();
  115.                         Console.WriteLine("Настройки массива\n");
  116.                         Console.Write("Введите количество строк: ");
  117.                         lineSize = Convert.ToInt32(Console.ReadLine());
  118.                         Console.Write("Введите количество столбцов: ");
  119.                         columnSize = Convert.ToInt32(Console.ReadLine());
  120.                         if (lineSize <= 0 || columnSize <= 0)
  121.                         {
  122.                             Console.WriteLine("Некорректное число");
  123.                             Console.ReadKey();
  124.                             isOpen = false;
  125.                             break;
  126.                         }
  127.                         Console.Write("Введите минимальное число: ");
  128.                         lowerLimit = Convert.ToInt32(Console.ReadLine());
  129.                         Console.Write("Введите максимальное число: ");
  130.                         upperLimit = Convert.ToInt32(Console.ReadLine())+1;
  131.                         if (lowerLimit > upperLimit)
  132.                         {
  133.                             Console.WriteLine("Ошибка ввода. Минимальное значение не может быть больше максимального");
  134.                             Console.ReadKey();
  135.                             isOpen = false;
  136.                             break;
  137.                         }
  138.                        
  139.                         matrixA = new int[lineSize, columnSize];
  140.                         for (int i = 0; i < matrixA.GetLength(0); i++)
  141.                         {
  142.                             for (int j = 0; j < matrixA.GetLength(1); j++)
  143.                             {
  144.                                 matrixA[i, j] = rand.Next(lowerLimit, upperLimit);                                
  145.                             }                            
  146.                         }
  147.                         paintedElement = int.MinValue;
  148.                         break;
  149.                     case "6":
  150.                         isOpen = false;
  151.                         break;
  152.                     default:
  153.                         Console.WriteLine("неверная команда");
  154.                         break;
  155.                 }
  156.                 Console.Clear();
  157.             }            
  158.         }
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement