Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- namespace Array_File
- {
- class Program
- {
- const string ERROR_MSG = "wrong";
- static int InputIntN(string errorMsg)
- {
- Console.WriteLine("Enter array length: ");
- string s = Console.ReadLine();
- int N;
- if (!int.TryParse(s, out N) || N < 1 || N > 100)
- {
- Console.WriteLine(errorMsg);
- return InputIntN(ERROR_MSG);
- }
- return N;
- }
- static void InputElements(string errorMsg, int N, int[] numbers, ref double count)
- {
- string[] arr = new string[N];
- for (int i = 0; i < N; i++)
- {
- Console.WriteLine("Enter array elements: ");
- arr[i] = Console.ReadLine();
- while (!int.TryParse(arr[i], out numbers[i]))
- {
- Console.WriteLine(errorMsg);
- Console.WriteLine("Enter array elements: ");
- arr[i] = Console.ReadLine();
- }
- if (numbers[i] < 0)
- {
- numbers[i] = 0;
- }
- count += numbers[i];
- }
- }
- static void Main()
- {
- do
- {
- int N = InputIntN(ERROR_MSG);
- int[] numbers = new int[N];
- double count = 0;
- InputElements(ERROR_MSG, N, numbers, ref count);
- string str = String.Join(" ", numbers);
- string file_name = "output.txt";
- try
- {
- File.Delete(file_name);
- File.AppendAllText(file_name, str);
- File.AppendAllText(file_name, "\n" + (count / N).ToString("f3"));
- }
- catch (IOException ex)
- {
- Console.WriteLine(ex.Message);
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- Console.WriteLine("To exit from program press ESCAPE. To restart this program press any key.....");
- } while (Console.ReadKey(true).Key != ConsoleKey.Escape); // Повтор решения.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement