Well, arrays are not exactly the same thing as pointers. You can do something like the above when you have a one-dimensional array, but not here. A decent solution here would be something like this:
#include <iostream>
usingnamespace std;
void print(int * array, int dim1, int dim2)
{
for (int i=0; i<dim1; i++)
{
for (int j=0; j<dim2; j++)
cout << array[i*dim2+j] << ' ';
cout << endl;
}
}
int main()
{
int d1=2; int d2=3;
//instead of a two-dimensional (d1) x (d2) array
//use a one-dimensional (d1*d2) array
int * Mem_in=newint[d1*d2];
for(int i=0;i<d1;i++)
for(int j=0;j<d2;j++)
Mem_in[i*d2+j]=i+j;
print(Mem_in,d1,d2);
delete[] Mem_in;
cin.get();
return 0;
}
This way, the dimensions are not required to be known at compile time
and you get something slightly faster than nested vectors, but it's kind of messier.
Due to the pattern emerging wouldn't it be faster to make something like
1 2 3 4 5 6 7
for(int i=0; i< d1; ++i) // MAKES THE FIRST LINE BE 1 THROUGH WHATEVER
Mem_in[i]=i;
for(int i=1, j=0; i*j<d1*d2;){
Mem_in[i*d2+j]=Mem_in[(i-1)*d2+j]+1; // MAKE EACH NEXT LINE BE THE ONE ABOVE+1
++i;
++j; // DOUBT for( ... ;++i,++j) IS LEGAL
}
??
EDIT:
OK, so I did a quick test but, is seems something's off...
Output for d1=10,d2=9 is:
and another way (better ) is to send the unkbown dimension as a parameter as follows :
3.
void arrive(int *Mem_in[100], int dimension2)
{
....
}
int main()
{