#include <iostream>
#include <cmath>
using namespace std;

class MatrixMN {
private:
    int m, n;
    double** a;

public:
    // конструктор
    MatrixMN(int rows = 1, int cols = 1) {
        m = rows;
        n = cols;

        a = new double*[m];
        for (int i = 0; i < m; i++)
            a[i] = new double[n]{0};
    }

    // деструктор
    ~MatrixMN() {
        for (int i = 0; i < m; i++)
            delete[] a[i];
        delete[] a;
    }

    // доступ до елементів
    double& at(int i, int j) {
        return a[i][j];
    }

    // перевірка нульової матриці
    bool isZero() {
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                if (a[i][j] != 0)
                    return false;
        return true;
    }

    // норма Фробеніуса
    double getNorm() {
        double s = 0;
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                s += a[i][j] * a[i][j];

        return sqrt(s);
    }

    // додавання
    MatrixMN add(const MatrixMN& b) {
        MatrixMN r(m, n);

        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                r.a[i][j] = a[i][j] + b.a[i][j];

        return r;
    }

    // +
    MatrixMN operator+(const MatrixMN& b) {
        return add(b);
    }

    // -
    MatrixMN operator-(const MatrixMN& b) {
        MatrixMN r(m, n);

        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                r.a[i][j] = a[i][j] - b.a[i][j];

        return r;
    }

    // *
    MatrixMN operator*(double x) {
        MatrixMN r(m, n);

        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                r.a[i][j] = a[i][j] * x;

        return r;
    }

    // вивід
    friend ostream& operator<<(ostream& os, const MatrixMN& mtx) {
        for (int i = 0; i < mtx.m; i++) {
            for (int j = 0; j < mtx.n; j++)
                os << mtx.a[i][j] << " ";
            os << endl;
        }
        return os;
    }
};

// ================= MAIN =================
int main() {
    MatrixMN A(2, 2);
    MatrixMN B(2, 2);

    // заповнення A
    A.at(0,0) = 1;
    A.at(0,1) = 2;
    A.at(1,0) = 3;
    A.at(1,1) = 4;

    // заповнення B
    B.at(0,0) = 5;
    B.at(0,1) = 6;
    B.at(1,0) = 7;
    B.at(1,1) = 8;

    cout << "Matrix A:\n" << A;
    cout << "Matrix B:\n" << B;

    cout << "A + B:\n" << (A + B);
    cout << "A - B:\n" << (A - B);
    cout << "A * 2:\n" << (A * 2);

    cout << "Norm of A: " << A.getNorm() << endl;

    if (A.isZero())
        cout << "A is zero matrix\n";
    else
        cout << "A is NOT zero matrix\n";

    return 0;
}
