C/C++

Что не так с 29 строкой и как это исправить?

Дан массив чисел размерностью 10 элементов.Написать функцию, которая сортирует массив по возрастанию или по убыванию, в зависимости от третьего параметра функции.Если он равен true, сортировка идет по убыванию, если false, то по возрастанию. Первые 2 параметра функции — это массив и его размер, третий параметр по умолчанию равен false.

 #include 

#include

#include

#include

using namespace std;

void sortIncrease(int a[], const int length, bool IsOn = false) {

for (size_t i = 0; i < length; i++)

{

for (size_t j = 0; j < length; j++)

{

if (a[i] > a[j]) {

a[i] = a[j] + a[i];

a[j] = a[i] - a[j];

a[i] = a[i] - a[j];

}

}

cout
#include <iostream>

#include <random>

#include <time.h>

#include <windows.h>

using namespace std;

void sortIncrease(int a[], const int length, bool IsOn = false) {

for (size_t i = 0; i < length; i++)
{
for (size_t j = 0; j < length-1; j++)
{
if (a[j] < a[j+1] ^ IsOn) //в этом месте IsOn инвертирует сравнение
{
swap(a[j], a[j + 1]);
}
}
}
}
void printarray(const int a[], const int length)
{
for (int i = 0; i < length; i++)
cout << a[i] << " ";
}

int main() {

const int length = 10;

int UserChoice;

int a[length] = { 1,3,5,2,8,11,9,4,15,19 };

cout << "Choose sorting metod:\n1-> increasing\n0->decreasing" << endl;

cin >> UserChoice;

sortIncrease(a, length, UserChoice);

printarray(a, length);

}
Александр Ильченко
Александр Ильченко
51 416
Лучший ответ
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
void usort(int* box, size_t length, bool status) {
if (status) sort(box, box + length);
else sort(box, box + length, greater<>());
}
void show(int* box, const size_t length) {
for (size_t i = 0; i < length; ++i) {
cout << box[i] << ' ';
}
puts("");
}
int main() {
int box[]{ 1, 3, 5, 2, 8, 11, 9, 4, 15, 19 };
const auto length = size(box);
show(box, length);
cout << "Status: ";
bool status = cin.get() == '1';
usort(box, length, status);
show(box, length);
}
СЕ
Саша Есин
95 542
функция возвращает void, ты пытаешься вывести возвращаемое значение функции, cout не умеет выводить void
Степан Демидов Огромное спасибо!
Степан Демидов А что можно сделать с необъявленным идентификатором IsOn?
return EXIT_SUCCESS;
все круто
Said Abbasov
Said Abbasov
8 899