#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
C/C++
Что не так с 29 строкой и как это исправить?
Дан массив чисел размерностью 10 элементов.Написать функцию, которая сортирует массив по возрастанию или по убыванию, в зависимости от третьего параметра функции.Если он равен true, сортировка идет по убыванию, если false, то по возрастанию. Первые 2 параметра функции — это массив и его размер, третий параметр по умолчанию равен false.
#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);
}
#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);
}
#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);
}
#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);
}
функция возвращает void, ты пытаешься вывести возвращаемое значение функции, cout не умеет выводить void
Степан Демидов
Огромное спасибо!
Степан Демидов
А что можно сделать с необъявленным идентификатором IsOn?
return EXIT_SUCCESS;
все круто
Похожие вопросы
- Подскажите, почему получается пустая строка и как это исправить.
- Найти максимальный элементы в строке матрицы
- Как реализовать поиск похожей строки в базе данных?
- Записать в файл те строки матрицы, которые содержат только один ноль С++ ПОМОГИТЕ ИСПРАВИТЬ КОД
- Программирование на С++ (строки)
- Двумерный динамический массив с неизвестны количеством столбиков или строк
- Передача строк в функцию С++
- Синтаксическая ошибка: константа строки в с++
- C++: "С-Строка" и ошибка
- С++. Упорядочить строки массива A в порядке убывания сумм цифр первого элемента каждой строки.