How to sum the corners of a matrix in Pascal

2 Answers

0 votes
program SumMatrixCorners;

const
  Rows = 3;
  Cols = 4;

var
  A: array[1..Rows, 1..Cols] of Integer =
    ((1, 2, 3, 4),
     (5, 6, 7, 8),
     (9,10,11,12));
  Sum: Integer;

begin
  Sum := A[1,1] +
         A[1,Cols] +
         A[Rows,1] +
         A[Rows,Cols];

  Writeln('Sum of corners = ', Sum);
end.



{
run:

Sum of corners = 26

}

 



answered Oct 11 by avibootz
0 votes
program SumMatrixCorners;

{$mode objfpc}{$H+}

type
  TMatrix = array[1..3, 1..4] of Integer;

function SumCorners(const A: TMatrix): Integer;
begin
  Result := A[1, 1] +
            A[1, 4] +
            A[3, 1] +
            A[3, 4];
end;

var
  M: TMatrix =
    ((1, 2, 3, 4),
     (5, 6, 7, 8),
     (9,10,11,12));

begin
  Writeln('Sum of corners = ', SumCorners(M));
end.



{
run:

Sum of corners = 26

}

 



answered Oct 11 by avibootz
...