#include <iostream>
#include <array>
#include <algorithm>
#include <random>
#include <iterator>
const size_t size_matrix = 5;
int main()
{
std::random_device random;
std::mt19937 generator(random());
std::uniform_real_distribution<float> dist(1.0, 100.0);
std::array<float, size_matrix> matrix[size_matrix];
for (size_t i = 0; i < size_matrix; i++)
{
std::for_each(matrix[i].begin(), matrix[i].end(), [&generator, &dist](auto& var)
{
var = dist(generator);
});
}
for (size_t i = 0; i < size_matrix; i++)
{
std::copy(matrix[i].begin(), matrix[i].end(),
std::ostream_iterator<float>(std::cout, " "));
std::cout << std::endl;
}
std::cout << "Result: " << std::endl;
for (size_t i = 0; i < size_matrix; i++)
std::cout << matrix[i][i] << std::endl;
std::system("PAUSE >> void");
return 0;
}
<em>// PascalABC.NET 3.2, сборка 1439 от 09.05.2017</em>
<em>// Внимание! Если программа не работает, обновите версию!</em>
begin
Writeln('*** Исходный массив [10x10] ***');
var q:=MatrRandom(10,10,-30,30);
q.Println(4); Writeln(4*q.ColCount*'-');
// #1
Writeln('S=',q.Rows.SelectMany(x->x).Where(x->(x>1) and (x<18)).Sum);
// #2, #3
var s:=q.ElementsWithIndexes;
// #2
Writeln('Ненулевые элементы');
s.Where(x->x[2]<>0).Foreach(x->Write('[',x[0]+1,',',x[1]+1,'] '));
Writeln;
// #3
Write('Первый максимальный отрицательный элемент: ');
var m:=s.MaxBy(x->x[2]);
Writeln('q[',m[0]+1,',',m[1]+1,']=',m[2])
end.
<u>Пример</u>
*** Исходный массив [10x10] ***
29 29 4 -12 10 -25 0 -27 17 -17
-27 5 -11 -21 26 1 -29 -10 -12 5
18 -18 0 -11 -16 0 10 -1 -10 23
-5 -20 -14 13 14 -27 0 -26 4 -11
-20 13 0 -24 4 -4 -3 12 -13 -4
-24 27 -21 -8 -13 17 -2 -15 -26 -21
-5 -29 13 25 18 24 -12 7 -4 23
-30 27 17 29 -6 -25 12 -6 15 18
-27 28 -19 25 17 -1 -30 25 -5 28
5 -15 -30 -6 25 -21 30 6 -11 10
----------------------------------------
S=230
Ненулевые элементы
[1,1] [1,2] [1,3] [1,4] [1,5] [1,6] [1,8] [1,9] [1,10] [2,1] [2,2] [2,3] [2,4] [2,5] [2,6] [2,7] [2,8] [2,9] [2,10] [3,1] [3,2] [3,4] [3,5] [3,7] [3,8] [3,9] [3,10] [4,1] [4,2] [4,3] [4,4] [4,5] [4,6] [4,8] [4,9] [4,10] [5,1] [5,2] [5,4] [5,5] [5,6] [5,7] [5,8] [5,9] [5,10] [6,1] [6,2] [6,3] [6,4] [6,5] [6,6] [6,7] [6,8] [6,9] [6,10] [7,1] [7,2] [7,3] [7,4] [7,5] [7,6] [7,7] [7,8] [7,9] [7,10] [8,1] [8,2] [8,3] [8,4] [8,5] [8,6] [8,7] [8,8] [8,9] [8,10] [9,1] [9,2] [9,3] [9,4] [9,5] [9,6] [9,7] [9,8] [9,9] [9,10] [10,1] [10,2] [10,3] [10,4] [10,5] [10,6] [10,7] [10,8] [10,9] [10,10]
Первый максимальный отрицательный элемент: q[10,7]=30