Advertisement
maxrusmos

Untitled

Mar 6th, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. using System;
  2.  
  3. namespace GeometricAverage {
  4. public class GeometricAverage {
  5.  
  6. private const double Eps = 1e-14;
  7.  
  8. public static double GeomAverage(string[] numStr) {
  9. if (numStr.Length == 0) {
  10. Console.ForegroundColor = ConsoleColor.Red;
  11. Console.WriteLine("Вы ничего не ввели.");
  12. System.Environment.Exit(1);
  13. }
  14.  
  15. double[] Arr = new double[numStr.Length];
  16.  
  17. for (int i = 0; i < numStr.Length; i++) {
  18. if (!double.TryParse(numStr[i], out double number)) {
  19. Console.ForegroundColor = ConsoleColor.Red;
  20. Console.WriteLine("Какой-либо из аргументов не является числом");
  21. System.Environment.Exit(2);
  22. }
  23. }
  24.  
  25. for (int i = 0; i < numStr.Length; i++) {
  26. Arr[i] = Convert.ToDouble(numStr[i]);
  27. }
  28.  
  29. var composition = 1.0;
  30. for (int i = 0; i < Arr.Length; i++) {
  31. composition *= Arr[i];
  32. }
  33.  
  34. if (composition < Eps) {
  35. Console.ForegroundColor = ConsoleColor.Red;
  36. Console.WriteLine("Геометрическое среднее может быть найдено тогда, и только тогда, если выборка состоит из положительных чисел.");
  37. System.Environment.Exit(3);
  38. }
  39.  
  40. return Math.Pow(composition, 1.0 / Arr.Length);
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement