include iostream include functional template std size_t void foreach d

 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
#include <iostream>
#include <functional>
template<std::size_t S> void foreach(double (&array)[S], std::function<void(double &number)> function) {
for(auto &element: array) function(element);
}
template<std::size_t S, std::size_t T> void foreach(double (&array)[S][T], std::function<void(double &number)> function) {
for(auto &elements: array) for(auto &element: elements) function(element);
}
int main() {
double max1, max2;
double array1[3], array2[2][4];
std::cout << "input array[10]" << std::endl;
foreach(array1, [&] (double &number) { std::cin >> number; });
std::cout << "input array2[4][4]" << std::endl;
foreach(array2, [&] (double &number) { std::cin >> number; });
foreach(array1, [&] (double &number) { if (abs(number) > max1) max1 = abs(number); });
foreach(array2, [&] (double &number) { if (abs(number) > max2) max2 = abs(number); });
std::cout
<< "biggest value in array[10] => " << max1 << std::endl
<< "biggest value in array[4][4] => " << max2 << std::endl;
return 0;
}