#include#include
void push_pack(int *&arr, int& size, const int val)
void bubbleSort(int* arr, const int size)
int getRandomNumber(const int min, const int max)
int main()
{
using namespace std;
setlocale(LC_ALL, "ru");
srand(time(NULL));
int N = 10, M = 0;
int* parr_1 = new int[N];
int* parr_2 = new int[M];
cout << "Начальный массив: " << endl;
for (int i = 0; i < N; i++)
parr_1[i] = getRandomNumber(-30, 30);
cout << parr_1[i] << '\t';
if (parr_1[i] < 0) push_pack(parr_2, M, parr_1[i]);
}
cout << "\n\nОтсортированный массив: " << endl;
bubbleSort(parr_1, N);
cout << "\n\nМассив с отрицательными числами: " << endl;
for (int i = 0; i < M; i++)
cout << parr_2[i] << '\t';
delete[] parr_1;
delete[] parr_2;
return 0;
void push_back(int *&arr, int& size, const int val)
int *newArr = new int[size + 1];
for (int i = 0; i < size; i++)
newArr[i] = arr[i];
newArr[size++] = val;
delete[] arr;
arr = newArr;
static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0);
return static_cast<int>(rand() * fraction * (max - min + 1) + min);
int temp = 0;
for (int i = 0; i < size - 1; i++)
for (int j = 0; j < size - 1; j++)
if (arr[j + 1] < arr[j])
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
#include#include
void push_pack(int *&arr, int& size, const int val)
void bubbleSort(int* arr, const int size)
int getRandomNumber(const int min, const int max)
int main()
{
using namespace std;
setlocale(LC_ALL, "ru");
srand(time(NULL));
int N = 10, M = 0;
int* parr_1 = new int[N];
int* parr_2 = new int[M];
cout << "Начальный массив: " << endl;
for (int i = 0; i < N; i++)
{
parr_1[i] = getRandomNumber(-30, 30);
cout << parr_1[i] << '\t';
if (parr_1[i] < 0) push_pack(parr_2, M, parr_1[i]);
}
cout << "\n\nОтсортированный массив: " << endl;
bubbleSort(parr_1, N);
for (int i = 0; i < N; i++)
cout << parr_1[i] << '\t';
cout << "\n\nМассив с отрицательными числами: " << endl;
for (int i = 0; i < M; i++)
cout << parr_2[i] << '\t';
delete[] parr_1;
delete[] parr_2;
return 0;
}
void push_back(int *&arr, int& size, const int val)
{
int *newArr = new int[size + 1];
for (int i = 0; i < size; i++)
newArr[i] = arr[i];
newArr[size++] = val;
delete[] arr;
arr = newArr;
}
int getRandomNumber(const int min, const int max)
{
static const double fraction = 1.0 / (static_cast<double>(RAND_MAX) + 1.0);
return static_cast<int>(rand() * fraction * (max - min + 1) + min);
}
void bubbleSort(int* arr, const int size)
{
int temp = 0;
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; j < size - 1; j++)
{
if (arr[j + 1] < arr[j])
{
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
}
}