作为家庭作业的一部分,我必须创建一个函数,该函数获取动态矩阵并查找等于其(i + j)之和的矩阵元素,还必须按值返回数组的大小和通过引用返回数组。 实际上,我已经完成了大小部分和所有功能,但是我没有得到它如何将它从main发送到函数的arr struct(因为尚未初始化),以及如何在函数中更新它。 / p>
谢谢!
typedef struct Three { // struct for the elements
int i;
int j;
int value;
}Three;
typedef struct List { // linked list struct
Three node;
struct List *next;
}List;
Three CreateThree(int i, int j, int value);
List *CreateThreeList(int **Mat, int rows, int cols);
Three *CreateThreeArr(int **Mat, int rows, int cols, int *newsize);
int CreateArrAndList(int **Mat, int rows, int cols);
void main() {
int **mat;
int row, col, i, j, value, arr_size;
printf("Please select the number of Rows and Cols in Matrix :\n");
printf("Rows: ");
scanf("%d", &row);
printf("Columns: ");
scanf("%d", &col);
mat = (int**)malloc(row * sizeof(int));
for (i = 0; i<col; i++)
mat[i] = (int*)malloc(col * sizeof(int));
printf("Please select %d values to your matrix:\n", row*col);
for (i = 0; i<row; i++)
{
for (j = 0; j<col; j++)
{
printf("select value for [%d][%d]: ", i, j);
scanf("%d", &value);
mat[i][j] = value;
}
}
arr_size = CreateArrAndList(mat, row, col);
}
Three CreateThree(int i, int j, int value) {
Three node;
node.i = i;
node.j = j;
node.value = value;
return node;
}
List *CreateThreeList(int **Mat, int rows, int cols) {
int i, j;
List *lst = NULL;
List *currLst = lst;
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if ((i + j) == Mat[i][j]) {
List *tmp = (List*)malloc(sizeof(List));
tmp->node = CreateThree(i, j, Mat[i][j]);
tmp->next = NULL;
if (currLst == NULL) {
lst = tmp;
}
else {
currLst->next = tmp;
}
currLst = tmp;
}
}
}
return lst;
}
Three *CreateThreeArr(int **Mat, int rows, int cols, int *newsize) {
int i, j, arrIndex = 0, size = 0;
Three *arr;
arr = (Three*)malloc((rows*cols) * sizeof(Three));
for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) {
if ((i + j) == Mat[i][j]) {
arr[arrIndex++] = CreateThree(i, j, Mat[i][j]);
size++;
}
}
}
arr = (Three*)realloc(arr,size * sizeof(Three));
*newsize = size;
return arr;
}
int CreateArrAndList(int **Mat, int rows, int cols) {
int arrSize;
Three *arr = CreateThreeArr(Mat, rows, cols, &arrSize);
List *lst = CreateThreeList(Mat, rows, cols);
return arrSize;
}
答案 0 :(得分:0)
ptrdiff_t create_arr_and_list(int **mat, ptrdiff_t rows, ptrdiff_t cols, struct three **arr, struct list **lst)
{
ptrdiff_t nmemb;
*arr = create_three_arr(mat, rows, cols, &nmemb);
*lst = create_three_list(mat, rows, cols);
return nmemb;
}
您可以在main(struct three *arr;
和struct list *lst;
)中创建一个指针,将指针传递给那些指针(create_arr_and_list(mat, rows, cold, &arr, &lst);
),然后稍后在函数中对其进行初始化。 / p>
int main(void)
{
int **mat;
int value
ptrdiff_t row, col, nmemb;
struct three *arr;
struct list *lst;
printf("Please select the number of Rows and Cols in Matrix :\n");
printf("Rows: ");
scanf("%ti", &row);
printf("Columns: ");
scanf("%ti", &col);
mat = malloc(sizeof(*mat) * row);
for (i = 0; i < col; i++)
mat[i] = malloc(sizeof(*mat[i]) * col);
printf("Please select %ti values to your matrix:\n", row * col);
for (ptrdiff_t i = 0; i < row; i++) {
for (ptrdiff_t j = 0; j < col; j++) {
printf("select value for [%ti][%ti]: ", i, j);
scanf("%d", &value);
mat[i][j] = value;
}
}
nmemb = create_arr_and_list(mat, rows, cold, &arr, &lst);
}