# Question about matrix and diagonals [closed]

I have to write a program in C++ to determinate the following:

1. Sum of main diagonal

2. Sum of elements above main diagonal

3. Sum of elements below main diagonal

4. Sum of secondary diagonal

5. Sum of elements above secondary diagonal

6. Sum of elements below secondary diagonal

So, I wanna firstly see how does it solve in math.

I know the following:

1. It has to equal i == j to be true. That is easy.

2. If i < j

3. If i > j.

4. Not sure

5. Not sure

6. Not sure

Any help is appreciated.

-

## closed as off-topic by Stefan Smith, Git Gud, Kaz, TMM, ShuchangDec 25 '13 at 4:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question is not about mathematics, within the scope defined in the help center." – Stefan Smith, Git Gud, Kaz, TMM, Shuchang
If this question can be reworded to fit the rules in the help center, please edit the question.

Hint: Wrote out a 2x2, then 3x3, the 4x4 using $a_{11}, a_{12} ... a_{nn}$ and see if you can figure out the pattern. – Amzoti Dec 25 '13 at 1:59
@Amzoti I did, but it's kinda hard... i'm still trying to see. – user117293 Dec 25 '13 at 2:02
I guess your attempt for the solution assumes that you have a loop over all row and columns (that is, $n^2$ loop iterations). It is, IMHO, a very bad solution if $n$ is somewhat large. – Algebraic Pavel Dec 25 '13 at 2:19
@AlgebraicPavel In my case it's not large, but I don't know any better way either, I'm a very beginner... also, one question off-question, how do you calculate the complexly of a problem? Like whether it's n, logn or n^2? – user117293 Dec 25 '13 at 2:21
1 and 4 should be implemented using one loop, they have $O(n)$ complexity and $O(n^2)$ for the rest (hint: how many elements you have to sum in the given $n\times n$ matrix to get the result?) – Algebraic Pavel Dec 25 '13 at 2:27

Assuming that $i$ is the "row" and the $j$ is the "column", and assuming that the input is a square matrix...

1. i == j
2. i < j
3. i > j
4. i+j == n-1 (where $n$ is the number of rows/columns of the matrix)
5. i+j < n-1
6. i+j > n-1

-
btw, I'm assuming that the matrix is a standard double array in C++, with indices starting at $0$ instead of $1$. If the indices are $1$ to $n$ instead of $0$ to $n-1$, change all $n-1$s into $n+1$. – 2012ssohn Dec 25 '13 at 2:05
Hm. Are you sure about the 4th one?? – user117293 Dec 25 '13 at 2:06
Ok, I get it, about the n-1 and n+1 part, and as for the 4th one I think it's good as you said, except in programming way it's n - j - 1. (where j is the column) – user117293 Dec 25 '13 at 2:10
i == n-j-1 and i+j == n-1 are equivalent expressions, so as long as you use the one that fits your purpose better, it should be the same. I just chose the i+j because it looks nicer. – 2012ssohn Dec 25 '13 at 2:13
Wow.. well, you're right. I just learned something new here! Thanks for that. I appreciate your help and time! Merry Christmas. – user117293 Dec 25 '13 at 2:20

I will let you do the declarations etc and give you the core functionality.

Assuming A is $N\times N$ and you use standard zero indexing

for (i=0; i $<$ N; i++) diagsum += A[i][i];

for (i=0; i $<$N; i++) secondarydiagsum += A[i][N-i-1];

for (i=0; i $<$ N; i++) for (j=i+1;j$<$N;j++) abovesum += A[i][j]; //i is row, j is column

for (i=0; i $<$N; i++) for (j=0;j$<$i;j++) belowsum += A[i][j]; //i is row, j is column

for (i=0; i $<$N; i++) for (j=0;j$<$ N-i-1;j++) abovesecondarysum += A[i][j];

for (i=0; i $<$N; i++) for (j=N-i;j$<$ N;j++) belowsecondarysum += A[i][j];

Please check the limits. I am having a hard time formatting C++ code here

-