Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace Array_File
  5. {
  6.     class Program
  7.     {
  8.         const string ERROR_MSG = "wrong";
  9.  
  10.         static int InputIntN(string errorMsg)
  11.         {
  12.             Console.WriteLine("Enter array length: ");
  13.             string s = Console.ReadLine();
  14.             int N;
  15.             if (!int.TryParse(s, out N) || N < 1 || N > 100)
  16.             {
  17.                 Console.WriteLine(errorMsg);
  18.                 return InputIntN(ERROR_MSG);
  19.             }
  20.  
  21.             return N;
  22.         }
  23.  
  24.  
  25.         static void InputElements(string errorMsg, int N, int[] numbers, ref double count)
  26.         {
  27.             string[] arr = new string[N];
  28.  
  29.             for (int i = 0; i < N; i++)
  30.             {
  31.                 Console.WriteLine("Enter array elements: ");
  32.                 arr[i] = Console.ReadLine();
  33.  
  34.                 while (!int.TryParse(arr[i], out numbers[i]))
  35.                 {
  36.                     Console.WriteLine(errorMsg);
  37.                     Console.WriteLine("Enter array elements: ");
  38.                     arr[i] = Console.ReadLine();
  39.                 }
  40.  
  41.                 if (numbers[i] < 0)
  42.                 {
  43.                     numbers[i] = 0;
  44.                 }
  45.  
  46.                 count += numbers[i];
  47.             }
  48.         }
  49.  
  50.  
  51.         static void Main()
  52.         {
  53.             do
  54.             {
  55.                 int N = InputIntN(ERROR_MSG);
  56.                 int[] numbers = new int[N];
  57.                 double count = 0;
  58.  
  59.                 InputElements(ERROR_MSG, N, numbers, ref count);
  60.  
  61.                 string str = String.Join(" ", numbers);
  62.  
  63.                 string file_name = "output.txt";
  64.  
  65.                 try
  66.                 {
  67.                     File.Delete(file_name);
  68.                     File.AppendAllText(file_name, str);
  69.                     File.AppendAllText(file_name, "\n" + (count / N).ToString("f3"));
  70.                 }
  71.  
  72.                 catch (IOException ex)
  73.                 {
  74.                     Console.WriteLine(ex.Message);
  75.                 }
  76.                 catch (Exception e)
  77.                 {
  78.                     Console.WriteLine(e.Message);
  79.                 }
  80.                 Console.WriteLine("To exit from program press ESCAPE. To restart this program press any key.....");
  81.             } while (Console.ReadKey(true).Key != ConsoleKey.Escape); //  Повтор решения.
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement