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 teyn_superior
- {
- class Helper
- {
- public static Random rng = new Random();
- public static int IntReader()
- {
- int num = (int)default;
- bool success = (bool)default;
- while (!success)
- {
- if (int.TryParse(Console.ReadLine(), out num) && num > 0)
- {
- success = true;
- }
- else
- {
- Console.WriteLine("Неправильный ввод, введите число снова");
- }
- }
- return num;
- }
- public static int[][] RandomMatrixGenerator(int n, int m)
- {
- int[][] array = new int[n][];
- try
- {
- for (int i = 0; i < n; i++)
- {
- array[i] = new int[m];
- for (int g = 0; g < m; g++)
- {
- array[i][g] = rng.Next(-257, 257);
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("При формировании массива возникла ошибка: " + ex.Message);
- }
- return array;
- }
- public static void ArraySum(int[][] array)
- {
- Console.WriteLine();
- int sum = (int)default;
- for (int i = 0; i < array.Length; i++)
- {
- for (int g = 0; g < array[i].Length; g++)
- {
- if (array[i][g] < 0)
- {
- sum += array[i][g];
- }
- }
- }
- Console.WriteLine("Сумма положительных элементов: " + sum);
- }
- public static void ArrayToConsole(int[][] array)
- {
- Console.WriteLine();
- for (int i = 0; i < array.Length; i++)
- {
- for (int g = 0; g < array[i].Length; g++)
- {
- Console.Write(array[i][g] + " ");
- }
- Console.WriteLine();
- }
- }
- }
- class Program
- {
- private static void Main(string[] args)
- {
- bool shutdown = (bool)default;
- while (!shutdown)
- {
- Console.WriteLine("Введите N");
- int n = Helper.IntReader();
- Console.WriteLine("Введите M");
- int m = Helper.IntReader();
- int[][] array = Helper.RandomMatrixGenerator(n, m);
- Helper.ArrayToConsole(array);
- Helper.ArraySum(array);
- Console.WriteLine("Запустить решение сначала? Введите 1, чтобы запустить, любое другое число, чтобы завершить работу программы.");
- shutdown = Helper.IntReader() == 1 ? false : true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement