static void Main(string[] args) { Number[] numArr = new Number[5];//Выбираем 5 рандомных чисел в диапазоне от [1;10] for (int i = 0; i < numArr.Length; i++) { numArr[i] = Random; Console.Write(" " + numArr[i].Invoke() + " "); // Метод Invoke принимает делегат //и выполняет его в том потоке, в котором был создан элемент управления } Console.WriteLine(); MediumCalc mediumCalc = delegate(Number[] arrayX) { int sum = 0;//иницыализация суммы for (int i = 0; i < arrayX.Length; i++) { sum += arrayX[i](); } // Перебираем все элементы // и складываем их в сумму return((sum) / (arrayX.Length)); // Среднее арифметическое }; Console.WriteLine(mediumCalc(numArr)); Console.ReadKey(); }
static void Main(string[] args) { Number[] numArr = new Number[5]; // for (int i = 0; i < numArr.Length; i++) { numArr[i] = Random; Console.Write("" + numArr[i].Invoke() + ""); // } Console.WriteLine(); MediumCalc mediumCalc = delegate(Number[] arrayX) { int sum = 0; // for (int i = 0; i < arrayX.Length; i++) { sum += arrayX[i](); // } return((sum) / (arrayX.Length)); // }; Console.WriteLine(mediumCalc(numArr)); Console.ReadLine(); }
static void Main(string[] args) { #region --Arithmetic mean /* * arithmeticMean _arithmeticMean = delegate(int a, int b, int c) * { * return (a + b + c ) / 3; * }; * _arithmeticMean(3, 5, 5); */ #endregion #region -- Task 2 Lambdas operators /* * Console.WriteLine("Choose Input operator, such as --> +,-,*,/"); * string a = Console.ReadLine(); * Console.WriteLine("Input firs value -->"); * int x = Convert.ToInt32(Console.ReadLine()); * Console.WriteLine("Input second value -->"); * int y = Convert.ToInt32(Console.ReadLine()); * switch (a) * { * case "+": arithmeticOperators _arithmeticOperatorsAdd = (a, b) => a + b; * int Add = _arithmeticOperatorsAdd(x, y); * Console.WriteLine("Answer is:{0}",Add); * break; * case "-": arithmeticOperators _arithmeticOperatorsSub = (a, b) => a - b; * int Sub = _arithmeticOperatorsSub(x, y); * Console.WriteLine("Answer is:{0}", Sub); * break; * case "*": arithmeticOperators _arithmeticOperatorsMul = (a, b) => a * b; * int Mul = _arithmeticOperatorsMul(x, y); * Console.WriteLine("Answer is:{0}", Mul); * break; * case "/": arithmeticOperators _arithmeticOperatorsDiv = (a, b) => b == 0 ? 0 : a / b; * int Div = _arithmeticOperatorsDiv(x, y); * Console.WriteLine("Answer is:{0}", Div); * break; * default: Console.WriteLine("Choose from the proposed:"); * break; * } * */ #endregion #region Task 3 ----------------------------- Number[] Arr = new Number[5]; for (int i = 0; i < Arr.Length; i++) { Arr[i] = Randomizer; Console.Write(" " + Arr[i] + " "); } Console.WriteLine(); MediumCalc mediumCalc = delegate(Number[] arrayX) { int sum = 0; for (int i = 0; i < arrayX.Length; i++) { sum += arrayX[i](); } return((sum) / (arrayX.Length)); }; #endregion Console.Read(); }