Загрузка данных


#ifndef MATMOD_H
#define MATMOD_H

const int SZ = 5;

void mat_add(const int A[][SZ], const int B[][SZ], int C[][SZ]);
void mat_mul(const int A[][SZ], const int B[][SZ], int C[][SZ]);
int mat_det(const int A[][SZ]);

#endif


-

#include "matmod.h"

void mat_add(const int A[][SZ], const int B[][SZ], int C[][SZ])
{
    for (int i = 0; i < SZ; i++)
        for (int j = 0; j < SZ; j++)
            C[i][j] = A[i][j] + B[i][j];
}

void mat_mul(const int A[][SZ], const int B[][SZ], int C[][SZ])
{
    for (int i = 0; i < SZ; i++)
    {
        for (int j = 0; j < SZ; j++)
        {
            C[i][j] = 0;

            for (int k = 0; k < SZ; k++)
                C[i][j] = C[i][j] + A[i][k] * B[k][j];
        }
    }
}

int det_rec(const int A[][SZ], int n)
{
    if (n == 1)
        return A[0][0];

    int result = 0;

    for (int col = 0; col < n; col++)
    {
        int M[SZ][SZ];
        int r = 0;

        for (int i = 1; i < n; i++)
        {
            int c = 0;

            for (int j = 0; j < n; j++)
            {
                if (j != col)
                {
                    M[r][c] = A[i][j];
                    c++;
                }
            }

            r++;
        }

        if (col % 2 == 0)
            result = result + A[0][col] * det_rec(M, n - 1);
        else
            result = result - A[0][col] * det_rec(M, n - 1);
    }

    return result;
}

int mat_det(const int A[][SZ])
{
    return det_rec(A, SZ);
}

---

#include <iostream>
#include "matmod.h"

using namespace std;

int main()
{
    int A[SZ][SZ];
    int B[SZ][SZ];
    int C[SZ][SZ];

    cout << "Matrix A:" << endl;

    for (int i = 0; i < SZ; i++)
        for (int j = 0; j < SZ; j++)
            cin >> A[i][j];

    cout << "Matrix B:" << endl;

    for (int i = 0; i < SZ; i++)
        for (int j = 0; j < SZ; j++)
            cin >> B[i][j];

    mat_add(A, B, C);

    cout << endl << "A + B:" << endl;

    for (int i = 0; i < SZ; i++)
    {
        for (int j = 0; j < SZ; j++)
            cout << C[i][j] << " ";

        cout << endl;
    }

    mat_mul(A, B, C);

    cout << endl << "A * B:" << endl;

    for (int i = 0; i < SZ; i++)
    {
        for (int j = 0; j < SZ; j++)
            cout << C[i][j] << " ";

        cout << endl;
    }

    cout << endl << "det(A) = " << mat_det(A) << endl;

    return 0;
}