Last active
September 28, 2016 10:41
-
-
Save astrcomp/3b6958c349bc7b9e61d486f00a4b1eb4 to your computer and use it in GitHub Desktop.
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 <omp.h> | |
using namespace std; | |
void main() { | |
setlocale(LC_ALL, "RUS"); | |
cout << "-----Умножение матриц A[x][y] B[m][n]---.\nПервая матрица A [x] [y]\nВведите x: "; | |
int i = 0; | |
int x = 0, y = 0, m = 0, n = 0, dx = 0, dy=0; | |
cin >> x; | |
cout << "Введите y: "; | |
cin >> y; | |
cout << "\nПервая матрица A [" << x << "] [" << y << "]"<<endl; | |
m = y; //столбцы равны строкам второй. | |
cout << "\nВторая матрица B [" << m << "] [n]\nВведите n: "; | |
cin >> n; | |
cout << "\nВторая матрица B [" << m << "] [" << n << "]\n"; | |
dx = x; | |
dy = n; | |
////////////Динамические матрицы/////////// | |
int **a, **b, **dm; | |
a = new int*[x]; | |
for (i = 0; i < x; i++) a[i] = new int[y]; | |
b = new int*[m]; | |
for (i = 0; i < m; i++) b[i] = new int[n]; | |
dm = new int*[dx]; | |
for (i = 0; i < dx; i++) dm[i] = new int[dy]; | |
/////////Заполнение матриц/////////////// | |
char ch; | |
bool check = false, resultat = false; | |
do { | |
cout << "\nЗаполнить матрицы автоматически д/н (y/n):"; | |
cin >> ch; | |
switch (ch) | |
{ | |
case -83: | |
{ | |
cout << "\nРучной ввод\n"; | |
check = true; | |
break; | |
} | |
case 110: | |
{ | |
cout << "\nРучной ввод\n"; | |
check = true; | |
break; | |
} | |
case 121: | |
{ | |
cout << "\nАвтоматический ввод\n"; | |
check = true; | |
resultat = true; | |
break; | |
} | |
case -92: | |
{ | |
cout << "\nАвтоматический ввод\n"; | |
check = true; | |
resultat = true; | |
break; | |
} | |
default: | |
break; | |
} | |
} while (!check); | |
if (resultat) { | |
#pragma omp parallel for private (i) | |
for (i = 0; i < x; i++) | |
for (int j = 0; j < y; j++) {//cout << "А[" << i + 1 << "] [" << j + 1 << "] = "; | |
a[i][j] = rand()%1000; | |
} | |
#pragma omp parallel for private (i) | |
for (i = 0; i < m; i++) | |
for (int j = 0; j < n; j++) { | |
//cout << "B[" << i + 1 << "] [" << j + 1 << "] = "; | |
b[i][j] = rand() % 1000; | |
} | |
} | |
else { | |
cout << "введите матрицу А" << endl; | |
for (i = 0; i < x; i++) | |
for (int j = 0; j < y; j++) | |
{ | |
cout << "А[" << i + 1 << "] [" << j + 1 << "] = " ; | |
cin >> a[i][j]; //a[x][y] | |
} | |
cout << "введите матрицу B" << endl; | |
for (int i = 0; i < m; i++) | |
for (int j = 0; j < n; j++) | |
{ | |
cout << "B[" << i + 1 << "] [" << j + 1 << "] = "; | |
cin >> b[i][j]; | |
} | |
} | |
cout << "матрица А[" << x << "] [" << y << "]" << endl; | |
for (int i = 0; i < y+3;i++) cout << "-" ; | |
cout << endl; | |
for (int i = 0; i < x; i++) { | |
for (int j = 0; j < y; j++) | |
{ | |
cout << a[i][j] << " "; | |
} | |
cout << endl; | |
} | |
for (int i = 0; i < y + 3; i++) cout << "-"; | |
cout << "-" << endl; | |
cout << "матрица B[" << m << "] [" << n << "]" << endl; | |
for (int i = 0; i < n + 3; i++) cout << "-"; | |
cout << endl; | |
for (int i = 0; i < m; i++) { | |
for (int j = 0; j < n; j++) | |
{ | |
cout << b[i][j] << " "; | |
} | |
cout << endl; | |
} | |
for (int i = 0; i < y + 3; i++) cout << "-"; | |
cout << "-" << endl; | |
/////////УМНОЖЕНИЕ МАТРИЦ///////// | |
int inner, row, col,sum; | |
#pragma omp parallel for private (row,col,sum) | |
for (int row = 0; row < dx; row++) { | |
for (int col = 0; col < dy; col++) { | |
sum = 0; | |
//Multiply the row of A by the column of B to get the row, column of product. | |
for (inner = 0, dm[row][col] = 0; inner < y; inner++) { | |
sum+= a[row][inner] * b[inner][col]; | |
} | |
dm[row][col] = sum; | |
} | |
} | |
cout << "матрица X[" <<dx << "] [" << dy << "]" << endl; | |
for (int i = 0; i < dy + 3; i++) cout << "-"; | |
cout << endl; | |
for (int i = 0; i < dx; i++) { | |
for (int j = 0; j < dy; j++) | |
{ | |
cout << dm[i][j] << " "; | |
} | |
cout << endl; | |
} | |
for (int i = 0; i < y + 3; i++) cout << "-"; | |
cout << "-" << endl; | |
for (i = 0; i < x; i++) delete[] a[i];//освобождение памяти для каждого элемнта a[i] | |
delete[]a;//освобождение памяти для а | |
for (i = 0; i < m; i++) delete[] b[i];//освобождение памяти для каждого элемнта b[i] | |
delete[] b;//освобождение памяти для b | |
for (i = 0; i < dx; i++)delete[] dm[i]; //освобождение памяти для каждого элемнта dm[i] | |
delete[]dm; //освобождение памяти dm | |
getchar(); | |
getchar(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment