Advertisement
filin

laba_4_z_1_new

Oct 20th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 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 laba_4
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.Title = "Laba4_1";
  14.             Console.WriteLine("Определить количество четных элементов, расположенных ниже главной диагонали.");
  15.             Console.Write("Ведите натуральное число, n= ");
  16.             int n = EnterInt();
  17.             int[,] arr = new int[n, n];
  18.             FillingArray(arr);
  19.             DisplayArray(arr);
  20.             Console.WriteLine("Количество четных элементов, расположенных ниже главной диагонали = {0}", Counting(arr));
  21.             Console.ReadKey();
  22.         }
  23.         static void FillingArray( int[,] arr)
  24.         {
  25.             Random Rand = new Random();
  26.             for (int i = 0; i < arr.GetLength(0); i++)
  27.             {
  28.                 for (int j = 0; j < arr.GetLength(1); j++)
  29.                 {
  30.                     arr[i, j] = Rand.Next(9);
  31.                 }
  32.             }
  33.         }
  34.         static void DisplayArray(int[,] arr)
  35.         {
  36.             for (int i = 0; i < arr.GetLength(0); i++)
  37.             {
  38.                 for (int j = 0; j < arr.GetLength(1); j++)
  39.                 {
  40.                     Console.Write("{0} ", arr[i, j]);
  41.                 }
  42.                 Console.WriteLine();
  43.             }
  44.         }
  45.         static int Counting(int[,] arr)
  46.         {
  47.             int count = 0;
  48.             for (int i = 0; i < arr.GetLength(0); i++)
  49.             {
  50.                 for (int j = 0; j < i; j++)
  51.                 {
  52.                     if (arr[i, j] % 2 == 0)
  53.                     {
  54.                         count++;
  55.                     }
  56.                 }
  57.             }
  58.             return count;
  59.         }
  60.         static int EnterInt()
  61.         {
  62.             int value;
  63.             bool result = false;
  64.             result = int.TryParse(Console.ReadLine(), out value);
  65.             if (result == false)
  66.             {
  67.                 do
  68.                 {
  69.                     Console.Write("Некорректные данные. Введите заново: ");
  70.                     result = int.TryParse(Console.ReadLine(), out value);
  71.                 }
  72.                 while (!result);
  73.             }
  74.             return value;
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement