creating 2d array dynamically

Hi.
What's wrong with the following code?I get a runtime error
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
	int	n, m, p;	//Used as the dimensions of the arrays
	
	
	cout << "Please enter the number of rows of the first array:" <<  endl;
	cin >> n;
	
	cout << "Please enter the number of columns of the first array:" <<  endl;
	cin >> m;
	
	int**a, **b, **c;
/***********Creating an n*m array**********************/
	*a  = new int[n];
    int  dim;   //used to traverse the rows of the array
    int  i;     //used to traverse the columns of the array
    
    
	for(dim=0;dim<n;++m)
       a[dim] = new int[m];
	
/***********Creating an m*p array**********************/
	
	*b  = new int[m];
	
	for(dim=0;dim<m;++dim)
        b[dim] = new int[p];
	  
/***********Creating an n*p array**********************/
	
	*c  = new int[n];
	
	for(dim=0;dim<n;++dim)
	   c[dim] = new int[p];
/*********reading arrays************/

/********reading array a***********/
	for(dim=0;dim<n;++m)
	{
        cout << "please enter the elements of row " << dim 
            <<" of array 'A' seperated by spaces:" <<  endl;
       for(i=0;i<m;++i)
       cin >> a[dim][i];
    }
/********reading array b***********/

    for(dim=0;dim<n;++dim)
    {
      cout << "please enter the elements of row " << dim 
           <<" of array 'B' seperated by spaces:" <<  endl;
      for(i=0;i<p;++i)
	    cin >> b[dim][i];
   }
   cout << "*****Array A**********"   ;
   for(dim=0;dim<n;dim++)
   {
      for(i=0;i<p;++i)
         cout << a[dim][i];
      if( dim == (n-1) )
         cout << endl;
   }
   cout << "*****Array B**********";
   for(dim=0;dim<n;dim++)
   {
      for(i=0;i<p;++i)
         cout << a[dim][i];
      if( dim == (n-1) )
         cout << endl;
   }
Last edited on
A generic usage of the dynamic 2D array memory allocation:

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
#include <iostream>

int main()
{
	int	m, n;	//Used as the dimensions of the arrays
	
	
	std::cout << "Please enter the number of rows of the first array:" << '\n';
	std::cin >> m;
	
	std::cout << "Please enter the number of columns of the first array:" << '\n';
	std::cin >> n;
	
	int **a;
	
	a = new int *[m];
	
	for(int i = 0; i < m; ++i)
		a[i] = new int [n];
		
		
	for(int i = 0; i < m; ++i)
		for(int j = 0; j < n; ++j)
			a[i][j] = 1;
			
	for(int i = 0; i < m; ++i)
	{
		for(int j = 0; j < n; ++j)
			std::cout << a[i][j] << ' ';
		std::cout << '\n';
	}
	
	return 0;
}
Please enter the number of rows of the first array: 6
Please enter the number of columns of the first array: 5
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1 
1 1 1 1 1
Last edited on
There are two ways to have a dynamic 2D array:

    1 array of pointers to arrays of element (more convenient to use, more cumbersome to create and destroy)
    2 1D array indexed as 2D array (this is how a C++ 2D array works)

Condor's response is the first one -- an array of pointers, where each pointer points to an array of elements (in this case, the elements are ints).

For the second type you just need a simple function to index the 2D elements. Read more here:
http://www.cplusplus.com/forum/general/116742/#msg637363

Maybe I'll work on the 2D array FAQ next -- it is getting a lot of attention lately.

Hope this helps.
@Duoas

Thank you very much for your note.
@condor
so how to destroy your version?

Releasing the dynamic allocated memory:

1
2
3
for (int i = 0; i < m; ++i)
	delete [] a[i];
delete [] a;

Last edited on
And what's wrong with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
	cout << "Please enter the number of columns of the first array:" <<  endl;
	cin >> m;
	
	cout << "Please enter the number of columns of the second array:" <<  endl;
	cin >> p;
	
	int **a, **b, **c;
	
/***********Creating an n*m array**********************/
	a  = new int *[n];
    int  dim=0;   //used to traverse the rows of the array
    int  i=0;     //used to traverse the columns of the array

	for(dim=0;dim<n;++m)
       a[dim] = new int[m];
/********reading array a***********/
	for(dim=0;dim<n;++m)
	{
        cout << "please enter the elements of row " << dim 
            <<" of array 'A' seperated by spaces:" <<  endl;
       for(i=0;i<m;++i)
       cin >> a[dim][i];
    }

that it doesn't execute to completion?
For creating matrix a[n][m] where is n? Who is p in this stuff?
Topic archived. No new replies allowed.