Help me in the following exercise:
Make a program that defines an array of 5x5 size integers. Then, initialize this array with random numbers between 5 and 9. Finally, your program must calculate the sum of the diagonal elements of that matrix. The program should print the generated matrix and the sum of its diagonal. Here is an example output. Matrix generated:
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 did, does not sum 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;
}