Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace ConsoleApp1
- {
- internal class Program
- {
- /// <summary>
- /// This method asks uer to input a number and checks if its format is correct
- /// </summary>
- /// <returns>input number</returns>
- private static int GetInput()
- {
- string input;
- int result;
- Console.WriteLine("Enter a number : ");
- input = Console.ReadLine();
- while (!int.TryParse(input, out result) || result < 0)
- {
- Console.WriteLine("Wrong number. Try again: ");
- input = Console.ReadLine();
- }
- return result;
- }
- /// <summary>
- /// This method fills an array with random numbers.
- /// If there are less than 50 numbers in range 0-100 to fill we only add even numbers
- /// If there are more than 50 numbers then we add 50 even numbers in range 0-100 then
- /// the rest with any numbers in range 0-900
- /// </summary>
- /// <param name="N">amount of numbers to put in array</param>
- /// <param name="array">array to fill with randoms</param>
- /// <returns>array</returns>
- static int[] Filling_randoms(int N, int[] array)
- {
- //Defining array of numbers
- Random rng = new Random();
- //Filling array with numbers
- for (int i = 0; i < (N > 50 ? 50 : N); i++)
- {
- int number;
- number = rng.Next(1, 100);
- if (number % 2 != 0)
- {
- array[i] = number - 1;
- }
- else
- {
- array[i] = number;
- }
- }
- if (N > 50)
- {
- for (int i = 50; i < N; i++)
- {
- int number;
- number = rng.Next(0, 901);
- array[i] = number;
- }
- }
- return array;
- }
- static public void Main()
- {
- /*
- *Имя Фамилия Пике Кирилл
- *Группа ВПИ197
- */
- //Variable to keep the cycle running
- string answer = "Output.txt";
- //Cycle to rerun the program if the user desires so
- do
- {
- //Defining variables
- string path = "Output.txt";
- int N,
- K;
- int odd = 0;
- //Asking the user for the amount of numbers in array
- do
- {
- Console.WriteLine("Enter the values of N and K");
- N = GetInput();
- K = GetInput();
- }
- while (N - K < 0);
- //Defining array of numbers
- int[] arrayOfNumbers = new int[N];
- //Filling array with random numbers
- try
- {
- arrayOfNumbers = Filling_randoms(N, arrayOfNumbers);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- Console.ReadLine();
- return;
- }
- int first_last;
- first_last = arrayOfNumbers[0] + arrayOfNumbers[N - 1];
- //Defining string
- string stringOfNumbers = "";
- //Converting array into string
- for (int i = 1; i < N + 1; i++)
- {
- if (i % K == 0)
- {
- if (arrayOfNumbers[i - 1] % 2 != 0)
- {
- odd += 1;
- }
- stringOfNumbers += arrayOfNumbers[i - 1] + " ";
- }
- }
- stringOfNumbers += Environment.NewLine + first_last;
- stringOfNumbers += Environment.NewLine + odd;
- Console.WriteLine(stringOfNumbers);
- //Writing the string in the file
- try
- {
- File.WriteAllText(path, stringOfNumbers);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- Console.ReadLine();
- return;
- }
- //Checking if the user wants to rerun the program
- Console.WriteLine("Do you want to repeat? [Yes/No]");
- answer = Console.ReadLine();
- }
- while (answer == "Yes" || answer == "yes");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement