Given a square matrix of order N. In the matrix, calculate the arithmetic mean of the positive
Given a square matrix of order N. In the matrix, calculate the arithmetic mean of the positive elements on the main diagonal.
program zz1;
const n = 4; // set the number of elements in the array
var i, j, k, s: integer; // set variables of integer type
a: array [1..n, 1..n] of integer; // set a two-dimensional array, n by n
begin
for i: = 1 to n do
for j: = 1 to n do // get random numbers and write them into an array
a [i, j]: = random (101) -50;
for i: = 1 to n do // display the resulting array on the screen
begin
for j: = 1 to n do
write (a [i, j]: 4);
writeln;
end;
for i: = 1 to n do
for j: = 1 to n do
if (a [i, j]> 0) and (i = j) then begin k: = k + 1; s: = s + a [i, j]; end; // check if the array element is positive and it is on the main diagonal, then we count it and add it to the sum
writeln (‘the arithmetic mean of positive elements on the main diagonal =’, s / k); // display the answer
end.