Advertisement
Cinder1986

Lab18-2

Mar 26th, 2023
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.71 KB | None | 0 0
  1. namespace ConsoleApp1
  2. {
  3.     class Program
  4.     {
  5.         const long intMin = 1;
  6.         const long intMax = 1 << 30;
  7.         const int taskArrSize = 20;
  8.  
  9.         static int takeInt()
  10.         {
  11.             int value = 0;
  12.             bool isCorrect;
  13.             do
  14.             {
  15.                 isCorrect= true;
  16.                 try
  17.                 {
  18.                     value = Convert.ToInt32(Console.ReadLine());
  19.                 }
  20.                 catch (Exception)
  21.                 {
  22.                     Console.WriteLine("Type mismatch!");
  23.                     isCorrect = false;
  24.                 }
  25.                 if(isCorrect && (value > intMax || value < intMin))
  26.                 {
  27.                     Console.WriteLine("Value out of range!");
  28.                     isCorrect= false;
  29.                 }
  30.             } while (!isCorrect);
  31.             return value;
  32.         }
  33.  
  34.         static int[] takeIntArray(int size)
  35.         {
  36.             int[] intArr = new int[size];
  37.             for(int i = 0;i < size; i++)
  38.             {
  39.                 Console.Write($"Enter the value of {i + 1} element: ");
  40.                 intArr[i] = takeInt();
  41.             }
  42.             return intArr;
  43.         }
  44.  
  45.         static bool checkArrElement(int element)
  46.         {
  47.             return element < 25;
  48.         }
  49.  
  50.         static void showTaskElements(int[] intArr)
  51.         {
  52.             int arrSize = intArr.Length;
  53.             for (int i = 0; i < arrSize; i++)
  54.                 if (checkArrElement(intArr[i]))
  55.                     Console.WriteLine($"Element {i + 1} - {intArr[i]}");
  56.         }
  57.  
  58.         static void Main(string[] args)
  59.         {
  60.             showTaskElements(takeIntArray(taskArrSize));
  61.         }
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement