This Code works only for small number of matrix size.
This code is supposed to find the product of 2 matrices of which the size is entered by the user at runtime. MY code is working fine for small inputs of size of matrix say 2x2 or 3x3 if I put size more than 3 in any of the four field(row1,col1,row2,col2) it crashes. I am a beginner. And the reason I have used pointer scripting instead of subscript is that it was the requirement for the assignment.Also, where can I use delete command. Here is the code: #include using namespace std; int **multiply(int ** &mat1,int** &mat2,int row1,int col1,int col2,int row2) { int temp = 0; //creating the resultant array int **res_mat = new int*(&row1); // row1 x col2 will be the size of resultant matrix for (int i = 0; i < row1; i++) { *((res_mat)+i) = new int(col2); } for (int i = 0; i < row1; ++i) { for (int j = 0; j < col2; ++j) { *(*(res_mat + i) + j) = 0; } } cout< > row1; cout << "Columns in matrix 1: "; cin >> col1; cout << endl << endl; cout << "Rows in matrix 2: "; cin >> row2; cout << "Columns in matrix 2: "; cin >> col2; cout << endl << endl; if (col1 != row2) cout << "Error!!!\n\nPlease follow the Instructions: "; } while (col1 != row2); int **mat1 = new int*(&row1); // creates first matrix for (int i = 0; i < row1; i++) { *((mat1) + i) = new int(col1); } int **mat2 = new int*(&row2); // creates second matrix for (int i = 0; i < row2; i++) { *((mat2)+i) = new int(col2); } //*********** taking input in matrix 1************ cout << "\t\t*********** Input in matrix 1************\n\n"; for (int i = 0; i < row1; i++) //i=row1 { for (int j = 0; j < col1; j++) //j=col1 { cout << "Enter [" << i << "][" << j << "] : "; cin >> *(*(mat1 + i) + j); } } //*********** taking input in matrix 2************ cout << "\t\t*********** Input in matrix 2************\n\n"; for (int i = 0; i < row2; i++) //i=row2 { for (int j = 0; j < col2; j++) //j=col2 { cout << "Enter [" << i << "][" << j << "] : "; cin >> *(*(mat2 + i) + j); } } system("cls"); int **reciever; reciever = multiply(mat1, mat2, row1, col1, col2, row2); displayMatrix(reciever, row1, col2); cout << endl << endl; //deleting the matrices /*for (int i = 0; i < row1; i++) delete[](*mat1 + i); delete[]mat1; for (int i = 0; i < row2; i++) delete[](*mat2 + i); delete[]mat2; */ /*for (int i = 0; i < row1; i++) delete[](*reciever + i); delete[]reciever;*/ return 0; }
Posted by userNotDead 2020-06-24 02:02:41
No Ans Posted
Recent Links
- ty-CppBuzz-Forum..
- ty-CppBuzz-Forum..
- Write a program to do the following:-Cpp..
- recursive function-CppBuzz-Forum..
- System command and variable-CppBuzz-Foru..
- best fitness tracker under 2000-CppBuzz-..
- How do I arrange this code so the 3 arti..
- library management system-CppBuzz-Forum..
- c++-CppBuzz-Forum..
- SWITCH STATEMENT-CppBuzz-Forum..
- more..