Написать программы в Паскале• Дан массив из N вещественных чисел. Поменять местами последний и минимальный элементы массива. • Дан массив из N целых чисел. Найти номер первого элемента, который равен количеству элементов, кратных 3.
program SwapMinAndLastElement; const N = 5; var arr: array[1..N] of real; i, minIndex, lastIndex: integer; min: real; begin writeln('Enter ', N, ' real numbers:'); for i := 1 to N do readln(arr[i]); min := arr[1]; minIndex := 1; for i := 2 to N do begin if arr[i] < min then begin min := arr[i]; minIndex := i; end; end; lastIndex := N; // Swap arr[minIndex] := arr[N]; arr[N] := min; writeln('Array after swapping:'); for i := 1 to N do writeln(arr[i]); end.
Пример программы на Паскале для второй задачи:
program FindFirstElementEqualToCountOfMultiplesOf3; const N = 7; var arr: array[1..N] of integer; i, countOfMultiplesOf3, resultIndex: integer; begin writeln('Enter ', N, ' integers:'); for i := 1 to N do readln(arr[i]); countOfMultiplesOf3 := 0; for i := 1 to N do begin if arr[i] mod 3 = 0 then countOfMultiplesOf3 := countOfMultiplesOf3 + 1; if arr[i] = countOfMultiplesOf3 then begin resultIndex := i; break; end; end; if countOfMultiplesOf3 > 0 then writeln('Number of elements that are multiples of 3: ', countOfMultiplesOf3) else writeln('No elements found that are multiples of 3.'); if resultIndex > 0 then writeln('Index of the first element equal to the count of multiples of 3: ', resultIndex) else writeln('No element found equal to the count of multiples of 3.'); end.
Пример программы на Паскале для первой задачи:
program SwapMinAndLastElement;const
N = 5;
var
arr: array[1..N] of real;
i, minIndex, lastIndex: integer;
min: real;
begin
writeln('Enter ', N, ' real numbers:');
for i := 1 to N do
readln(arr[i]);
min := arr[1];
minIndex := 1;
for i := 2 to N do
begin
if arr[i] < min then
begin
min := arr[i];
minIndex := i;
end;
end;
lastIndex := N;
// Swap
arr[minIndex] := arr[N];
arr[N] := min;
writeln('Array after swapping:');
for i := 1 to N do
writeln(arr[i]);
end.
Пример программы на Паскале для второй задачи:
program FindFirstElementEqualToCountOfMultiplesOf3;const
N = 7;
var
arr: array[1..N] of integer;
i, countOfMultiplesOf3, resultIndex: integer;
begin
writeln('Enter ', N, ' integers:');
for i := 1 to N do
readln(arr[i]);
countOfMultiplesOf3 := 0;
for i := 1 to N do
begin
if arr[i] mod 3 = 0 then
countOfMultiplesOf3 := countOfMultiplesOf3 + 1;
if arr[i] = countOfMultiplesOf3 then
begin
resultIndex := i;
break;
end;
end;
if countOfMultiplesOf3 > 0 then
writeln('Number of elements that are multiples of 3: ', countOfMultiplesOf3)
else
writeln('No elements found that are multiples of 3.');
if resultIndex > 0 then
writeln('Index of the first element equal to the count of multiples of 3: ', resultIndex)
else
writeln('No element found equal to the count of multiples of 3.');
end.