Задача Найти произведение двух матриц.
Произведение двух матриц
{\displaystyle A={\begin{bmatrix}a_{11} &a_{12} & \cdots &a_{1m}\\a_{21} &a_{22} & \cdots &a_{2m}\\ \vdots & \vdots & \ddots & \vdots \\a_{l1} &a_{l2} & \cdots &a_{lm}\end{bmatrix}}}
{\displaystyle B={\begin{bmatrix}b_{11} &b_{12} & \cdots &b_{1k}\\b_{21} &b_{22} & \cdots &b_{2k}\\ \vdots & \vdots & \ddots & \vdots \\b_{m1} &b_{m2} & \cdots &b_{mk}\end{bmatrix}}}
можно записать как
C\ = \ A\ \cdot \ B
{\displaystyle C={\begin{bmatrix}c_{11} &c_{12} & \cdots &c_{1k}\\c_{21} &c_{22} & \cdots &c_{2k}\\ \vdots & \vdots & \ddots & \vdots \\c_{l1} &c_{l2} & \cdots &c_{lk}\end{bmatrix}}}
{\displaystyle c_{ij}=\sum _{p=1}^{m}a_{ip}b_{pj}\;\;\;\left(i=1,2,\ldots l;\;j=1,2,\ldots k\right).}
Произведение двух матриц можно найти только в случае, если количество столбцов левой матрицы совпадает с количеством строк правой.
Реализация на C++
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
using namespace std;
int main()
{
int row1, row2, col1, col2;
double** a, ** b, ** c;
system("chcp 1251");
system("cls");
cout << "Введите количество строк первой матрицы: ";
cin >> row1;
cout << "Введите количество столбцов первой матрицы: ";
cin >> col1;
cout << "Введите количество строк второй матрицы: ";
cin >> row2;
cout << "Введите количество столбцов второй матрицы: ";
cin >> col2;
if (col1 != row2)
{
cout << "Умножение невозможно!";
cin.get(); cin.get();
return 0;
}
// Ввод элементов первой матрицы
a = new double* [row1];
cout << "Введите элементы первой матрицы" << endl;
for (int i = 0; i < row1; i++)
{
a[i] = new double[col1];
for (int j = 0; j < col1; j++)
{
cout << "a[" << i << "][" << j << "]= ";
cin >> a[i][j];
}
}
// Вывод элементов первой матрицы
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col1; j++)
cout << a[i][j] << " ";
cout << endl;
}
// Ввод элементов второй матрицы
b = new double* [row2];
cout << "Введите элементы второй матрицы" << endl;
for (int i = 0; i < row2; i++)
{
b[i] = new double[col2];
for (int j = 0; j < col2; j++)
{
cout << "b[" << i << "][" << j << "]= ";
cin >> b[i][j];
}
}
// Вывод элементов второй матрицы
for (int i = 0; i < row2; i++)
{
for (int j = 0; j < col2; j++)
{
cout << b[i][j] << " ";
}
cout << endl;
}
// Умножение матриц
c = new double* [row1];
for (int i = 0; i < row1; i++)
{
c[i] = new double[col2];
for (int j = 0; j < col2; j++)
{
c[i][j] = 0;
for (int k = 0; k < col1; k++)
c[i][j] += a[i][k] * b[k][j];
}
}
// Вывод матрицы произведения
cout << "Матрица произведения" << endl;
for (int i = 0; i < row1; i++)
{
for (int j = 0; j < col2; j++)
cout << c[i][j] << " ";
cout << endl;
}
// Освобождение памяти
for (int i = 0; i < row1; i++)
{
delete [] a[i];
delete [] c[i];
}
for (int i = 0; i < row2; i++)
delete [] b[i];
delete [] a;
delete [] b;
delete [] c;
cin.get(); cin.get();
return 0;
}