Search
 
SCRIPT & CODE EXAMPLE
 

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;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: irremoteesp8266 example 
Cpp :: convert ros time to double 
Cpp :: amusia 
Cpp :: jquery ajax post json asp.net core 
Cpp :: how to get the numbers in a vector c++ sfml 
Cpp :: lower bound c++ 
Cpp :: Casino Number Guessing Game - C++ 
Cpp :: auto i cpp 
Cpp :: sort c++ array 
Cpp :: fenwick tree 
Cpp :: return function in cpp 
Cpp :: convert from hex to decimal c++ 
Cpp :: Find first and last digit of int 
Cpp :: can derived class access private members 
Cpp :: set elements to 42 back 
Cpp :: how to implement binders and decorators on c++ lik python? 
C :: malloc is undefined 
C :: How to install npm in alpine linux 
C :: how to make a hello world program in c 
C :: react-textfit 
C :: restart nginx in alpine linix 
C :: is it possible to access argv in function 
C :: cannot get / react router dom 
C :: c round function 
C :: c assign pointer to struct 
C :: C read a character 
C :: CL/cl.h: No such file or directory 
C :: set value of boolean in c 
C :: multiplication table in c 
C :: sqlserver insert with set identity 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =