So I had a Campus recruitment coding test, and came across a question which had to make use of matrices. In the pre-defined code supplied by the question, they specified the arguments in the function as int function_name(int input1, int input2, int **input3), where input3 is the variable passed for the MxN matrix. Any idea how to manipulate with the matrix values? Usually I pass the arguments as input3[][], and are manipulated using for loops.
It is evident that the third parameter is a pointer to a dynamically allocated array of pointers that in turn also point to dynamically allocated arrays (their first elements). Something like
int **input3 = new int *[M];
for ( size_t i = 0; i < M; i++ )
{
input3[i] = new int[N];
}
So you can use two indices to access elements of the dynamically allocated arrays within the function.
for ( size_t i = 0; i < input1; i++ )
{
for ( size_t j = 0; j < input2; j++ )
{
// do something with input3[i][j]
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.