我做了这个函数,可以将任何矩阵拆分成较小的矩阵,然后将其用于另一个函数,该函数将继续通过需要使用for循环(循环遍历)在“容器”数组上进行计算。考虑到此函数中的参数来自另一个函数,您将知道如何正确返回“容器”数组以在另一个函数中使用(而不是在主函数中使用)。因此,此函数将从另一个函数接收一个数组,然后将其拆分并将其打包为3D数组,然后将其返回以用于该函数中来自以下参数的参数
int split_matrix(int matrix[][COL3]){
int row2, col2, row3, col3, row1, col1 , row , col , x , unwanted_col;
int container[COL3][COL3-1][COL3-1];
for (x =0, unwanted_col = 0 ; x < COL3 ; x++ , unwanted_col++){
for (row2 = 0 , row3=0; row3 < COL3 ; row3++ ){
for (col2=0, col3 =0; col3 < COL3; col3++){
if (row3!=0 && col3 !=unwanted_col){
container[x][row2][col2]= matrix[row3][col3] ;
col2++;
if (col2 == (COL3-1)){row2++;}
}
}
}
}
return ?? ;}
容器3D数组将在此函数中使用,此函数将从用户输入(主函数)接收2D数组作为参数,然后将此数组传递给上述函数,然后使用上述函数的返回值(在这种情况下为容器3D阵列)并继续进行操作,如何在上述函数中从(容器3D阵列)中提取所有元素,并在此处使用它们。
void function_name(int matrix[][COL3]){
// for loop goes here to cycle through the return array and print every single element in the matrix return from the first function
我该如何做一个正确的回报,以及如何在第二个函数中使用它?
这是一个符合要求的答案:通过创建一个动态的指针数组来制作3D数组。这样就可以在函数外部使用数组。使用malloc()分配动态内存。最后,释放3D阵列以再次使用。
int COL; //global variable that will be assigned a value by the user's input
void split_matrix (int *matrix[COL]);
int main ()
{
scanf ("%d", &COL);
//code goes here to receive the matrix from the user
split_matrix (the_matrix);
return 0;
}
void split_matrix (int *matrix[COL])
{
int x, y;
int ***container;
container = malloc (COL * sizeof (int **));
for (x = 0; x < (COL); x++) {
container[x] = malloc ((COL - 1) * sizeof (int *));
for (y = 0; y < (COL - 1); y++) {
container[x][y] = malloc ((COL - 1) * sizeof (int));
}
}
//code goes here to split the parameter matrix(2D array)
for (x = 0; x < (COL); x++) {
for (y = 0; y < (COL - 1); y++) {
free (container[x][y]);
}
free (container[x]);
}
free (container);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句