[b]Complete the following function that takes 2 integers m and n as parameters. The function defines a dynamic matrix of integers of size mXn, fills the matrix with information from the KB, then returns the address of the matrix.
Note: DO NOT forget to put the returned datatype in the given space(………….)
………………………. allocate (int m, int n) { } [/b]
and this is my solution :
int allocate (int m, int n){
cin>>n>>m;
int **a;
a=new int *a[n];
for (int i =0; i<n; i++)
a[i] = new int[m];
for (int i=0; i<n ; i++)
for (int J=0; j<m ; j++)
cin >> a[i][j];
return a;
}
Salam rabaya:
but i do that twice because it should be two dimintional array
You mean you write a 2 times because it is 2 dimensionall array?
a=new int *a[n];
It dosen't mater if it is 2 dimensional, it is incorect to write it twice.
And about the input of n and m: you pass them as parameters so you should get those value before you call this function and just pass them as parameters.
And I'm just curius, do you kow how to mane normal multidimensional array? If you do I would recomend you to use that solution. Because this way you use space for pointers to the second dimension arrays of your 2 dimensional array.