Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Задача на умножение матриц.

#include <iostream>
#include <stdio.h>

using namespace std;

int **arr_input(int y, int x, char name)


{
//initialize
int **arr = new int* [y];
for (int i = 0; i < y; i++)
arr[i] = new int[x];

//arr input
cout << endl << name << ":" << endl;
for (int i = 0; i < y; i++) {
for (int j = 0; j < x; j++) {
arr[i][j] = 1 + rand() % 10;
cout << arr[i][j] << " ";
}
cout << endl;
}

return arr;
}

void multi_arr(int** arr1, int r1, int c1, int** arr2, int r2, int c2) {

if (c1 == r2) {

int **arr = new int* [r1];


for (int i = 0; i < r1; i++) {
arr[i] = new int[c2];
for (int j = 0; j < c2; j++) {
arr[i][j] = 0;
cout << endl;
for (int k = 0; k < c1; k++) {
cout << arr1[i][k] << " * " << arr2[k][j] << endl;
arr[i][j] += arr1[i][k] * arr2[k][j];
}
}
}

cout << endl;


for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) cout << arr[i][j] << " ";
cout << endl;
}
}

else {
cout << "Ax != By";
}
}

int main() {

//size input
int r1, c1, r2, c2;
cout << "\nEnter size of the A array \nx = "; cin >> c1; cout << "y = "; cin >> r1;
cout << "\nEnter size of the B array \nx = "; cin >> c2; cout << "y = "; cin >> r2;

//m, k - columns
//n, l - rows

int **a = arr_input(r1, c1, 'A'), **b = arr_input(r2, c2, 'B');

multi_arr(a, r1, c1, b, r2, c2);

cout << endl;

return 0;
}

You might also like