Nagraju Kasarla
Nagraju Kasarla

Reputation: 47

Adding of multiple matrices which are dynamically allocated?

Here I'm dealing with ' DMA to matrices ' which is very confusing me when its comes to additions of dynamically allocated matrices.

This is how I created a multi-dimensional

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;
}

This is how I tried to perform addition

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];
               
           }
       }
}

But it is not working!!!!.

This is what I want

If matrices = 2, rows = 2, cols = 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];*

I'm stuck , help me .

Upvotes: 0

Views: 49

Answers (2)

tstanisl
tstanisl

Reputation: 14107

First .. use VLAs. It is both simpler, faster and more memory efficient. Arrays of pointers approach wastes a lot of memory when size of inner dimension is small (i.e. int[1000][1000][2]). Moreover, it is difficult for a compiler to vectorize.

Allocation:

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

and that's all

Freeing:

free(array);

I assume that you try to sum array along rows and columns.

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);

Upvotes: 2

Moaz Fathy
Moaz Fathy

Reputation: 91

You are just adding the matrices incorrectly. Here is how to do it

First, you need to allocate the matrix and set it to zero;

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;
    }
}

Then, add the matrices.

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];
        }
    }
}

Upvotes: 1

Related Questions