Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

how to calculate the sum of primary diagonal matrix and secondary diagonal matrix using c++

#include <bits/stdc++.h>
using namespace std;

int main() {
    int N, pd = 0, sd = 0;
    cin>>N;
    
    int mat[N][N];
    
    for(int i = 0; i < N; i++){
        for(int j = 0; j < N; j++){
            cin>>mat[i][j];
        }
    }
    
    for(int i = 0, c = N - 1; i < N, c >= 0; i++, c--){
        sd += mat[i][c];
        for(int j = 0; j < N; j++){
            if(i == j){
                pd += mat[i][j];
            }
        }
    }
    
    int res = abs(pd - sd);
    cout<<res<<endl;

    return 0;
}
 
PREVIOUS NEXT
Tagged: #calculate #sum #primary #diagonal #matrix #secondary #diagonal #matrix
ADD COMMENT
Topic
Name
5+6 =