Files
cpp-course/modules/04_arrays_and_strings/src/multidimensional.cpp
T

33 lines
840 B
C++
Raw Normal View History

2026-08-11 17:40:36 +03:00
#include "multidimensional.hpp"
2026-08-12 21:55:27 +03:00
#include <algorithm>
#include <numeric>
2026-08-11 17:40:36 +03:00
namespace arrays {
2026-08-12 21:55:27 +03:00
auto create_matrix(const int rows, const int cols, const int fill) -> Matrix {
return Matrix(rows, std::vector(cols, fill));
}
auto transpose(const Matrix& matrix) -> Matrix {
auto new_mat = Matrix(matrix[0].size(), std::vector(matrix.size(), 0));
for (size_t row = 0; row < matrix.size(); ++row) {
for (size_t col = 0; col < matrix[row].size(); ++col) {
new_mat[col][row] = matrix[row][col];
}
}
return new_mat;
}
auto row_sums(const Matrix& matrix) -> std::vector<int> {
auto sums = std::vector(matrix.size(), 0);
auto idx = 0;
for (auto row: matrix) {
sums[idx] = std::ranges::fold_left(row, 0, std::plus{});
idx++;
}
return sums;
}
2026-08-11 17:40:36 +03:00
} // namespace arrays