#include <iostream>
#include <clocale>
#include <array>
#include <algorithm>
#include <iterator>
#include <random>
#include <numeric>
const size_t
N = 5,
M = 4;
int main()
{
std::setlocale(LC_ALL, "Russian");
std::random_device random;
std::default_random_engine generator(random());
std::uniform_int_distribution<int> dist(-100, 50);
std::array<int, M> arr[N];
std::for_each(arr, arr+N, [&dist, &generator](auto& var)
std::for_each(var.begin(), var.end(), [&dist, &generator](auto& var)
var = dist(generator);
});
std::for_each(arr, arr+N, [](auto& var)
std::copy(var.begin(), var.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::array<int, M> sum_min{ 0 };
for (size_t j = 0u; j < M; j++)
for (size_t i = 0u; i < N; i++)
if (arr[i][j] < 0)
sum_min[j] += arr[i][j];
}
std::copy(sum_min.begin(), sum_min.end(),
std::system("PAUSE >> void");
return 0;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
#include <iostream>
#include <clocale>
#include <array>
#include <algorithm>
#include <iterator>
#include <random>
#include <numeric>
const size_t
N = 5,
M = 4;
int main()
{
std::setlocale(LC_ALL, "Russian");
std::random_device random;
std::default_random_engine generator(random());
std::uniform_int_distribution<int> dist(-100, 50);
std::array<int, M> arr[N];
std::for_each(arr, arr+N, [&dist, &generator](auto& var)
{
std::for_each(var.begin(), var.end(), [&dist, &generator](auto& var)
{
var = dist(generator);
});
});
std::for_each(arr, arr+N, [](auto& var)
{
std::copy(var.begin(), var.end(),
std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
});
std::array<int, M> sum_min{ 0 };
for (size_t j = 0u; j < M; j++)
{
for (size_t i = 0u; i < N; i++)
{
if (arr[i][j] < 0)
sum_min[j] += arr[i][j];
}
}
std::cout << std::endl;
std::copy(sum_min.begin(), sum_min.end(),
std::ostream_iterator<int>(std::cout, " "));
std::system("PAUSE >> void");
return 0;
}