Dualer
Newbie | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору Здравствуйте. Помогите пожалуйста с вопросом об умножении двух матриц. Есть код, который отлично выполняет эту операцию, но он нерационален для матриц с большими размерностями, т.к. вводить вручную всю матрицу - это жестко. В общем, требуется умножить 2 матрицы размерностями 10*50 и 50*80 соответственно. Можете подсказать, как правильно сгенерировать эти две матрицы? Сам код: ------------------------------------------------------------- #include <iostream> //Выделение памяти для матрицы double ** AllocMatrix(int Rows, int Colomns) { using namespace std; double ** Matrix; try { Matrix = new double * [Rows]; if( Matrix == NULL ) throw 0; for (int i = 0; i < Rows; i++) { Matrix[i] = new double [Colomns]; if( Matrix[i] == NULL ) throw i; } } catch( int index ) { cout << "Error memory!!!" << endl; for (int i = 0; i < index; i++) { delete [] Matrix[i]; Matrix[i] = NULL; } return NULL; } return Matrix; } //Ввод матрицы bool GetMatrix(double ***Matrix, int *Rows, int *Colomns) { using namespace std; cout << "Rows: "; cin >> (*Rows); cout << "Colomns: "; cin >> (*Colomns); //Выделение памяти для массива double **Ret; Ret = AllocMatrix(*Rows, *Colomns); *Matrix = Ret; if (Ret != NULL) { using namespace std; for (int i = 0; i < *Rows; i++) for (int j = 0; j < *Colomns; j++) { printf("Matrix[%d][%d] = ", i, j); cin >> Ret[i][j]; } return true; }else return false; } //Проверка размерности bool CheckMatrixes(int Rows1, int Colomns1, int Rows2, int Colomns2) { return (Rows2 == Colomns1); } //Умножение матриц bool MultiplyMatrix(double **Matrix1, int Rows1, int Columns1, double **Matrix2, int Rows2, int Columns2, double ***ResultMatrix, int *ResultRows, int *ResultColumns) { using namespace std; *ResultRows = Rows1; *ResultColumns = Columns2; double **Ret; Ret = AllocMatrix(*ResultRows, *ResultColumns); *ResultMatrix = Ret; if (Ret != NULL) { for (int i = 0; i < *ResultRows; i++) for (int j = 0; j < *ResultColumns; j++) { double Sum = 0; for (int n = 0; n < Rows2; n++) { Sum += Matrix1[i][n]*Matrix2[n][j]; } (*ResultMatrix)[i][j] = Sum; } return true; }else return false; } //Вывод матрицы void MatrixOut(double **Matrix, int Rows, int Columns) { using namespace std; for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) cout << Matrix[i][j] << "\t"; cout << endl; } } //Освободить память матрицы void FreeMatrix(double **Matrix, int Rows) { for (int i = 0; i < Rows; i++) { delete [] Matrix[i]; Matrix[i] = NULL; } delete [] Matrix; Matrix = NULL; } int main() { using namespace std; locale::global(locale("rus")); double ***A = new double **, ***B = new double **, ***C = new double **; int ARowCount, AColCount, BRowCount, BColCount, CRowCount, CColCount; do { cout << "Enter the !A! matrix:\n"; } while (!GetMatrix(A, &ARowCount, &AColCount)); do { cout << "Now enter the !B! matrix:\n"; } while (!GetMatrix(B, &BRowCount, &BColCount)); if (CheckMatrixes(ARowCount, AColCount, BRowCount, BColCount)) { if (MultiplyMatrix(*A, ARowCount, AColCount, *B, BRowCount, BColCount, C, &CRowCount, &CColCount)) { cout << "Result is:\n"; MatrixOut(*C, CRowCount, CColCount); FreeMatrix(*C, CRowCount); } } else cout << "Cannot to do this operation\n"; FreeMatrix(*A, ARowCount); FreeMatrix(*B, BRowCount); system("pause"); return 0; } |