Diagonal Difference

  • + 8 comments

    You do not need to have any conditions to complete this problem.

    int main()
    {
        int n;
        int firstDiagonal=0;
        int secondDiagonal=0;
    
        cin >> n;
        int count=n-1;
        vector< vector<int> > a(n,vector<int>(n));
        for(int a_i = 0;a_i < n;a_i++){
           for(int a_j = 0;a_j < n;a_j++){
              cin >> a[a_i][a_j];
           }
        }
    
        for(int i=0; i<n; i++)
        { 
            firstDiagonal+=a.at(i).at(i);
            secondDiagonal+=a.at(count).at(i);
            count--;
        }
    
        cout << abs(firstDiagnol-secondDiagnol);
    
        return 0;
    }
    

    Because the diagonal are always going to be positions we know when we have the size of the matrix.