Matrica
Napisati klasu Matrix koja predstavlja matricu fiksnih dimenzija ROWS x COLS i tipa elemenata T. Implementirati:
template <typename T, size_t ROWS, size_t COLS>
class Matrix {
public:
Matrix();
T& at(size_t row, size_t col);
const T& at(size_t row, size_t col) const;
size_t rows() const;
size_t cols() const;
};
template <typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator+(const Matrix<T, ROWS, COLS> &a, const Matrix<T, ROWS, COLS> &b);
template <typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator*(const T &scalar, const Matrix<T, ROWS, COLS> &a);Test
#include <iostream>
namespace matf {
template <typename T, size_t ROWS, size_t COLS>
class Matrix {
// Ovde ide vasa implementacija
};
// Ovde ide vasa implementacija
// Ovde ide vasa implementacija
} // namespace matf
int main(void)
{
matf::Matrix<int, 3, 4> mat1;
mat1.at(1, 2) = 42;
mat1.at(0, 0) = 7;
mat1.at(2, 3) = 0;
mat1.at(0, 1) = 0;
for (size_t i = 0; i < mat1.rows(); ++i) {
for (size_t j = 0; j < mat1.cols(); ++j) {
std::cout << mat1.at(i, j) << " ";
}
std::cout << std::endl;
}
// Output:
// 7 0 0 0
// 0 0 42 0
// 0 0 0 0
std::cout << "------------------" << std::endl;
matf::Matrix<int, 3, 4> mat2;
mat2.at(0, 0) = 1;
mat2.at(1, 1) = 2;
mat2.at(2, 2) = 3;
mat2.at(0, 3) = 4;
for (size_t i = 0; i < mat2.rows(); ++i) {
for (size_t j = 0; j < mat2.cols(); ++j) {
std::cout << mat2.at(i, j) << " ";
}
std::cout << std::endl;
}
// Output:
// 1 0 0 4
// 0 2 0 0
// 0 0 3 0
std::cout << "------------------" << std::endl;
matf::Matrix<int, 3, 4> mat_sum = mat1 + mat2;
for (size_t i = 0; i < mat_sum.rows(); ++i) {
for (size_t j = 0; j < mat_sum.cols(); ++j) {
std::cout << mat_sum.at(i, j) << " ";
}
std::cout << std::endl;
}
// Output:
// 8 0 0 4
// 0 2 42 0
// 0 0 3 0
std::cout << "------------------" << std::endl;
matf::Matrix<int, 3, 4> mat_scaled = -1 * mat_sum;
for (size_t i = 0; i < mat_scaled.rows(); ++i) {
for (size_t j = 0; j < mat_scaled.cols(); ++j) {
std::cout << mat_scaled.at(i, j) << " ";
}
std::cout << std::endl;
}
// Output:
// -8 0 0 -4
// 0 -2 -42 0
// 0 0 -3 0
return 0;
}Rešenje
main.cpp
#include <iostream>
namespace matf {
template <typename T, size_t ROWS, size_t COLS>
class Matrix {
public:
Matrix()
{
for (size_t i = 0; i < ROWS; ++i) {
for (size_t j = 0; j < COLS; ++j) {
data[i * COLS + j] = T();
}
}
}
T& at(size_t row, size_t col)
{
return data[row * COLS + col];
}
const T& at(size_t row, size_t col) const
{
return data[row * COLS + col];
}
size_t rows() const
{
return ROWS;
}
size_t cols() const
{
return COLS;
}
private:
T data[ROWS * COLS];
};
template <typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator+(const Matrix<T, ROWS, COLS> &a, const Matrix<T, ROWS, COLS> &b)
{
Matrix<T, ROWS, COLS> result;
for (size_t i = 0; i < ROWS; ++i) {
for (size_t j = 0; j < COLS; ++j) {
result.at(i, j) = a.at(i, j) + b.at(i, j);
}
}
return result;
}
template <typename T, size_t ROWS, size_t COLS>
Matrix<T, ROWS, COLS> operator*(const T &scalar, const Matrix<T, ROWS, COLS> &a)
{
Matrix<T, ROWS, COLS> result;
for (size_t i = 0; i < ROWS; ++i) {
for (size_t j = 0; j < COLS; ++j) {
result.at(i, j) = scalar * a.at(i, j);
}
}
return result;
}
} // namespace matf
int main(void)
{
matf::Matrix<int, 3, 4> mat1;
mat1.at(1, 2) = 42;
mat1.at(0, 0) = 7;
mat1.at(2, 3) = 0;
mat1.at(0, 1) = 0;
for (size_t i = 0; i < mat1.rows(); ++i) {
for (size_t j = 0; j < mat1.cols(); ++j) {
std::cout << mat1.at(i, j) << " ";
}
std::cout << std::endl;
}
// Output:
// 7 0 0 0
// 0 0 42 0
// 0 0 0 0
std::cout << "------------------" << std::endl;
matf::Matrix<int, 3, 4> mat2;
mat2.at(0, 0) = 1;
mat2.at(1, 1) = 2;
mat2.at(2, 2) = 3;
mat2.at(0, 3) = 4;
for (size_t i = 0; i < mat2.rows(); ++i) {
for (size_t j = 0; j < mat2.cols(); ++j) {
std::cout << mat2.at(i, j) << " ";
}
std::cout << std::endl;
}
// Output:
// 1 0 0 4
// 0 2 0 0
// 0 0 3 0
std::cout << "------------------" << std::endl;
matf::Matrix<int, 3, 4> mat_sum = mat1 + mat2;
for (size_t i = 0; i < mat_sum.rows(); ++i) {
for (size_t j = 0; j < mat_sum.cols(); ++j) {
std::cout << mat_sum.at(i, j) << " ";
}
std::cout << std::endl;
}
// Output:
// 8 0 0 4
// 0 2 42 0
// 0 0 3 0
std::cout << "------------------" << std::endl;
matf::Matrix<int, 3, 4> mat_scaled = -1 * mat_sum;
for (size_t i = 0; i < mat_scaled.rows(); ++i) {
for (size_t j = 0; j < mat_scaled.cols(); ++j) {
std::cout << mat_scaled.at(i, j) << " ";
}
std::cout << std::endl;
}
// Output:
// -8 0 0 -4
// 0 -2 -42 0
// 0 0 -3 0
return 0;
}