Дан одномерный массив из n элементов, найти max, поменять его местами с первым элементом, результат вывести на экран( Программа в Паскале)

14 Авг 2021 в 19:40
108 +1
0
Ответы
1

program MaxSwap;

var
arr: array of integer;
n, i, maxIndex, tmp: integer;

begin
write('Enter the number of elements in the array: ');
readln(n);

SetLength(arr, n);

// Input elements of the array
writeln('Enter the elements of the array:');
for i := 0 to n - 1 do
readln(arr[i]);

// Find the index of the maximum element
maxIndex := 0;
for i := 1 to n - 1 do
begin
if arr[i] > arr[maxIndex] then
maxIndex := i;
end;

// Swap the maximum element with the first element
tmp := arr[0];
arr[0] := arr[maxIndex];
arr[maxIndex] := tmp;

// Output the result
writeln('Max element swapped with the first element:');
for i := 0 to n - 1 do
write(arr[i], ' ');
end.

17 Апр 2024 в 13:24
Не можешь разобраться в этой теме?
Обратись за помощью к экспертам
Гарантированные бесплатные доработки в течение 1 года
Быстрое выполнение от 2 часов
Проверка работы на плагиат
Поможем написать учебную работу
Прямой эфир