+ 1

Help me in can't do sum of diagonal elements of a matrix

C language https://code.sololearn.com/cG6I4MVXbPHh/?ref=app

10th May 2022, 5:27 PM
Ravi Pandey
Ravi Pandey - avatar
1 Réponse
0
//read comments and try to correct mistakes.. // hope it helps.. #include <stdio.h> int main() { int N ,M; int mat[N][M],i,j; //N,M have undefined still so first take values then declare array scanf("N=%d and M=%d",&N,&M); //dont put anything except farmat specifiers in scanf.remove N= and .. for(i=0;i<N;i++){ for(j=0;j<M;j++){ printf ("enter matrix [%d][%d] \t",i,j); scanf("%d %d",mat[N][M]); //must be mat[i][j] , i,j. not N,M } } int sum=0; for(i=0;i<N;i++){ for(j=0;j<M;j++){ if(i==j){ sum=sum+mat[N][M];// here also mat[i][j], not mat[N][M] } else{ // dont need else here just output after loop next one. printf("the req sum of a diagonal elemensts of a matrix is %d",sum); } } } return 0; }
10th May 2022, 5:42 PM
Jayakrishna 🇮🇳