Passing variables to 2D arrays

I don't know how to pass variables to an 2D array, I have included my code underneath. Obviously its wrong; can someone tell me how to do it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
using namespace std;
int main()
{
	
	int size1,size2;

	cout<<"Enter order seperated by space : ";
	cin>>size1>>size2;

	int *a;
	a=new int [size1][size2];

	delete []a;

	return 0;
}
hope u would understand my code

#include<iostream>
using namespace std;
int main()
{

int size[2][2];

cout<<"Enter 4 value, press enter after each value : ";
cin >> size[0][0] >> size[0][1];

cin >> size[1][0] >> size[1][1];

cout << "You have enter : ";
for (int i=0; i<2; i++)
{
for (int j=0; j<2; j++)
{
cout << "array[" << i;
cout << "][" << j ;
cout << "] : ";
cout << size[i][j] << endl;
}
}

cin >> size[1][0] >> size[1][1];
return 0;
}
 
int *a;


you can only use that for a single array, for a 2 dimensional array you'll need to use a pointer to a pointer to an int. Like so...

 
int **a


Anyways this piece of code you wrote doesn't work in c++ it's for java.
 
a=new int [size1][size2];


In c++ you'll have to allocate memory for the first array, then loop through that array and allocate memory for each pointer to an int. lol that sounds really confusing though so I'll give an example.

dynamic 2D array memory allocation in C++
1
2
3
4
5
int **array = new int*[size1];
for(int i = 0; i < size1; ++i)
{
     array[i] = new int[size2];
}


then to release it you'll have to loop through it again like so...
1
2
3
4
5
for(int i = 0; i < size1; ++i)
{
     delete [] array[i];
}
delete [] array;

Sorry um late to reply (had enough work on university). But I have to say I have tried out Dacster13's way It's working. So thank you Dacster13 for giving wisdom further on arrays and pointers.
You may also want to read this: http://www.cplusplus.com/forum/articles/17108/
Topic archived. No new replies allowed.