alyavladimirov1
23.05.2021 22:49

Как отсортировать массив после максимального элемента подскажите . с++.

Нажмите на рекламу ниже и сразу увидите ответ
Популярные вопросы:
Ответ:
Kybikowskatana
09.10.2020 10:19
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

template <typename T>
void swap(T &a, T&b)
{
T t=a;
a=b;
b=t;
}

void printArray(int *array, int length)
{
for (int i=0;i<length;i++)
std::cout<<std::setw(4)<<array[i];
std::cout<<std::endl;
}

int main()
{
srand(time(0));
int length;
std::cin>>length;
int *array=new int[length];
for (int i=0;i<length;i++)
array[i]=rand()%100+1;
printArray(array,length);
int posMax=0;
for (int i=1;i<length;i++)
if (array[posMax]<array[i])
posMax=i;
bool flag=false;
while (flag==false)
{
flag=true;
for (int i=posMax+2;i<length;i++)
if (array[i]<array[i-1])
{
flag=false;
swap(array[i],array[i-1]);
}
}
printArray(array,length);
return 0;
}
0,0(0 оценок)
Полный доступ
Позволит учиться лучше и быстрее. Неограниченный доступ к базе и ответам от экспертов и ai-bota Оформи подписку
logo
Начни делиться знаниями
Вход Регистрация
Что ты хочешь узнать?
Спроси ai-бота