Advertisement
teyn_superior

Untitled

Nov 5th, 2019
528
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 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 teyn_superior
  8. {
  9.     class Helper
  10.     {
  11.         public static Random rng = new Random();
  12.  
  13.         public static int IntReader()
  14.         {
  15.             int num = (int)default;
  16.             bool success = (bool)default;
  17.             while (!success)
  18.             {
  19.                 if (int.TryParse(Console.ReadLine(), out num) && num > 0)
  20.                 {
  21.                     success = true;
  22.                 }
  23.                 else
  24.                 {
  25.                     Console.WriteLine("Неправильный ввод, введите число снова");
  26.                 }
  27.             }
  28.             return num;
  29.         }
  30.  
  31.         public static int[][] RandomMatrixGenerator(int n, int m)
  32.         {
  33.             int[][] array = new int[n][];
  34.             try
  35.             {
  36.                 for (int i = 0; i < n; i++)
  37.                 {
  38.                     array[i] = new int[m];
  39.                     for (int g = 0; g < m; g++)
  40.                     {
  41.                         array[i][g] = rng.Next(-257, 257);
  42.                     }
  43.                 }
  44.             }
  45.             catch (Exception ex)
  46.             {
  47.                 Console.WriteLine("При формировании массива возникла ошибка: " + ex.Message);
  48.             }
  49.             return array;
  50.         }
  51.  
  52.         public static void ArraySum(int[][] array)
  53.         {
  54.             Console.WriteLine();
  55.             int sum = (int)default;
  56.             for (int i = 0; i < array.Length; i++)
  57.             {
  58.                 for (int g = 0; g < array[i].Length; g++)
  59.                 {
  60.                     if (array[i][g] < 0)
  61.                     {
  62.                         sum += array[i][g];
  63.                     }
  64.                 }
  65.             }
  66.             Console.WriteLine("Сумма положительных элементов: " + sum);
  67.         }
  68.         public static void ArrayToConsole(int[][] array)
  69.         {
  70.             Console.WriteLine();
  71.             for (int i = 0; i < array.Length; i++)
  72.             {
  73.                 for (int g = 0; g < array[i].Length; g++)
  74.                 {
  75.                     Console.Write(array[i][g] + "   ");
  76.                 }
  77.                 Console.WriteLine();
  78.             }
  79.         }
  80.     }
  81.  
  82.     class Program
  83.     {
  84.         private static void Main(string[] args)
  85.         {
  86.             bool shutdown = (bool)default;
  87.             while (!shutdown)
  88.             {
  89.                 Console.WriteLine("Введите N");
  90.                 int n = Helper.IntReader();
  91.                 Console.WriteLine("Введите M");
  92.                 int m = Helper.IntReader();
  93.  
  94.                 int[][] array = Helper.RandomMatrixGenerator(n, m);
  95.                 Helper.ArrayToConsole(array);
  96.                 Helper.ArraySum(array);
  97.  
  98.                 Console.WriteLine("Запустить решение сначала? Введите 1, чтобы запустить, любое другое число, чтобы завершить работу программы.");
  99.                 shutdown = Helper.IntReader() == 1 ? false : true;
  100.             }
  101.         }
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement