MY solution

Good evening :)

i have this question :

[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;
}


is it right ??
or dose it have some error ?
If you are returning a, then the function has to return the same type as a.
1
2
3
int allocate (int m, int n){
int **a;
return a;

You wrote a twice here, it should be just int*
a=new int *a[n];

You declared uppercase J and used loowercase j.
for (int J=0; j<m ; j++)
you are also forgetting to delete that allocated memory.
If you are passing n and m as parameters to the function, why are you get new values from the user?

E: @DeXecipher
Why would he delete it when he is going to return it??

What zoran404 said is true. The function as to be of type pointer to pointer, i.e.

int **allocate(int m, int n){}
Last edited on
zoran404 :
thanks ..
but i do that twice because it should be two dimintional array


DeXecipher :
thanks..
it's good note .. but i would to return it :) as Smac89 said

Smac89 :
thanks...
should i delete the "cin" statement ...
but how can i enter it ???
i wanna to tell you something .. that's i'm a girl not boy :) :)



thanks all :)

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.
Ahaaaaaaaa .... i understand now :) :)
thanks

really i I learned only how to mane two dimensional array
because I'm still in the first year in the " Information Technology College"

and thanks for your help :) :)
Topic archived. No new replies allowed.