Как написать лучше и так чтоб работало правильно?
#include <iostream>
#include <fstream>
#include <vector>
#include <ctime>
using namespace std;
void CreatMatr(int n, int m, int l) {
ofstream file("matriza.bin", ios::app);
cout << "Ввести строки первой матрицы";
cin >> n;
cout << "Ввести столбцы для первой матрицы и строки для второй";
cin >> m;
int** M;
M = new int* [n];
for (int i = 0; i < n; i++)
M[i] = new int[m];
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
M[i][j] = rand() % 13, 5;
cout << "Ввести столбцы второй матрицы";
cin >> l;
int** M2;
M2 = new int* [m];
for (int i = 0; i < m; i++)
M2[i] = new int[l];
for (int i = 0; i < m; i++)
for (int j = 0; j < l; j++)
M2[i][j] = rand() % 13, 5;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++) {
file << M[i][j] << " ";
}
file << endl;
}
file << endl;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < l; j++) {
file << M2[i][j] << " ";
}
file << endl;
}
file << endl;
file.close();
}
vector<vector<int>> ReadMatrix(ifstream& is, int n, int m) {
vector<vector<int>> result(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
is >> result[i][j];
}
}
return result;
}
void WriteMatrix(ofstream& os, const vector<vector<int>>& m1) {
os << m1.size() << " " << m1[0].size() << "\n";
for (int i = 0; i < m1.size(); ++i) {
for (int j = 0; j < m1[0].size(); ++j) {
os << m1[i][j] << " ";
}
os << "\n";
}
}
vector<vector<int>> MatrixProduct(const vector<vector<int>>& m1, const vector<vector<int>>& m2) {
int n = m1.size(), m = m2.size(), l = m2[0].size();
vector<vector<int>> result(n, vector<int>(l));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < l; ++j) {
int sum = 0;
for (int k = 0; k < m; ++k) {
sum += m1[i][k] * m2[k][j];
}
result[i][j] = sum;
}
}
return result;
}
int main() {
setlocale(LC_ALL, "Rus");
int m = 1, n = 1, l=1;
int k = 3;
int r = 0;
while (r < k) {
CreatMatr(n, m, l);
r++;
}
ifstream file("matriza.bin", ios::in);
ofstream out("result.bin", ios::out);
if (!file.is_open() || !out.is_open()) {
cout << "Файл не открыт\n";
return 1;
}
for (int i = 0; i < k; i++) {
file >> n >> m >> l;
vector<vector<int>> m1 = ReadMatrix(file, n, m);
vector<vector<int>> m2 = ReadMatrix(file, m, l);
WriteMatrix(out, MatrixProduct(m1, m2));
}
out.close();
file.close();
return 0;
}