fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iostream>
  4. #include <random>
  5.  
  6. int main()
  7. {
  8. const size_t h = 5, w = 8;
  9. std::random_device rand;
  10. std::mt19937 gen(rand());
  11. std::uniform_int_distribution <> distr(-30, 30);
  12. int matrix[h][w];
  13. std::cout << "matrix: " << std::endl;
  14. for(auto &row : matrix)
  15. {
  16. for(auto &val : row)
  17. {
  18. val = distr(gen);
  19. std::cout.width(4);
  20. std::cout << val;
  21. }
  22. std::cout << std::endl;
  23. }
  24. typeof(**matrix) sum = 0;
  25. std::cout << "Elements of third column: ";
  26. for(size_t idx = 0; idx < h; idx++)
  27. {
  28. sum += matrix[idx][2];
  29. std::cout << matrix[idx][2] << ' ';
  30. }
  31. std::cout << std::endl;
  32. auto max = std::max_element(matrix[1], matrix[1] + w);
  33. std::cout << "Max item in line 2: " << *max
  34. << ", index : " << std::distance(matrix[1], max) << std::endl;
  35. std::cout << "Sum of third column: " << sum << std::endl;
  36. return 0;
  37. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
matrix: 
  19 -11   5  10  16 -22   6 -16
 -26 -18  10   5  23 -10 -13 -19
 -13 -25  18  10 -27  19   7 -30
  22   4 -24  29  -4  22  26 -28
  22 -30 -25  30   8  -5   1 -15
Elements of third column: 5 10 18 -24 -25 
Max item in line 2: 23, index : 4
Sum of third column: -16