-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask02.cs
More file actions
47 lines (38 loc) · 1018 Bytes
/
Copy pathtask02.cs
File metadata and controls
47 lines (38 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Задайте одномерный массив, заполненный случайными числами.
// Найдите сумму элементов, стоящих на нечётных позициях.
// [3, 7, 23, 12] -> 19
// [-4, -6, 89, 6] -> 0
int n = 6;
int[] Array(int n)
{
int[] arr = new int[n];
for (int i = 0; i < arr.Length; i++)
{
arr[i] = new Random().Next(-10,100);
}
return arr;
}
int[] SumUnevenPos(int[] arr)
{
int[] sum = new int[1];
for (int j = 0; j < arr.Length; j++)
{
if (j % 2 != 0)
sum[0] += arr[j];
}
return sum;
}
void PrintArray(int[] arr)
{
Console.WriteLine();
for (int k = 0; k < arr.Length; k++)
{
Console.Write($"{arr[k]} ");
}
Console.WriteLine();
}
Console.Write($"Massive: ");
int[] array = Array(n);
PrintArray(array);
Console.Write($"Sum of elements in uneven positions: ");
PrintArray(SumUnevenPos(array));