Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace _2020_lab1
- {
- class Program
- {
- static void Main(string[] args)
- {
- int option;
- while (true)
- {
- try
- {
- Console.Write("выберите\n" +
- "1. Размер массива вводит пользователь, массив генерируется случайным образом\n" +
- "2. Размер массива до 10 элементов генерируется случайным образом, элементы массива вводит пользователь.\n" +
- ">>> ");
- option = Convert.ToInt32(Console.ReadLine());
- if (option == 1 || option == 2)
- { break; }
- else
- {
- Console.WriteLine("выберите 1 или 2");
- continue;
- }
- }
- catch
- {
- Console.WriteLine("не некорректное значение");
- continue;
- }
- }
- Random rnd = new Random();
- int max_negative = -101;
- if (option == 1)
- {
- int mas_lenght;
- while (true)
- {
- Console.Write("размер массива >>> ");
- try
- {
- mas_lenght = Convert.ToInt32(Console.ReadLine());
- break;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- continue;
- }
- }
- int[] array = new int[mas_lenght];
- for (int i = 0; i < array.Length; i++)
- {
- array[i] = rnd.Next(-100, 100);
- Console.Write(array[i] + " ");
- if (array[i] < 0 & array[i] > max_negative) { max_negative = array[i]; }
- }
- }
- else
- {
- int s = rnd.Next(1, 10);
- int[] array = new int[s];
- Console.WriteLine("количество элементов = " + s);
- for (int i = 0; i < array.Length; i++)
- {
- //Console.Write(i+1 + " элемент = ");
- while (true)
- {
- try
- {
- Console.Write(i + 1 + " элемент (от -100 до 100) = ");
- array[i] = Convert.ToInt32(Console.ReadLine());
- if (array[i] < -100) { Console.WriteLine("выход из диапазона "); continue; }
- break;
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- continue;
- }
- }
- //array[i] = Convert.ToInt32(Console.ReadLine());
- if (array[i] < 0 & array[i] > max_negative) { max_negative = array[i]; }
- }
- }
- if (max_negative == -101) { Console.WriteLine("\nотрицательных элементов нет"); }
- else { Console.Write("\nмаксимум из отрицательных элементов -> " + max_negative); }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement