How to add the diagonal of a matrix in C?

Asked

Viewed 15,417 times

3

Help me in the following exercise:

Make a program that defines an integer array of size 5x5. Next, initialize this matrix with random numbers between 5 and 9. Finally, your program must calculate the sum of the diagonal elements of this matrix. The program must print the generated matrix and the sum of its diagonal. Here is an example of output. Generated matrix:

5 6 7 8 9
9 5 6 7 8
8 9 5 6 7
7 8 9 5 6
6 7 8 9 5

The sum of the diagonal of the matrix is: 25.

The code I made, it doesn’t add up correctly:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char**argv){

    int matriz[5][5];
    int i, x, j, p;
    int soma=0;

    srand(time(NULL));

    printf("Matriz gerada: ");

    for(i=0; i<5; i++){
        printf("\n");
            for(j=0; j<5; j++){
                x=5+(rand()%5);
                printf("%3d", x);
                matriz[i][j];
                if(i==j){
                    for(p=0; p<5; p++){
                        soma+=matriz[i][j];
                    }
                }
            }
    }
    printf("\n");
    printf("Soma da diagonal principal: %d", soma);
    return 0;
}
  • 1

    The problem is that you don’t update the value of the matrix cell when you draw the number. Instead of doing matriz[i][j];, do matriz[i][j] = x; on the third line within the for more internal. Probably is a typo. :)

  • 2

    CORRECTION: I withdrew the for of the sum, I was adding several times. thank you Luiz Vieira!

  • The third for with the variable p doesn’t make sense. Like you said, the third for makes the sum count each cell in the middle five times.

2 answers

4

Your code had basically two problems. Like @Luiz Vieira mentioned, you weren’t holding the value of x in the matrix. Also, as you yourself noticed, it had an unnecessary loop making the sum.

To make it easier to read, all redundant variables were removed and given a slight blurring in the code, already solving the two problems mentioned:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char**argv){
    int matriz[5][5];
    int i, j;
    int soma=0;

    srand( time( NULL ) );

    printf( "Matriz:\n" );
    for( i = 0; i < 5; i++ ){
        for( j = 0; j < 5; j++ ){
            matriz[i][j] = 5 + rand() % 5;
            printf( "%3d", matriz[i][j] );
            if( i==j ){
                soma += matriz[i][j];
            }
        }
        printf( "\n" );
    }
    printf( "Soma da diagonal principal: %d", soma );
    return 0;
}

See working on IDEONE.

Now, imagining that you had to add up the diagonal of a pre-existing matrix, see what the sum loop would look like, if it was independent of the rest:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char**argv) {
    int matriz[5][5];
    int i, j;
    int soma = 0;

    srand( time( NULL ) );

    /* gerando a matriz */
    printf( "Matriz:\n" );
    for( i = 0; i < 5; i++ ) {
        for( j = 0; j < 5; j++ ) {
            matriz[i][j] = 5 + rand() % 5;
            printf( "%3d", matriz[i][j] );
        }
        printf( "\n" );
    }

    /* fazendo a soma */
    for( i = 0; i < 5; i++ ) {
        soma += matriz[i][i];
    }
    printf("Soma da diagonal principal: %d", soma);

    return 0;
}

See working on IDEONE.

0

I did it in a way that doesn’t even need to if, in C, I’m new in programming but if it helps anything, here it is:

   #include<stdio.h>
     main()
    {
         int D[5][5], i=0, j=0, soma1=0,soma2=0 ,x=5;
          printf("--------------------------\ninforme os valores da matriz\n____________________________\n");
for(i=0;i<5;i++){   
    x--;
    for(j=0;j<5;j++){
        printf("%d - %d = ",i+1,j+1);
        scanf("%d",&D[i][j]);
    }
    soma1=soma1+D[i][i];
    soma2=soma2+D[i][x];
}
printf("\n-----------\nsoma diagonal 1:\t%d",soma1);
printf("\n-----------\nsoma diagonal 2:\t%d",soma2);

}

Browser other questions tagged

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