Last active
January 5, 2022 21:06
-
-
Save Yegorsh/a197c9a0717ba9217df1bfe4dd68467a to your computer and use it in GitHub Desktop.
Find more at https://solutionarium.notion.site/Matrix-multiplication-ed4f042cc05f45eb898555af3cc78f54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <ctime> | |
| #include <fstream> | |
| using namespace std; | |
| void FillMatrix(int **matrix, const int rows, const int cols){ | |
| for (int i=0; i<rows; i++){ | |
| for (int j=0; j<cols; j++){ | |
| matrix[i][j]=rand()%15; | |
| } | |
| } | |
| } | |
| void PrintMatrix(int **matrix, const int rows, const int cols){ | |
| for (int i=0; i<rows; i++){ | |
| for (int j=0; j<cols; j++){ | |
| cout << matrix[i][j] << "\t"; | |
| } | |
| cout << endl; | |
| } | |
| } | |
| int main(){ | |
| srand(time(NULL)); | |
| int cols_a; | |
| int rows_a; | |
| int cols_b; | |
| int rows_b; | |
| again: | |
| cout << "Число строк матрицы А" << endl; | |
| cin >> rows_a; | |
| cout << "Число столбцов матрицы А" << endl; | |
| cin >> cols_a; | |
| cout << "Число строк матрицы В" << endl; | |
| cin >> rows_b; | |
| cout << "Число столбцов матрицы В" << endl; | |
| cin >> cols_b; | |
| if (cols_a==rows_b) { | |
| int **matrix_c = new int *[rows_a]; | |
| for (int i = 0; i < rows_a; i++){ | |
| matrix_c[i] = new int [cols_b]; | |
| } | |
| int **matrix_a = new int *[rows_a]; | |
| for (int i = 0; i < rows_a; i++){ | |
| matrix_a[i] = new int[cols_a]; | |
| } | |
| int **matrix_b = new int *[rows_b]; | |
| for (int i = 0; i < rows_b; i++){ | |
| matrix_b[i] = new int [cols_b]; | |
| } | |
| FillMatrix(matrix_a, rows_a, cols_a); | |
| FillMatrix(matrix_b, rows_b, cols_b); | |
| for (int i = 0; i < rows_a; i++){ | |
| for (int k = 0; k < cols_b; k++){ | |
| for (int j = 0; j < cols_a; j++){ | |
| matrix_c[i][k] += matrix_a[i][j] * matrix_b[j][k]; | |
| } | |
| } | |
| } | |
| cout << "Matrix A" << endl; | |
| PrintMatrix(matrix_a, rows_a, cols_a); | |
| cout << endl << "Matrix B" << endl; | |
| PrintMatrix(matrix_b, rows_b, cols_b); | |
| cout << endl << "Matrix C" << endl; | |
| PrintMatrix(matrix_c, rows_a, cols_b); | |
| ofstream txt_matrix;{ | |
| txt_matrix.open("matrix.txt"); | |
| txt_matrix << "Matrix A" << endl; | |
| for (int i=0; i<rows_a; i++){ | |
| for (int j=0; j<cols_a; j++){ | |
| txt_matrix << matrix_a[i][j] << "\t"; | |
| } | |
| txt_matrix << endl; | |
| } | |
| txt_matrix << endl << "Matrix B" << endl; | |
| for (int i=0; i<rows_b; i++){ | |
| for (int j=0; j<cols_b; j++){ | |
| txt_matrix << matrix_b[i][j] << "\t"; | |
| } | |
| txt_matrix << endl; | |
| } | |
| txt_matrix << endl << "Matrix C" << endl; | |
| for (int i=0; i<rows_a; i++){ | |
| for (int j=0; j<cols_b; j++){ | |
| txt_matrix << matrix_c[i][j] << "\t"; | |
| } | |
| txt_matrix << endl; | |
| } | |
| txt_matrix.close();} | |
| } | |
| else{ | |
| cout << endl << "Число столбцов матрицы А должно быть равным числу строк матрицы В!" << endl; | |
| goto again; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment