Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
387 views
in Technique[技术] by (71.8m points)

c++ - calculate the sum of diagonals in a matrix

I need to calculate the sum of two diagonals in a matrix in C++, I already have a solution for that but I must be dumb because I cant understand what it is doing, so I would like to know if there is another version which I can understand. here is the code which does the job:

cout<<"Jepi rangun e  matrices"<<endl;  // pra bejme manipulim me matrice katrore ku rreshtat=kolonat
cin>>n;
cout<<"Tani jepi elementet e matrices"<<endl; // lexohet matrica

for(i=1;i<=n;i++)
{
     for(j=1;j<=n;j++)
        cin>>a[i][j];
}

d=0;
s=0; // ketu e keni kushtin si dhe mbledhjen per te dy diagonalet me dy variabla te ndryshme

for(i=1;i<=n;i++)
    for(j=1;j<=n;j++)
    {
        if(i==j)
            d=d+a[i][j];
        if(j==n-i+1 || i==n-j+1) 
            s=s+a[i][j];
    }

The part that is difficult to understand is

if(j==n-i+1 || i==n-j+1) 
    s=s+a[i][j];

Here is the entire code that I changed but it doesnt work for the secondary diagonal:

#include <iostream>
using namespace std;

int main()
{
    int d=0,s=0; // ketu e keni kushtin si dhe mbledhjen per te dy diagonalet me dy variabla te ndryshme
    int i,j,n;
    int a[5][5];

    cout<<"Jepi rangun e  matrices"<<endl;  // pra bejme manipulim me matrice katrore ku rreshtat=kolonat
    cin>>n;
    cout<<"Tani jepi elementet e matrices"<<endl; // lexohet matrica

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            cin>>a[i][j];
    }

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(i==j) 
                d+=a[i][j]; //principal diagonal 
            if(i+j==n-1)
                s+=a[i][j];//secondary diagonal

        }
    }

    cout << d << endl;
    cout << s << endl;
    cin.get();
    cin.get();
    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It would be nice to have comments in English, but, the your code does (second loop):

browse all rows
  browse all cells
    if i == j (is in main diagonal):
        increase one sum
    if i == n - i + 1 (the other diagonal)
        increase the second sum

The much nicer and much more effective code (using n, instead of n^2) would be:

for( int i = 0; i < n; i++){
   d += a[i][i];  // main diagonal
   s += a[i][n-i-1]; // second diagonal (you'll maybe need to update index)
}

This goes straight trough the diagonals (both at the one loop!) and doesn't go trough other items.

EDIT:

Main diagonal has coordinates {(1,1), (2,2), ..., (i,i)} (therefor i == j).

Secondary diagonal has coordinates (in matrix 3x3): {(1,3), (2,2),(3,1)} which in general is: {(1,n-1+1), (2, n-2+1), ... (i, n-i+1), .... (n,1)}. But in C, arrays are indexed from 0, not 1 so you won't need that +1 (probably).

All those items in secondary diagonal than has to fit condition: i == n - j + 1 (again due to C's indexing from 0 +1 changes to -1 (i=0,, n=3, j=2, j = n - i - 1)).

You can achieve all this in one loop (code above).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...