### Input to a dynamic multidimensional array

Hi all,
I am currently reading Frank Luna's "Intro to 3D Game Programming".
One of the exercises is to write a program that calculates the transpose of a matrix. Following is the start of my code for this exercise:

 ``123456789101112131415161718192021222324252627282930313233343536`` ``````#include using namespace std; int main(void) { cout << "-------------------------\n" <<"Transpose of a Matrix\n" << "-------------------------\n\n"; int i,j; // i = rows; j = columns; cout << "Specify how many rows compose the matrix (up to 4): "; cin >> i; cout << "How many columns (up to 4): "; cin >> j; //declaration int ** matrix; //allocation matrix = new int*[i]; for (int n = 0; n < i; n++) { matrix[n] = new int[j]; } for (int a = 0; a < i; a++) { for (int b = 0; b < j; b++) { cout << "Enter " << a+1 << b+1 << ": "; cin >> matrix[i][j]; } } return 0; }``````

There is a problem with the way I am initializing the matrix array. I am not getting a compiler error, but the program crashes after you input the first value. Please tell me how to properly handle user input of the matrix elements.

Thanks,
tyler22
line 32 should be `cin >> matrix[a][b];`. That's why you shouldn't name i = rows; j = columns;

and you must delete 2d array at the end of your program
thanks tnt
EDIT:
* I realized that the transpose of an mxn matrix is an nxm matrix. That explains why it worked fine for square matrices. I fixed the problem and the program works as expected now.*

I have another problem. I am not sure if I am deallocating the memory correctly or if the error is something else. Here is my new program:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include using namespace std; void printMatrix(int* arr[],int,int); void transposeMatrix(int* arr[],int,int); //(matrix,rows,columns); int main(void) { cout << "-------------------------\n" <<"Transpose of a Matrix\n" << "-------------------------\n\n"; int i,j; // i = rows; j = columns; cout << "Specify how many rows compose the matrix (up to 4): "; cin >> i; cout << "How many columns (up to 4): "; cin >> j; //declaration int ** matrix; //allocation matrix = new int*[i]; for (int n = 0; n < i; n++) { matrix[n] = new int[j]; } for (int a = 0; a < i; a++) { for (int b = 0; b < j; b++) { cout << "Enter " << a << b << ": "; cin >> matrix[a][b]; } } cout << "A:\n"; printMatrix(matrix,i,j); cout << "Transpose of A:\n"; transposeMatrix(matrix,i,j); // deallocation for (int m = 0; m < i; m++) delete matrix[m]; delete matrix; return 0; } void printMatrix(int* arr[],int i, int j) { for (int a = 0; a < i; a++) { for (int b = 0; b < j; b++) { cout << arr[a][b] << " "; } cout << "\n"; } } void transposeMatrix(int* arr[],int i,int j) { //temporary array int ** temp; //allocation temp = new int*[i]; for (int n = 0; n < i; n++) { temp[n] = new int[j]; } for (int a = 0; a < i; a++) { for (int b = 0; b < j; b++) { temp[a][b] = arr[b][a]; //reverse } } printMatrix(temp,i,j); //deallocation for (int m = 0; m < i; m++) delete temp[m]; delete temp; }``````

For whatever reason, if I input a square matrix the program works as expected. Problems arise if I input matrices of different dimensions. For example, a 1x3 will crash when it tries to transpose, and a 4x2 will transpose part of the matrix correctly but the other elements will be crazy numbers (like -32777).

If anyone can help me I would appreciate it.
Last edited on
Topic archived. No new replies allowed.