-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask56
More file actions
74 lines (63 loc) · 2.25 KB
/
Task56
File metadata and controls
74 lines (63 loc) · 2.25 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Задача 56: Задайте прямоугольный двумерный массив.
// Напишите программу, которая будет находить строку с наименьшей суммой элементов.
// Например, задан массив:
// 1 4 7 2
// 5 9 2 3
// 8 4 2 4
// 5 2 6 7
// Программа считает сумму элементов в каждой строке и выдаёт номер строки с наименьшей суммой элементов: 1 строка
Console.Write("Введите количество строк массива: ");
int rows = int.Parse(Console.ReadLine());
Console.Write("Введите количество столбцов массива: ");
int columns = int.Parse(Console.ReadLine());
// Console.Write("Введите минимальное число массива: ");
// int minValue = int.Parse(Console.ReadLine());
// Console.Write("Введите максимальное число массива: ");
// int maxValue = int.Parse(Console.ReadLine());
int[,] array = GetArray(rows, columns, 0, 10); // minValue, maxValue
int minSumLine = 0;
int sumLine = SumLineElements(array, 0);
PrintArray(array);
Console.WriteLine();
for (int i = 1; i < array.GetLength(0); i++)
{
int tempSumLine = SumLineElements(array, i);
if (sumLine > tempSumLine)
{
sumLine = tempSumLine;
minSumLine = i;
}
}
Console.WriteLine($"В строке {minSumLine + 1} наименьшая сумма элементов: {sumLine}");
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();
}
}
int[,] GetArray(int m, int n, int minValue, int maxValue)
{
int[,] result = new int[m, n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
result[i, j] = new Random().Next(minValue, maxValue + 1);
}
}
return result;
}
int SumLineElements(int[,] array, int i)
{
int sumLine = array[i, 0];
for (int j = 1; j < array.GetLength(1); j++)
{
sumLine += array[i, j];
}
return sumLine;
}