Написать программу ,которая с клавиатуры запрашивает размер двумерного массива найти и вывести на экран сумму значений элементов расположенных в левом верхнем углу матрицы и правом нижним???Pascal
var arr: array of array of Integer; rows, cols, i, j: Integer; sum1, sum2: Integer;
begin Write('Enter the number of rows: '); ReadLn(rows); Write('Enter the number of columns: '); ReadLn(cols);
SetLength(arr, rows, cols);
for i := 0 to rows - 1 do begin for j := 0 to cols - 1 do begin Write('Enter element at position (', i, ', ', j, '): '); ReadLn(arr[i, j]); end; end;
sum1 := 0; sum2 := 0;
for i := 0 to rows - 1 do begin for j := 0 to cols - 1 do begin if i = j then sum1 := sum1 + arr[i, j]; if i + j = rows - 1 then sum2 := sum2 + arr[i, j]; end; end;
WriteLn('Sum of elements in the top left diagonal: ', sum1); WriteLn('Sum of elements in the bottom right diagonal: ', sum2);
program SumOfDiagonals;
var
arr: array of array of Integer;
rows, cols, i, j: Integer;
sum1, sum2: Integer;
begin
Write('Enter the number of rows: ');
ReadLn(rows);
Write('Enter the number of columns: ');
ReadLn(cols);
SetLength(arr, rows, cols);
for i := 0 to rows - 1 do
begin
for j := 0 to cols - 1 do
begin
Write('Enter element at position (', i, ', ', j, '): ');
ReadLn(arr[i, j]);
end;
end;
sum1 := 0;
sum2 := 0;
for i := 0 to rows - 1 do
begin
for j := 0 to cols - 1 do
begin
if i = j then
sum1 := sum1 + arr[i, j];
if i + j = rows - 1 then
sum2 := sum2 + arr[i, j];
end;
end;
WriteLn('Sum of elements in the top left diagonal: ', sum1);
WriteLn('Sum of elements in the bottom right diagonal: ', sum2);
end.