# Beispiellösung

#### Content

#include <iomanip>
#include <iostream>
#include "matrix.hpp"

template<typename T>
void print_matrix(const Matrix<T>& A) {
for (std::size_t i = 0; i < A.m; ++i) {
for (std::size_t j = 0; j < A.n; ++j) {
std::cout << " " << std::setw(8) << A(i, j);
}
std::cout << std::endl;
}
}

template<typename T, typename Initializer>
void init_matrix(Matrix<T>& A, Initializer&& initializer) {
for (std::size_t i = 0; i < A.m; ++i) {
for (std::size_t j = 0; j < A.n; ++j) {
A(i, j) = initializer(i, j);
}
}
}

template<typename T>
T sum_matrix(const Matrix<T>& A) {
T sum{};
for (std::size_t i = 0; i < A.m; ++i) {
for (std::size_t j = 0; j < A.n; ++j) {
sum += A(i, j);
}
}
return sum;
}

int main() {
Matrix<double> A(7, 8, StorageOrder::ColMajor);
init_matrix(A, [&A](std::size_t i, std::size_t j) {
return i * A.n + j;
});
std::cout << "A:" << std::endl; print_matrix(A);
std::cout << std::endl << "sum(A) = " << sum_matrix(A) << std::endl;
}

theon$g++ -Wall -o matrix-sum matrix-sum.cpp theon$ ./matrix-sum
A:
0        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       31
32       33       34       35       36       37       38       39
40       41       42       43       44       45       46       47
48       49       50       51       52       53       54       55

sum(A) = 1540
theon\$