Задан массив {xi}; x=1,n. Построить новый массив по правилу: - C#
Формулировка задачи:
Укажите ошибки.
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
int[] myArr = new int[4];
int x1 = 1, x2 = 8, x3 = 9 , x4 = 10;
int y;
int i;
int m;
myArr[1] = 1;
myArr[2] = 8;
myArr[3] = 9;
myArr[4] = 10;
foreach (int p in myArr)
Console.WriteLine(p);
Console.ReadLine();
y = 1 / x1 + i (x1 ^ 2 + x2 ^ 2 + x3 ^ 2 + x4 ^ 2);
Console.WriteLine("Введи мне n, ссыкло!");
int n = int.Parse(Console.ReadLine());
int i = 1;
for (int j=1; j<=n; j++)
{
i = i * j;
}
Console.WriteLine("Пидор-факториал:"+ i);
}
}
}Решение задачи: «Задан массив {xi}; x=1,n. Построить новый массив по правилу:»
textual
Листинг программы
class Program
{
static double Factors(int i)
{
if ((i == 0) || (i == 1))
return 1;
else
return i * Factors(i - 1);
}
static int GetSum(int[] inputX)
{
return inputX.Select(n=> n*n).Sum();
}
static double[] GetY(int[] inputX)
{
double[] result = new double[inputX.Length];
//Сумма всех Х одна - и вычислить ее достаточно 1 раз
int sumX = GetSum(inputX);
for(int i=0;i<inputX.Length;i++)
{
//по поводу i+1 - согласно заданию индексация происходит с 1, а это значит
// что первое значение I равно 1
result[i] = (1.0F / (inputX[i] + (i + 1.0f))) * (sumX + Factors(i + 1));
}
return result;
}
static void Main(string[] args)
{
Console.WriteLine("Enter N:");
int n = int.Parse(Console.ReadLine());
int[] x = new int[n];
for(int i=0;i<n;i++)
{
x[i] = i + 1;
}
double[] y = GetY(x);
foreach(var item in y)
{
Console.WriteLine(item);
}
Console.ReadKey();
}