Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何用另一个二维数组的内容填充二维数组的内容(其维度由用户输入)?_C_Arrays_Pointers_Multidimensional Array_Dynamic Memory Allocation - Fatal编程技术网

C 如何用另一个二维数组的内容填充二维数组的内容(其维度由用户输入)?

C 如何用另一个二维数组的内容填充二维数组的内容(其维度由用户输入)?,c,arrays,pointers,multidimensional-array,dynamic-memory-allocation,C,Arrays,Pointers,Multidimensional Array,Dynamic Memory Allocation,在以下作业中: 创建一个由100个随机数组成的数组,范围为1…999,为以下每个进程编写一个函数。在构建数组时,如果3或7将随机数等分,则将其存储为负数。 a。将数组的十个值打印到一行。确保值按行对齐。 B返回偶数个值的计数 C返回数组中所有值的总和 创建一个尺寸为10 X 10的二维数组。用上述一维数组中的值填充此二维数组。确定每行中的最大值。显示二维数组和每行的最大值。 三,。重复上面的数字2,但这次不是10 X 10数组,而是提示用户输入行和列的大小,允许用户填写值并显示数组。提示:使用指

在以下作业中:

创建一个由100个随机数组成的数组,范围为1…999,为以下每个进程编写一个函数。在构建数组时,如果3或7将随机数等分,则将其存储为负数。 a。将数组的十个值打印到一行。确保值按行对齐。 B返回偶数个值的计数 C返回数组中所有值的总和

创建一个尺寸为10 X 10的二维数组。用上述一维数组中的值填充此二维数组。确定每行中的最大值。显示二维数组和每行的最大值。 三,。重复上面的数字2,但这次不是10 X 10数组,而是提示用户输入行和列的大小,允许用户填写值并显示数组。提示:使用指针和动态内存分配

我被困在3号线上了。我不知道如何正确使用动态内存分配和指针来为用户输入的行数和列数腾出空间

int main(void)
{
int hundred[100];
cien(hundred);
even(hundred);
total(hundred);
two_dim(hundred, table);
hi_row(table);
cust_arr(hundred,table);
system("pause");
return 0;
}
void cien(int hundred[])
{
    int i = 0;
    int range = (999 - 1) + 1;
    for (i = 0; i < 100; i++)
    {
        hundred[i] = rand() % range + 1;
        if (hundred[i] % 3 == 0 || hundred[i] % 7 == 0)
        {
            hundred[i] = hundred[i] * -1;
            printf("%d\t", hundred[i]);
        }
        else
        {
            printf("%d\t", hundred[i]);
        }

    }
    return;
}

int two_dim(int hundred[], int arr[][10])
{
    int x;
    int y;
    int i = 0;
    int table[10][10];
    while (i != 100)
    {
        for (x = 0; x <10; x++)
        {
            for (y = 0; y < 10; y++)
            {
                arr[x][y] = hundred[i];
                printf("%d\t", arr[x][y]);
                i++;
            }
        }

    }
    return **table;
}

void** cust_arr(int hundred[], int table[][10])
{
    int x, y, i, j, k=0;
    int **arr;
    printf("input the number of rows.\n" );
    scanf_s("%d", &x);
    i = (int*)calloc(x, sizeof(int*));
    printf("input the number of columns.\n");
    scanf_s("%d", &y);
    j = (int*)calloc(y, sizeof(int*));
    while (k != 100)
    {
        for (i = 0; i <= x; i++)
        {
            for (j = 0; j <= y; j++)
            {
                hundred[k] = table[10][10];
                table[10][10] = **arr[i][j];
            }
        }
        printf("%d\n", **arr[i][j]);
        k++;
    }
}

由于提示显示重复数字2,我已尝试在cust_arr函数中为我建议使用的10x10阵列创建类似的循环。如果要使用,请使用malloc函数

int x, y;
int **table;

scanf("%d %d", &x, &y);
table = malloc(x*sizeof(int *));
for(int i = 0; i < x; i++)
    table[i] = malloc(y*sizeof(int));

除此之外,显示的代码有许多缺陷,需要修复。最好将所有函数声明和定义放在函数外部,并指定它们的返回类型和参数。

malloc将返回分配的内存区域的指针。您可以在指针上使用括号来指定索引,就像静态数组一样。因为需要一个2d数组,所以需要一个指向指针数组的指针,即列指向行,行指向值

int ** dynamicPointer = malloc(sizeof(int*)*columns);
for(int i = 0; i < columns; i++)
   dynamicPointer[i] = malloc(sizeof(int)*rows);
//address of final column and row
dynamicPointer[columns- 1][rows  - 1] = 3;
print("%d\n", dynamicPointer[columns- 1][rows  - 1]);