uses crt;
const nmax = 100; {Nr maxim de elemente}
type matrice = array[1..nmax, 1..nmax] of integer;
var a: matrice;
    i, j, m, n: word;
    
function suma: longint;
var s: longint;
begin
    s := 0;
    for i := 1 to n do   {Parcurgerea liniilor} 
      for j := 1 to m do {Parcurgerea coloanelor} 
        inc(s, a[i, j]); {la s se aduna a[i]}
    suma := s;
end;

function max(var im, jm: word): integer;
var m: integer; 
    i, j: word;
begin
  im := 1; jm := 1;
  m := a[im, jm];
  for i := 1 to n do   {Parcurgerea liniilor} 
    for j := 1 to m do {Parcurgerea coloanelor}
      if a[i,j] > a[im,jm] then begin
        im := i; {Indicii elementului maxim}
        jm := j;
      end; 
end;    

function nr_pare: integer;
var p: integer;
begin
   p := 0;
  for i := 1 to n do   {Parcurgerea liniilor} 
    for j := 1 to m do {Parcurgerea coloanelor}
      if a[i, j] mod 2 = 0 then inc(p); {Daca restul impartirii la 2 este 0, atunci nr este par}
   nr_pare := p;  
end;

function media: real;
begin
  media := suma / (m+n);
end;

begin
  clrscr;    {Curatirea ecranului}
  randomize; {Pentru obtinerea numerelor aleatoare}
  writeln('Scrie nr de elemente (intre 1 si ', nmax, '): ');
  write('Coloane: '); readln(m);
  write('Linii:   '); readln(n);
  writeln('Doresti sa generez ', m+n, ' numere? (Apasa "D" sau "N")');
  if UpCase(ReadKey) = 'D' then  {UpCase transforma caracterul citit de ReadKey in majuscula}
    for i := 1 to n do 
      for j := 1 to m do 
        a[i, j] := random(MAXINT)     {Un numar aleator intre 0 si MAXINT}
  else
    for i := 1 to n do
      for j := 1 to m do begin 
        write('a[', i, ',' , j, '] = '); 
        readln(a[i, j]);
      end;

    clrscr;
    
    writeln('Tabloul a:');
    for i := 1 to n do begin
      for j := 1 to m do write(a[i, j]:6);
      writeln;
    end;  
    
    writeln;
    writeln;
    writeln('Suma: ');
    writeln(suma:10);
    writeln;
    writeln('Elentul maxim: ');
    max(i, j);
    writeln('   a[', i, ',', j, '] = ', a[i, j]);
    writeln;
    writeln('Nr elemente pare: ');
    writeln(nr_pare:10);
    writeln;
    writeln('Media: ');
    writeln(media:13:2);
    
    readkey;  
end.
