Написать программу на Pascal использую процедуры и функции 1. Вычислить сумму элементов массива, расположенных между первым и последним положительными элементами. 2. Преобразовать массив таким образом, чтобы сначала располагались элементы, равные 0, а затем – остальные
procedure readArray(var a: TArray); var i: integer; begin writeln('Enter ', N, ' elements:'); for i := 1 to N do begin readln(a[i]); end; end;
function sumBetweenPositiveElements(a: TArray): integer; var i, firstPos, lastPos, sum: integer; begin firstPos := 0; lastPos := 0; sum := 0;
// Find indexes of first and last positive elements for i := 1 to N do begin if (a[i] > 0) and (firstPos = 0) then firstPos := i; if a[i] > 0 then lastPos := i; end;
// Calculate sum of elements between first and last positive elements if (firstPos = 0) or (lastPos = 0) then begin sum := 0; // If no positive elements found, sum is 0 end else begin for i := firstPos + 1 to lastPos - 1 do begin sum := sum + a[i]; end; end;
sumBetweenPositiveElements := sum; end;
procedure moveZeroesToFront(var a: TArray); var i, tmp, firstNonZeroIndex: integer; begin firstNonZeroIndex := 1;
// Move zeroes to the front of the array for i := 1 to N do begin if a[i] = 0 then begin tmp := a[firstNonZeroIndex]; a[firstNonZeroIndex] := a[i]; a[i] := tmp; firstNonZeroIndex := firstNonZeroIndex + 1; end; end; end;
procedure printArray(a: TArray); var i: integer; begin writeln('Array elements:'); for i := 1 to N do begin write(a[i], ' '); end; writeln; end;
begin readArray(arr);
writeln('Sum between first and last positive elements: ', sumBetweenPositiveElements(arr));
program ArrayManipulations;
const
N = 10;
type
TArray = array[1..N] of integer;
var
arr: TArray;
procedure readArray(var a: TArray);
var
i: integer;
begin
writeln('Enter ', N, ' elements:');
for i := 1 to N do
begin
readln(a[i]);
end;
end;
function sumBetweenPositiveElements(a: TArray): integer;
var
i, firstPos, lastPos, sum: integer;
begin
firstPos := 0;
lastPos := 0;
sum := 0;
// Find indexes of first and last positive elements
for i := 1 to N do
begin
if (a[i] > 0) and (firstPos = 0) then
firstPos := i;
if a[i] > 0 then
lastPos := i;
end;
// Calculate sum of elements between first and last positive elements
if (firstPos = 0) or (lastPos = 0) then
begin
sum := 0; // If no positive elements found, sum is 0
end
else
begin
for i := firstPos + 1 to lastPos - 1 do
begin
sum := sum + a[i];
end;
end;
sumBetweenPositiveElements := sum;
end;
procedure moveZeroesToFront(var a: TArray);
var
i, tmp, firstNonZeroIndex: integer;
begin
firstNonZeroIndex := 1;
// Move zeroes to the front of the array
for i := 1 to N do
begin
if a[i] = 0 then
begin
tmp := a[firstNonZeroIndex];
a[firstNonZeroIndex] := a[i];
a[i] := tmp;
firstNonZeroIndex := firstNonZeroIndex + 1;
end;
end;
end;
procedure printArray(a: TArray);
var
i: integer;
begin
writeln('Array elements:');
for i := 1 to N do
begin
write(a[i], ' ');
end;
writeln;
end;
begin
readArray(arr);
writeln('Sum between first and last positive elements: ', sumBetweenPositiveElements(arr));
moveZeroesToFront(arr);
printArray(arr);
end.