Sum the diagonals of a matrix

Asked

Viewed 2,019 times

0

I need to do a program that takes out the diagonals of a matrix. As an example exemplo

But I’m having a hard time. Follows code:

#include <stdio.h>
#include <math.h>
#define M 4
#define N 3
int main(){
    int i, j, soma=0;
    int mat[M][N]={{1,2,3},{4,5,6},{7,8,9},{10,11,12}}, result[M+N-1];

    for(i=0; i<M+N-1; i++){ //deixar todos valores do vetor iguais a zero
        result=0;
    }

    int a, b, k=0, aux; //auxiliares

    for(i=M-1; i>=0; i--){
        for(j=N-1; j>=0; j--){
            result[k]=mat[j];   //posição 0 do vetor recebe posição 4x3 da matriz;
                a=i;
                b=j;
                while(a>=0 && b<N && b>=0){   //verificar se linha>=0, coluna<N e >=0
                    a--;  //decrementa linha
                    b++;  //acrescenta coluna
                    result[k]+=mat[a]; //valor da posição 0 do vetor soma com o proximo valo da diagonal superior direita
                }
            k++; //avança para proxima posição do vetor*/
        }
    }

    for(i=0; i<M; i++){
        for(j=0; j<N; j++){
            printf("\t%d   ", mat[j]);
        }
        printf("\n");
    }

    printf("\n");

    for(i=0; i<M+N-1; i++){
        printf("%d   ", result);
    }
}

At first I’m just trying to sum up the diagonals and then use the threads to carry out the processes. Can someone help me? Thank you in advance.

  • The logic is quite simple: considering the elements in the line i and column j, just add up all that sum of i+j be it z, being z the result matrix line. In other words: r[0] shall be the sum of all items the i+j is 0, that is, mat[0][0]; r[1] shall be the sum of all items the i+j is 1, that is, mat[0][1]+mat[1][0]; thus successively.

1 answer

2

Solution only implementing the logic of sums, as required, without working with threads.

Logic, as I commented here, to define the secondary diagonals is to verify the sum between the position of the line and the column of the element. For example, consider the generic matrix below:

    [ a00 a01 a02 ]
A = [ a10 a11 a12 ]
    [ a20 a21 a22 ]
    [ a30 a31 a32 ]

Applying the sum of the secondary diagonals, storing the sum in a matrix resultado, we have:

resultado[0] = a00
resultado[1] = a10 + a01
resultado[2] = a20 + a11 + a02
resultado[3] = a30 + a21 + a12 + a03
resultado[4] = a31 + a22
resultado[5] = a32

You realize the sum of i and j always results in the matrix position resultado? I mean, to resultado[0] the sum i+j always is 0, for resultado[1] the sum i+j always is 1, so on. So the code would look something like:

#define M 4
#define N 3
#define Z M+N-1

int main(int argc, char const *argv[]) {

    int mat[M][N] = {
        { 1,  2,  3},
        { 4,  5,  6},
        { 7,  8,  9},
        {10, 11, 12}
    };

    int result[Z] = {0};

    // z varia de 0 a M+N-1, exclusive
    // Indica a linha na matriz resultados:
    for (int z = 0; z < Z; z++) {

        // i varia de 0 a M, exclusive
        // Indica a linha na matriz mat:
        for (int i = 0; i < M; i++) {

            // j varia de 0 a N, exclusive
            // Indica a coluna na matriz mat:
            for (int j = 0; j < N; j++) {

                // Se a soma da posição for igual a z:
                if (i + j == z) {

                    // Incrementa a soma:
                    result[z] += mat[i][j];

                }

            }

        }

        // Exibe o resultado da soma:
        printf("%2d\n", result[z]);

    }

    return 0;
}

When executing the code, the output will be:

 1
 6
15
24
20
12

Note: The picture of the question brings the wrong result in the fourth line, because the sum should be 10+8+6 and was made 10+9+6.

To implement logic with threads, instead of you iterating on the value of z, you create the T threads, where each is responsible for calculating a given result position. Even you could define a function to calculate a specific diagonal:

int sum(int z, int mat[M][N]) {

    int result = 0;

    // i varia de 0 a M, exclusive
    // Indica a linha na matriz mat:
    for (int i = 0; i < M; i++) {

        // j varia de 0 a N, exclusive
        // Indica a coluna na matriz mat:
        for (int j = 0; j < N; j++) {

            // Se a soma da posição for igual a z:
            if (i + j == z) {

                // Incrementa a soma:
                result += mat[i][j];

            }

        }

    }

    return result;

}

And make the calls from result[0] = sum(0, mat), result[1] = sum(1, mat), ..., in threads.

See working on Ideone.

  • I think you forgot the link

  • 1

    @Jeffersonquesado Corrected, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.