Advertisement
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 CSharpArray2D_02
- {
- class Program
- {
- static void Main(string[] args)
- {
- int lineSize = 10;
- int columnSize = 10;
- int[,] matrixA = new int[lineSize, columnSize];
- Random rand = new Random();
- int lowerLimit = 0;
- int upperLimit = 9;
- int maxElement = int.MinValue;
- int minElement;
- int paintedElement = int.MinValue;
- ConsoleColor color;
- bool isOpen = true;
- bool clone = false;
- for (int i = 0; i < matrixA.GetLength(0); i++)
- {
- for (int j = 0; j < matrixA.GetLength(1); j++)
- {
- matrixA[i, j] = rand.Next(lowerLimit, upperLimit);
- }
- }
- while (isOpen)
- {
- Console.SetCursorPosition(0, 15);
- Console.WriteLine("Исходная матрица");
- for (int i = 0; i < matrixA.GetLength(0); i++)
- {
- for (int j = 0; j < matrixA.GetLength(1); j++)
- {
- if (matrixA[i,j] == paintedElement)
- {
- color = Console.BackgroundColor;
- Console.BackgroundColor = ConsoleColor.Red;
- Console.Write($" {matrixA[i, j]} ");
- Console.BackgroundColor = color;
- }
- else
- {
- Console.Write($" {matrixA[i, j]} ");
- }
- }
- Console.WriteLine();
- }
- if (clone)
- {
- Console.WriteLine("\nПолученная матрица");
- for (int i = 0; i < matrixA.GetLength(0); i++)
- {
- for (int j = 0; j < matrixA.GetLength(1); j++)
- {
- if (matrixA[i,j] == maxElement)
- {
- matrixA[i, j] = 0;
- }
- Console.Write($" {matrixA[i, j]} ");
- }
- Console.WriteLine();
- }
- clone = false;
- }
- Console.SetCursorPosition(0, 0);
- Console.WriteLine("Наибольший и наименьший элемент массива");
- Console.WriteLine($"Сформирован массив {lineSize}x{columnSize}, заполненный числовыми значениями от {lowerLimit} до {upperLimit-1}");
- Console.WriteLine("\n1 - Найти наибольшее значение\n\n2 - Обнулить наибольшее занчение\n\n3 - Найти наименьшее значение\n\n5 - Настройки массива\n\n6 - Выход");
- Console.Write("\nВведите номер команды: ");
- switch (Console.ReadLine())
- {
- case "1":
- maxElement = int.MinValue;
- for (int i = 0; i < matrixA.GetLength(0); i++)
- {
- for (int j = 0; j < matrixA.GetLength(1); j++)
- {
- if(maxElement < matrixA[i, j])
- {
- maxElement = matrixA[i, j];
- }
- }
- }
- paintedElement = maxElement;
- break;
- case "2":
- clone = true;
- break;
- case "3":
- minElement = int.MaxValue;
- for (int i = 0; i < matrixA.GetLength(0); i++)
- {
- for (int j = 0; j < matrixA.GetLength(1); j++)
- {
- if(minElement > matrixA[i, j])
- {
- minElement = matrixA[i, j];
- }
- }
- }
- paintedElement = minElement;
- break;
- case "5":
- Console.Clear();
- Console.WriteLine("Настройки массива\n");
- Console.Write("Введите количество строк: ");
- lineSize = Convert.ToInt32(Console.ReadLine());
- Console.Write("Введите количество столбцов: ");
- columnSize = Convert.ToInt32(Console.ReadLine());
- if (lineSize <= 0 || columnSize <= 0)
- {
- Console.WriteLine("Некорректное число");
- Console.ReadKey();
- isOpen = false;
- break;
- }
- Console.Write("Введите минимальное число: ");
- lowerLimit = Convert.ToInt32(Console.ReadLine());
- Console.Write("Введите максимальное число: ");
- upperLimit = Convert.ToInt32(Console.ReadLine())+1;
- if (lowerLimit > upperLimit)
- {
- Console.WriteLine("Ошибка ввода. Минимальное значение не может быть больше максимального");
- Console.ReadKey();
- isOpen = false;
- break;
- }
- matrixA = new int[lineSize, columnSize];
- for (int i = 0; i < matrixA.GetLength(0); i++)
- {
- for (int j = 0; j < matrixA.GetLength(1); j++)
- {
- matrixA[i, j] = rand.Next(lowerLimit, upperLimit);
- }
- }
- paintedElement = int.MinValue;
- break;
- case "6":
- isOpen = false;
- break;
- default:
- Console.WriteLine("неверная команда");
- break;
- }
- Console.Clear();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement