Const n=20; type ar=array[1..n] of integer; procedure p1(var a:ar); var i:integer; begin for i:=1 to n do begin a[i]:=random(11)-5; write(a[i],' '); end; writeln; end; function sum(var a:ar):integer; var s,i:integer; begin s:=0; for i:=1 to n do if a[i]>0 then s:=s+a[i]; sum:=s; end; var a:ar; p:real; begin Randomize; p1(a); writeln('s = ',sum(a)); end.
type ar=array[1..n] of integer;
procedure p1(var a:ar);
var i:integer;
begin
for i:=1 to n do begin
a[i]:=random(11)-5;
write(a[i],' ');
end;
writeln;
end;
function sum(var a:ar):integer;
var s,i:integer;
begin
s:=0;
for i:=1 to n do
if a[i]>0 then s:=s+a[i];
sum:=s;
end;
var a:ar; p:real;
begin
Randomize;
p1(a);
writeln('s = ',sum(a));
end.
Пример:
2 -4 -1 3 -2 -5 -1 5 -5 2 -1 3 3 -4 0 4 2 -1 -4 -4
s = 24