Multiplication of arrays and store in another array?

1

Be a function that receives three arrays of integer values A, B, and C, and two integers n and m representing the number of rows and columns of these arrays. This function must compute the multiplication of the matrices A and B and store the result in the matrix C.

This is my role:

void multiplicacao(int matA[] [MAX], int na, int ma, int matB[] [MAX],int nb, int mb, int matC [] [MAX], int nc, int mc)  
{  

int i , j;

if (na == nb && nb == nc && na == nc && ma == mb && mb == mc && ma == mc)
{
    for (i =0; i < na ;i ++)
    {
        for ( j = 0; j < ma ;j ++)
        {
           printf (" Digite matA [%d][%d]: ",i , j) ;
           scanf ("%d",&matA [i][j]) ;
        }
    }

    for (i =0; i < nb ;i ++)
    {
        for ( j = 0; j < mb ;j ++)
        {
           printf (" Digite matB [%d][%d]: ",i , j) ;
           scanf ("%d",&matB [i][j]) ;
        }
    }

    for(i=0; i<nb && i<na; i++)
    {
        for(j=0; j<nb && j<na; i++)
        {
            matC[i][j]= (matA[i][j]) * (matB[i][j]);
        }
        printf("%d \n",matC[i][j] );
    }
}

}

Only that it is not compiling, it is giving "Segmentation failure (recorded core image)". What's wrong? Is my logic also right for storing in C?

    
asked by anonymous 19.06.2018 / 20:54

1 answer

1

One thing is to read the user's arrays, another is to multiply them and another is to display it. The function that multiplies the matrices must do no more than the multiplication itself. The reason for this is that if you want to multiply arrays read from a file or produced as a result of some other process, this is not possible if you are reading them from the user. The same thing if you want to display an array without having multiplied.

The functions of reading and displaying an array are as follows:

void ler_matriz(int mat[][MAX], int n, int m) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("Digite mat[%d][%d]: ", i, j);
            scanf("%d", &mat[i][j]);
        }
    }
}

void mostrar_matriz(int mat[][MAX], int n, int m) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            printf("%d ", mat[i][j]);
        }
        printf("\n");
    }
}

Let's look at this condition:

na == nb && nb == nc && na == nc

na == nc is unnecessary. The same can be said of ma == mc .

Another problem with your code is this:

    for(j=0; j<nb && j<na; i++)

You are using j as a loop variable, but you are incrementing i .

You can also use an integer return to represent error. Let's put that 0 is ok and 1 is array size error.

So if you wanted to just multiply the elements in the same positions, then:

int multiplicar_elementos(
        int matA[][MAX], int na, int ma,
        int matB[][MAX], int nb, int mb,
        int matC[][MAX], int nc, int mc)
{  

    if (na != nb || nb != nc || ma != mb || mb != mc) return 1;

    for (int i = 0; i < na; i++) {
        for (int j = 0; k < ma; j++) {
            matC[i][j] = matA[i][j] * matB[i][j];
        }
    }
    return 0;
}

However, if your idea is to implement the Matrix multiplication algorithm, the problem here is greater. In this case, the matrices do not have the same sizes. The number of rows in the first array is equal to the number of columns (not rows) in the second array. That is, unless you are multiplying square arrays, they do not should be the same sizes.

The product of an array of dimensions A x B by a B x C gives in an array A x C . >

As you are multiplying a na x ma array by a nb x mb to give a nc x% mc must be equal to ma , nb must equal na and nc must equal mb .

With this, we can reduce the six numbers of array sizes to just three.

The matrix multiplication algorithm will need three loops. The reason for this is that in the resulting array, each cell contains the sum of the products of the elements of a row of the first array by the elements of a column of the second. That way, to fill each cell of the third array, you'll need an internal loop traversing the first row and the second row at the same time. Out of this inner loop, you'll need two more to visit each cell in the third array.

Your neat code to multiply arrays looks like this:

int multiplicar_matrizes(
        int matA[][MAX], int na, int ma,
        int matB[][MAX], int nb, int mb,
        int matC[][MAX], int nc, int mc)
{  

    if (ma != nb || na != nc || mb != mc) return 1;

    int a = na, b = ma, c = mb;

    for (int i = 0; i < a; i++) {
        for (int k = 0; k < c; k++) {
            matC[i][k] = 0;
            for (int j = 0; j < b; j++) {
                matC[i][k] += matA[i][j] * matB[j][k];
            }
        }
    }
    return 0;
}
    
19.06.2018 / 22:01