Задача:
Массив из 10 целых чисел заполнить случайными числами от -5 до 5. Произвести сортировку массива. Вывести на экран исходный и отсортированный массивы. Сортировать по убыванию прямым выбором
Другие языки программирования и технологии
Нужен код программы, C++
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
typedef unsigned int index_t;
void choice(int *, const size_t);
void show(int *, const size_t);
int * random(int *, const size_t, const int = 0, int = 10);
int * create(const size_t);
int main() {
size_t const size = 10;
int * vector = NULL;
vector = random(vector, size, -5, 6);
if (vector) {
show(vector, size);
choice(vector, size);
show(vector, size);
delete[] vector;
}
cin.get();
return 0;
}
void choice(int * _vector, const size_t _size) {
index_t k;
int x;
for (index_t n = 0; n < _size; n++) {
k = n;
x = _vector[n];
for (index_t m = n + 1; m < _size; m++) {
if (_vector[m] > x) {
k = m;
x = _vector[m];
}
}
_vector[k] = _vector[n];
_vector[n] = x;
}
}
int * create(const size_t _size) { return new (nothrow) int [_size]; }
int * random(int * _vector, const size_t _size, const int _left, int _rigth) {
srand(unsigned(time(NULL)));
if (!_vector) _vector = create(_size);
if (_vector) {
if (_left < 0 && _rigth >= 0) _rigth += abs(_left);
else if (_left < 0 && _rigth < 0) _rigth = abs(_left) - abs(_rigth);
else if (_left >= 0 && _rigth > 0) _rigth -= _left;
for (index_t n = 0; n < _size; n++) _vector[n] = rand() % _rigth + _left;
} else exit(1);
return _vector;
}
void show(int * _v, const size_t _size) {
index_t n = 0;
do cout << setw(3) << _v[n]; while (++n < _size);
cout << endl;
}
#include <iomanip>
#include <ctime>
using namespace std;
typedef unsigned int index_t;
void choice(int *, const size_t);
void show(int *, const size_t);
int * random(int *, const size_t, const int = 0, int = 10);
int * create(const size_t);
int main() {
size_t const size = 10;
int * vector = NULL;
vector = random(vector, size, -5, 6);
if (vector) {
show(vector, size);
choice(vector, size);
show(vector, size);
delete[] vector;
}
cin.get();
return 0;
}
void choice(int * _vector, const size_t _size) {
index_t k;
int x;
for (index_t n = 0; n < _size; n++) {
k = n;
x = _vector[n];
for (index_t m = n + 1; m < _size; m++) {
if (_vector[m] > x) {
k = m;
x = _vector[m];
}
}
_vector[k] = _vector[n];
_vector[n] = x;
}
}
int * create(const size_t _size) { return new (nothrow) int [_size]; }
int * random(int * _vector, const size_t _size, const int _left, int _rigth) {
srand(unsigned(time(NULL)));
if (!_vector) _vector = create(_size);
if (_vector) {
if (_left < 0 && _rigth >= 0) _rigth += abs(_left);
else if (_left < 0 && _rigth < 0) _rigth = abs(_left) - abs(_rigth);
else if (_left >= 0 && _rigth > 0) _rigth -= _left;
for (index_t n = 0; n < _size; n++) _vector[n] = rand() % _rigth + _left;
} else exit(1);
return _vector;
}
void show(int * _v, const size_t _size) {
index_t n = 0;
do cout << setw(3) << _v[n]; while (++n < _size);
cout << endl;
}
за 300р решу
#include<locale.h>
#include <math.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
//функция случайного ввода
void random_enter(int a[] , int b){
srand(time(NULL));
for(int i=0;i<b;i++)>=-5+rand() % 5;
}
//функция вывода на экран
void print(int a[] , int b){
for(int i=0;i<b;i++)>);
}
};
//функция сортировки по убыванию методом пузырька (обменами)
void sort(int a[] , int b){
int i,j,temp;
for(i=0;i<b;i++){>i;j--){
if(a[j-1] < a[j]){
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp; }
}}
};
//главная функция програмы
int main(int argc, char* argv[]){
setlocale(LC_ALL,"Russian");
int A[10] ;
random_enter(A,10);
printf(\n Исходный массив: \n);
print(A,10);
sort(A,10);
printf(\n Отсортированный массив: \n);
print(A.10);
getch();
return 0;
}
#include <math.h>
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
//функция случайного ввода
void random_enter(int a[] , int b){
srand(time(NULL));
for(int i=0;i<b;i++)>=-5+rand() % 5;
}
//функция вывода на экран
void print(int a[] , int b){
for(int i=0;i<b;i++)>);
}
};
//функция сортировки по убыванию методом пузырька (обменами)
void sort(int a[] , int b){
int i,j,temp;
for(i=0;i<b;i++){>i;j--){
if(a[j-1] < a[j]){
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp; }
}}
};
//главная функция програмы
int main(int argc, char* argv[]){
setlocale(LC_ALL,"Russian");
int A[10] ;
random_enter(A,10);
printf(\n Исходный массив: \n);
print(A,10);
sort(A,10);
printf(\n Отсортированный массив: \n);
print(A.10);
getch();
return 0;
}
Похожие вопросы
- помогите пожалуйста кто смыслит в C# или в C++, очень нужна ваша помощь, нужен код программы, точнее двух
- Необходим код программы, C++
- Нужен код программы для Осуществления циклического сдвига элементов прямоугольной матрицы на n элементов вправо в с#
- Помогите исправить ошибку в программе (c++).
- Исправление кода на C++
- Как в C# вызвать событие из кода программы?
- Для чего нужны консольные программы, и что означает system("pause") в C++ ?
- Помогите пожалуйста исправить код программы на visual c++!!
- Помогите откорректировать код в C++
- Помогите пожалуйста перевести код программы с C++ на Си.