C/C++

Помогите исправить ошибку в коде, с++

// A-B*C

#include <iostream>
using namespace std;

double** CreateMatrix(int n)
{
double** arr = new double* [n];
for (int i = 0; i < n; i++)
{
arr[i] = new double[n];
}
return arr;
}

void InputMatrix(double** arr, int& n)
{
cout << "Ведите порядок матрицы: ";
cin >> n;

cout << "Enter elements of matrix: " << endl;

for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
cin >> arr[i][j];
}

void MultiplicationMatrix(double** A, double** B, double** C, int n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
C[i][j] = 0;
for (int k = 0; k < n; k++)
{
C[i][j] += A[i][k] * B[k][i];
}
}
}
}

void MatrixRaz(double** A, double** B, double** C, int m, int n)
{
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
C[i][j] = A[i][j] - B[i][j];
}


void PrintMatrix(double** A, int m, int n)
{
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
cout << A[i][j] << " ";
}
cout << endl;
}
}

int main()
{
int n;
cout << "Введите количество строк и столбцов в матрицах: ";
cin >> n;

double** A = CreateMatrix(n);
double** B = CreateMatrix(n);
double** C = CreateMatrix(n);
double** BC = CreateMatrix(n);
double** D = CreateMatrix(n); //D - результат

cout << "Объявление матрицы A: " << endl;
InputMatrix(A, n);
cout << "Объявление матрицы B: " << endl;
InputMatrix(B, n);
cout << "Объявление матрицы C: " << endl;
InputMatrix(C, n);

MultiplicationMatrix(B, C, BC, n);
MatrixRaz(A, BC, D, n);

cout << "Итоговая матрица: " << endl;
PrintMatrix(D, n);

return 0;
}
A_
Angel_Of _Death
132
 MatrixRaz(A, BC, D, n, n);
PrintMatrix(D, n, n);
Эти функции были написаны под прямоугольные матрицы, так что вместо размеров m и n нужно два раза ввести n. Ну или убрать в них "int m" из аргументов, a "m" заменить на "n" в циклах.
Aleksandr Ivanov
Aleksandr Ivanov
51 417
Лучший ответ
Angel_Of _Death Спасибо!
 #include  
#include
#include
#include

using namespace std;

double** create_matrix(const size_t n, const size_t m) {
auto matrix = new(nothrow) double* [n];
if (matrix != nullptr) {
for (size_t i = 0; i < n; ++i) {
matrix[i] = new double[m];
if (matrix[i] == nullptr) {
for (size_t j = 0; j < i; ++j) delete[] matrix[j];
delete[] matrix;
matrix = nullptr;
break;
}
}
}
return matrix;
}

double** destroy_matrix(double** matrix, const size_t n) {
if (matrix != nullptr) {
for (size_t i = 0; i < n; ++i) delete[] matrix[i];
delete[] matrix;
matrix = nullptr;
}
return matrix;
}

void fill_matrix_random(double** matrix, const size_t n, const size_t m) {
uniform_int_distribution uid(10, 99);
mt19937 gen{ random_device()() };
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) {
matrix[i][j] = uid(gen) / 10.0;
}
}
}

void show_matrix(double** matrix, const size_t n, const size_t m, const streamsize w) {
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < m; ++j) cout
Асек Амантай
Асек Амантай
87 724
C[i][j] = 0;

for (int k = 0; k < n; k++)

{

C[i][j] += A[i][k] * B[k][j];

}

}

}

}

}

int main()

{

setlocale(LC_ALL, “Russian”);

int n;

double** A, **B, **C;

cout << "Введите порядок матрицы А: ";

cin >> n;

A = CreateMatrix(n);

InputMatrix(A, n);

cout << endl;

B = CreateMatrix(n);

InputMatrix(B, n);

MultiplicationMatrix(A, B, C, n);

system(“pause”);

return 0;
Ю. Баб.
Ю. Баб.
40 871