Writing an algorithm for matrix

366 Views Asked by At

I've been given a task to write an algorithm and it's scheme and, honestly, I'm really stuck, don't know steps i need to take firstly. All I'm asking is to put me on a road to solve this.

The task is : There is $n \times n$ matrix. You need to find how many positive elements are there over the diagonal and then find their sum and average.

1

There are 1 best solutions below

0
On

If we suppose that your matrix $A$ is indexed by row and columns with $A_{ij}$ the element with row $i$ and column $j$, then the elements over the diagonal are the ones such that $j \geq i$.

You can do a two nested loops with $i$ from $1$ to $n$ and then $j$ from $i$ to $n$.

For example, in pseudo-code, to compute the sum of all elements over the diagonal if $A_{ij}$ is accessed by A[i,j]:

sum = 0
for i from 1 to n
    for j from i to n
        sum = sum + A[i,j]
    endfor
endfor