添加动态分配的多个矩阵?

Adding of multiple matrices which are dynamically allocated?

这里我处理的是“DMA 到矩阵”,这让我在添加动态分配的矩阵时非常困惑。

这就是我创建多维

的方式
int main(void)
{
    int ***array, rows, columns, matrices, *arraySum;
    printf ("\n Enter Number of Matrices : ");
    scanf ("%d", &matrices);
    printf ("\n Enter Number of Rows : ");
    scanf ("%d", &rows);
    printf ("\n Enter Number of columns : ");
    scanf ("%d", &columns);

 //Allocating memory for matrices-array
    array = malloc ( sizeof ( array ) * matrices );
    if( !array )
    {
        perror ("Matrices-array");
        return 1;
    }
    for ( int i = 0; i < matrices; i++ )
    {
        array[i] = malloc ( sizeof ( array[i] ) * matrices );
        if ( !array[i] )
        {
            perror ("Matrix[i]");
            return 1;
        }
            printf ("\n\n Enter %d matrix elements !", i);
        for ( int j = 0; j < rows; j++ )
        {
            array[i][j] = malloc ( sizeof ( array[i][j] ) * columns );
        if ( !array[i][j] )
        {
           perror ("Matrix-rows");
           return 1;
        }
            for (int k = 0; k < columns; k++ )
            {
                  printf ("\n Enter element - [%d][%d] : ", j+1, k+1);
                  if ( scanf ("%d", &array[i][j][k]) != 1 )
                  {
                       printf("\n Wrong Input");
                       return 1;
                  }
            }
        }
        putchar ('\n');
    }


//Deallocating memories
for ( int i = 0; i < matrices; i++ )
{
    for ( int j = 0; j < rows; j++ )
    {
        free ( array[i][j] );   // 
        Deallocating memory  of matrix-rows
    }
    free ( array[i] ); // Deallocating memory  of matrices[i]
}
free ( array );
return 0;
}

这就是我尝试执行加法的方式

for ( int i = 0; i < mat; i++ )
{
   for ( int j = 0; j < rows; j++ )
   {
       for ( int k = 0; k < columns; k++ )
       {
           for ( int a; a < columns; a++)
           {
               arraySum = malloc ( sizeof ( arraySum ) * columns;
               arraySum[a] = array[i][j][k] + array[i+1][j+1][k+1];
               
           }
       }
}

但它不起作用!!!!

这就是我想要的

如果矩阵 = 2,行 = 2,列 = 3;

    a[0][0][0] + a[1][0][0];

    a[0][0][1] + a[1][0][1]
    a[0][0][2] + a[1][0][2];

    a[0][1][0] + a[1][1][0];

    a[0][1][1] + a[1][1][1];

    a[0][1][2] + a[1][1][2];*

我卡住了,帮帮我。

您只是错误地添加了矩阵。方法如下

首先,您需要分配矩阵并将其置零;

int** sumArray = (int**) malloc( sizeof(int*) * rows );
for(int i=0; i<rows; i++){
    sumArray[i] = (int*) malloc( sizeof(int) * columns );
    
    // initialize the sum with zero
    // memset could be used to initialize sumArray with 0
    for(int j=0; j<columns; j++){
        sumArray[i][j] = 0;
    }
}

然后,添加矩阵。

for(int i=0; i<rows; i++){
    for(int j=0; j<columns; j++){
        
        // for each (i,j) in sumArray add the corresponding
        // (i,j)s in the all give matrices. 
        for(int matrixIndex=0; matrixIndex<matrices; matrixIndex++){
            sumArray[i][j]+=array[matrixIndex][i][j];
        }
    }
}

首先..使用VLA。它更简单、更快、内存效率更高。 当内部维度的大小很小时(即int[1000][1000][2]),指针数组方法会浪费大量内存。而且,编译器很难向量化。

分配:

int (*array)[rows][columns] = calloc(matrices, sizeof *array);
// check if `array` is NULL

就这些了

释放:

free(array);

我假设您尝试对行和列求和 array

int (*sum)[columns] = calloc(rows, sizeof *sum); // allocation and zeroing
// error handling
for (int m = 0; m < matrices; ++m) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
        sum[i][j] += array[m][i][j];
}}}

// process sum

free(sum);