В вашей программе должно быть создано четыре одномерных динамических массива A, B, C, D.
Количество элементов в массивах A, B задайте с клавиатуры (для каждого). Массивы заполните случайными числами в диапазоне от -20 до +20.
В массив C запишите все положительные числа из массивов A и B.
В массив D запишите все отрицательные числа из массивов A и B.
Выведите на экран содержимое массивов A, B, C, D.
Освободите память, занимаемую массивами A, B, C, D.
C/C++
Помогите пожалуйста - создать 4 массива на c++
#include "iostream"
#include "algorithm"
#include "ctime"
using namespace std;
int main()
{
int asize, bsize;
std::cin >> asize >> bsize;
// соданиие массивов a b
int* a = new int[asize];
int* b = new int[bsize];
// случайные элементы
srand(time(NULL));
generate(a,a+asize,[]() { return rand()%41-20; } );
generate(b,b+bsize,[]() { return rand()%41-20; } );
// подсчет числа отрицательных в обоих массивах
int neg =
count_if(a,a + asize,[](int x) { return x<0; } ) +
count_if(b,b + bsize,[](int x) { return x<0; } );
// создание массивов c d
int* c = new int[asize+bsize-neg];
int* d = new int[neg];
// копирование отрицательных и положительных чисел в массивы d и с
int ic = 0, id = 0;
for_each(a,a + asize,[&](int x) { x<0 ? d[id++]=x :c[ic++] = x;} );
for_each(b,b + bsize,[&](int x) { x<0 ? d[id++]=x :c[ic++] = x;} );
// вывод всех массивов
for_each(a,a+asize,[](int x) { cout<< x <<' '; } ); std::cout << "\n";
for_each(b,b+bsize,[](int x) { cout<< x <<' '; } ); std::cout << "\n";
for_each(c,c+ic,[](int x) { cout<< x <<' '; } ); std::cout << "\n";
for_each(d,d+id,[](int x) { cout<< x <<' '; } ); std::cout << "\n";
// освобождение памяти
delete[]a; delete []b; delete[]c; delete[]d;
}
#include "algorithm"
#include "ctime"
using namespace std;
int main()
{
int asize, bsize;
std::cin >> asize >> bsize;
// соданиие массивов a b
int* a = new int[asize];
int* b = new int[bsize];
// случайные элементы
srand(time(NULL));
generate(a,a+asize,[]() { return rand()%41-20; } );
generate(b,b+bsize,[]() { return rand()%41-20; } );
// подсчет числа отрицательных в обоих массивах
int neg =
count_if(a,a + asize,[](int x) { return x<0; } ) +
count_if(b,b + bsize,[](int x) { return x<0; } );
// создание массивов c d
int* c = new int[asize+bsize-neg];
int* d = new int[neg];
// копирование отрицательных и положительных чисел в массивы d и с
int ic = 0, id = 0;
for_each(a,a + asize,[&](int x) { x<0 ? d[id++]=x :c[ic++] = x;} );
for_each(b,b + bsize,[&](int x) { x<0 ? d[id++]=x :c[ic++] = x;} );
// вывод всех массивов
for_each(a,a+asize,[](int x) { cout<< x <<' '; } ); std::cout << "\n";
for_each(b,b+bsize,[](int x) { cout<< x <<' '; } ); std::cout << "\n";
for_each(c,c+ic,[](int x) { cout<< x <<' '; } ); std::cout << "\n";
for_each(d,d+id,[](int x) { cout<< x <<' '; } ); std::cout << "\n";
// освобождение памяти
delete[]a; delete []b; delete[]c; delete[]d;
}
Ещё вариант:
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
void print_array(int *x, int size, char y)
{ cout << "\nArray " << y << ':' << endl;
int i, j = 0; for (i = 0; i < size; i++) { j++;
cout << setw(4) << x[i]; if (j == 10)
{ cout << endl; j = 0; } } if (j) cout << endl; }
int negatives(int *x, int size)
{ int i, j = 0; for (i = 0; i < size; i++) if (x[i] < 0)
j++; return j; }
int positives(int *x, int size)
{ int i, j = 0; for (i = 0; i < size; i++) if (x[i] > 0)
j++; return j; }
int main()
{ int a, b, c, d, i, j = 0, k = 0; cout << "a b: ";
cin >> a >> b; srand(time(NULL));
int *A = new int [a], *B = new int [b];
for (i = 0; i < a; i++) A[i] = rand() % 41 - 20;
for (i = 0; i < b; i++) B[i] = rand() % 41 - 20;
print_array(A, a, 'A'); print_array(B, b, 'B');
c = positives(A, a) + positives(B, b);
d = negatives(A, a) + negatives(B, b);
int *C = new int [c], *D = new int [d];
for (i = 0; i < a; i++) { if (A[i] > 0) { C[j] = A[i];
j++; } if (A[i] < 0) { D[k] = A[i]; k++; } }
for (i = 0; i < b; i++) { if (B[i] > 0) { C[j] = B[i];
j++; } if (B[i] < 0) { D[k] = B[i]; k++; } }
print_array(C, c, 'C'); print_array(D, d, 'D');
delete [] A; delete [] B; delete [] C; delete [] D; }

#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
void print_array(int *x, int size, char y)
{ cout << "\nArray " << y << ':' << endl;
int i, j = 0; for (i = 0; i < size; i++) { j++;
cout << setw(4) << x[i]; if (j == 10)
{ cout << endl; j = 0; } } if (j) cout << endl; }
int negatives(int *x, int size)
{ int i, j = 0; for (i = 0; i < size; i++) if (x[i] < 0)
j++; return j; }
int positives(int *x, int size)
{ int i, j = 0; for (i = 0; i < size; i++) if (x[i] > 0)
j++; return j; }
int main()
{ int a, b, c, d, i, j = 0, k = 0; cout << "a b: ";
cin >> a >> b; srand(time(NULL));
int *A = new int [a], *B = new int [b];
for (i = 0; i < a; i++) A[i] = rand() % 41 - 20;
for (i = 0; i < b; i++) B[i] = rand() % 41 - 20;
print_array(A, a, 'A'); print_array(B, b, 'B');
c = positives(A, a) + positives(B, b);
d = negatives(A, a) + negatives(B, b);
int *C = new int [c], *D = new int [d];
for (i = 0; i < a; i++) { if (A[i] > 0) { C[j] = A[i];
j++; } if (A[i] < 0) { D[k] = A[i]; k++; } }
for (i = 0; i < b; i++) { if (B[i] > 0) { C[j] = B[i];
j++; } if (B[i] < 0) { D[k] = B[i]; k++; } }
print_array(C, c, 'C'); print_array(D, d, 'D');
delete [] A; delete [] B; delete [] C; delete [] D; }

Похожие вопросы
- Как создать гигантский массив в C++?
- Помогите пожалуйста написать программу небольшую в C++. Одномерный массив
- Помогите пожалуйста составить правильную программу на C++ 12 вариант
- Помогите, пожалуйста, исправить ошибку в коде (C++).
- Здравствуйте, помогите пожалуйста написать программы на языке C++.
- Помогите, пожалуйста, нужно написать программу на C++
- Помогите пожалуйста, нужно сделать программу на C++
- Помогите пожалуйста сделать задачу по программированию C++
- Помогите, пожалуйста решить на C++, используя многомерные статические массивы
- C++ | Структуры и массивы структур. Помогите пожалуйста!
Все верно!