C/C++

Задание по программированию c++, буду благодарен помощи

Вычислите матрицы S(N, М) и R(N, М), являющиеся суммой и разностью целочисленных матриц А(N, М) и В(N, М). То есть надо построить матрицу c помощью цикла for и массива
Вот пример:

#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
SetConsoleOutputCP(1251);
int s, r, k, b, a[10][10];
cin >> k;
cin >> b;
for (s = 0; s < k; s++)
{
for (r = 0; r < b; r++)
{
cin >> a[s][b];
}
}
for (s = 0; s < k; s++) {
for (r = 0; r < b; r++)
{
cout << a[s][b] <<"|";
}
}
cout << endl;
system("pause");
return 0;
}
(но это просто код который выводит числа в виде матрицы)
#include <iostream>
#include <iomanip>
#include <random>
using namespace std;
size_t length(const char* msg) {
auto value = 0U;
while (!value) {
cout << msg;
cin >> value;
cin.ignore(cin.rdbuf()->in_avail());
}
return value;
}
int** get_matrix(const size_t n, const size_t m) {
auto matrix = new int* [n];
for (auto i = 0U; i < n; ++i) matrix[i] = new int[m];
return matrix;
}
void random_fill(int** matrix, const size_t n, const size_t m) {
uniform_int_distribution<> uid(10, 99);
mt19937 gen{ random_device()() };
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < m; ++j) {
matrix[i][j] = uid(gen);
}
}
}
void show(int** matrix, const size_t n, const size_t m) {
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < m; ++j) cout << setw(5) << matrix[i][j];
puts("");
}
puts("");
}
int** sum_matrix(int** a, int** b, const size_t n, const size_t m) {
auto s = get_matrix(n, m);
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < m; ++j) {
s[i][j] = a[i][j] + b[i][j];
}
}
return s;
}
int** sub_matrix(int** a, int** b, const size_t n, const size_t m) {
auto s = get_matrix(n, m);
for (auto i = 0U; i < n; ++i) {
for (auto j = 0U; j < m; ++j) {
s[i][j] = a[i][j] - b[i][j];
}
}
return s;
}
int** destroy(int** matrix, const size_t n) {
if (matrix != nullptr) {
for (auto i = 0U; i < n; ++i) delete[] matrix[i];
delete[] matrix;
matrix = nullptr;
}
return matrix;
}
int main() {
auto n = length("N: ");
auto m = length("M: ");
puts("");
auto a = get_matrix(n, m);
random_fill(a, n, m);
show(a, n, m);
auto b = get_matrix(n, m);
random_fill(b, n, m);
show(b, n, m);
auto s = sum_matrix(a, b, n, m);
show(s, n, m);
auto r = sub_matrix(a, b, n, m);
show(r, n, m);
a = destroy(a, n);
b = destroy(b, n);
s = destroy(s, n);
r = destroy(r, n);
system("pause > nul");
}
Иван Лазарев
Иван Лазарев
63 139
Лучший ответ
Aсхат Амренов Сложный для понимания код... А можно сделать только с библиотекой iostream?