-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask62
More file actions
42 lines (37 loc) · 1.1 KB
/
Task62
File metadata and controls
42 lines (37 loc) · 1.1 KB
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
// Задача 62. Напишите программу, которая заполнит спирально массив 4 на 4.
// Например, на выходе получается вот такой массив:
// 01 02 03 04
// 12 13 14 05
// 11 16 15 06
// 10 09 08 07
Console.Write("Введите количество строк массива: ");
int size = int.Parse(Console.ReadLine());
int[,] spiralArray = new int[size, size];
int temp = 1;
int i = 0;
int j = 0;
while (temp <= spiralArray.GetLength(0) * spiralArray.GetLength(1))
{
spiralArray[i, j] = temp;
temp++;
if (i <= j + 1 && i + j < spiralArray.GetLength(1) - 1)
j++;
else if (i < j && i + j >= spiralArray.GetLength(0) - 1)
i++;
else if (i >= j && i + j > spiralArray.GetLength(1) - 1)
j--;
else
i--;
}
PrintArray(spiralArray);
void PrintArray(int[,] inArray)
{
for (int i = 0; i < inArray.GetLength(0); i++)
{
for (int j = 0; j < inArray.GetLength(1); j++)
{
Console.Write($"{inArray[i, j]} ");
}
Console.WriteLine();
}
}