Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.88 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. namespace ConsoleApp1
  4.  
  5. {
  6.     internal class Program
  7.     {
  8.  
  9.         /// <summary>
  10.         /// This method asks uer to input a number and checks if its format is correct
  11.         /// </summary>
  12.         /// <returns>input number</returns>
  13.         private static int GetInput()
  14.         {
  15.  
  16.             string input;
  17.             int result;
  18.  
  19.             Console.WriteLine("Enter a number : ");
  20.             input = Console.ReadLine();
  21.             while (!int.TryParse(input, out result) || result < 0)
  22.             {
  23.                 Console.WriteLine("Wrong number. Try again: ");
  24.                 input = Console.ReadLine();
  25.             }
  26.             return result;
  27.         }
  28.  
  29.         /// <summary>
  30.         /// This method fills an array with random numbers.
  31.         /// If there are less than 50 numbers in range 0-100 to fill we only add even numbers
  32.         /// If there are more than 50 numbers then we add 50 even numbers in range 0-100 then
  33.         /// the rest with any numbers in range 0-900
  34.         /// </summary>
  35.         /// <param name="N">amount of numbers to put in array</param>
  36.         /// <param name="array">array to fill with randoms</param>
  37.         /// <returns>array</returns>
  38.         static int[] Filling_randoms(int N, int[] array)
  39.         {
  40.             //Defining array of numbers
  41.  
  42.             Random rng = new Random();
  43.  
  44.             //Filling array with numbers
  45.             for (int i = 0; i < (N > 50 ? 50 : N); i++)
  46.             {
  47.                 int number;
  48.                 number = rng.Next(1, 100);
  49.                 if (number % 2 != 0)
  50.                 {
  51.                     array[i] = number - 1;
  52.                 }
  53.                 else
  54.                 {
  55.                     array[i] = number;
  56.                 }
  57.             }
  58.             if (N > 50)
  59.             {
  60.                 for (int i = 50; i < N; i++)
  61.                 {
  62.                     int number;
  63.                     number = rng.Next(0, 901);
  64.                     array[i] = number;
  65.                 }
  66.  
  67.             }
  68.             return array;
  69.         }
  70.  
  71.         static public void Main()
  72.         {
  73.  
  74.             /*  
  75.             *Имя Фамилия Пике Кирилл
  76.             *Группа ВПИ197
  77.             */
  78.  
  79.             //Variable to keep the cycle running
  80.             string answer = "Output.txt";
  81.  
  82.             //Cycle to rerun the program if the user desires so
  83.             do
  84.             {
  85.  
  86.                 //Defining variables
  87.                 string path = "Output.txt";
  88.  
  89.                 int N,
  90.                     K;
  91.                 int odd = 0;
  92.  
  93.                 //Asking the user for the amount of numbers in array              
  94.                 do
  95.                 {
  96.                     Console.WriteLine("Enter the values of N and K");
  97.                     N = GetInput();
  98.                     K = GetInput();
  99.                 }
  100.                 while (N - K < 0);
  101.  
  102.  
  103.                 //Defining array of numbers
  104.                 int[] arrayOfNumbers = new int[N];
  105.  
  106.                 //Filling array with random numbers
  107.                 try
  108.                 {
  109.                     arrayOfNumbers = Filling_randoms(N, arrayOfNumbers);
  110.                 }
  111.                 catch (Exception ex)
  112.                 {
  113.                     Console.WriteLine(ex.Message);
  114.                     Console.ReadLine();
  115.                     return;
  116.                 }
  117.  
  118.                 int first_last;
  119.  
  120.                 first_last = arrayOfNumbers[0] + arrayOfNumbers[N - 1];
  121.  
  122.                 //Defining string
  123.                 string stringOfNumbers = "";
  124.  
  125.                 //Converting array into string
  126.                 for (int i = 1; i < N + 1; i++)
  127.                 {
  128.                     if (i % K == 0)
  129.                     {
  130.                         if (arrayOfNumbers[i - 1] % 2 != 0)
  131.                         {
  132.                             odd += 1;
  133.                         }
  134.                         stringOfNumbers += arrayOfNumbers[i - 1] + " ";
  135.                     }
  136.                 }
  137.  
  138.                 stringOfNumbers += Environment.NewLine + first_last;
  139.                 stringOfNumbers += Environment.NewLine + odd;
  140.  
  141.                 Console.WriteLine(stringOfNumbers);
  142.  
  143.                 //Writing the string in the file
  144.                 try
  145.                 {
  146.                     File.WriteAllText(path, stringOfNumbers);
  147.  
  148.                 }
  149.  
  150.                 catch (Exception ex)
  151.  
  152.                 {
  153.                     Console.WriteLine(ex.Message);
  154.                     Console.ReadLine();
  155.  
  156.                     return;
  157.  
  158.                 }
  159.  
  160.                 //Checking if the user wants to rerun the program
  161.                 Console.WriteLine("Do you want to repeat? [Yes/No]");
  162.                 answer = Console.ReadLine();
  163.                
  164.  
  165.             }
  166.  
  167.             while (answer == "Yes" || answer == "yes");
  168.  
  169.  
  170.  
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement