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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>

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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>

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.