Массив задан случайным образом на интервале [-31, 45]. Сформировать новый массив В, состоящий из нечетных элементов массива А. Размер произвольный.
(задача на одномерный массив)
Написать код для задачи C++, нужен только код на C++
C/C++
Написать код для задачи C++
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int n,m;
int * A, * B;
do
{
cout << "n: ";
cin >> n;
}while(n < 1);
A = new int[n];
srand(time(nullptr));
rand();
m = 0;
for(int x = 0; x < n; x++)
{
A[x] = rand() % 77 - 31;
cout << A[x] << ' ';
if(A[x] % 2 == 0)m++;
}
cout << endl << endl;
B = new int[m];
for(int x = 0, k = 0; x < n; x++)
{
if(A[x] % 2 == 0)
{
B[k] = A[x];
cout << B[k] << ' ';
k++;
}
}
cout << endl;
if(A != (int*) nullptr)
{
delete [] A;
A = (int*) nullptr;
}
else exit(1);
if(B != (int*) nullptr)
{
delete [] B;
B = (int*) nullptr;
}
else exit(1);
return 0;
}
#include <ctime>
#include <cstdlib>
using namespace std;
int main()
{
int n,m;
int * A, * B;
do
{
cout << "n: ";
cin >> n;
}while(n < 1);
A = new int[n];
srand(time(nullptr));
rand();
m = 0;
for(int x = 0; x < n; x++)
{
A[x] = rand() % 77 - 31;
cout << A[x] << ' ';
if(A[x] % 2 == 0)m++;
}
cout << endl << endl;
B = new int[m];
for(int x = 0, k = 0; x < n; x++)
{
if(A[x] % 2 == 0)
{
B[k] = A[x];
cout << B[k] << ' ';
k++;
}
}
cout << endl;
if(A != (int*) nullptr)
{
delete [] A;
A = (int*) nullptr;
}
else exit(1);
if(B != (int*) nullptr)
{
delete [] B;
B = (int*) nullptr;
}
else exit(1);
return 0;
}
#include <ctime>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
void print_array(vector <int> x, char y)
{ int i, j = 0; cout << "Array " << y << ':' << endl;
for (i = 0; i < x.size(); i++) { j++; cout <<
setw(4) << x[i]; if (j == 30) { j = 0;
cout << endl; } } if (j) cout << endl; }
int main()
{ int i, n; cout << "n: "; cin >> n;
vector <int> A(n), B; srand(time(nullptr));
for (i = 0; i < n; i++) A[i] = rand() % 77 - 31;
print_array(A, 'A'); for (i = 0; i < n; i++)
if (A[i] & 1) B.push_back(A[i]);
print_array(B, 'B'); system("pause > nul");
return 0; }

#include <vector>
#include <cstdlib>
#include <iomanip>
#include <iostream>
using namespace std;
void print_array(vector <int> x, char y)
{ int i, j = 0; cout << "Array " << y << ':' << endl;
for (i = 0; i < x.size(); i++) { j++; cout <<
setw(4) << x[i]; if (j == 30) { j = 0;
cout << endl; } } if (j) cout << endl; }
int main()
{ int i, n; cout << "n: "; cin >> n;
vector <int> A(n), B; srand(time(nullptr));
for (i = 0; i < n; i++) A[i] = rand() % 77 - 31;
print_array(A, 'A'); for (i = 0; i < n; i++)
if (A[i] & 1) B.push_back(A[i]);
print_array(B, 'B'); system("pause > nul");
return 0; }

Геннадий Гончаров
похоже Алекс Юстасу
Похожие вопросы
- Написать код для задачи на C++
- Написал код для языка C, но работает не правильно
- Написать код на языке C++
- Помогите написать код с файлами C++
- Помогите написать код с файлами C++
- Написать код на языке C++
- Надо написать код на языке C 1 курс
- Помогите написать код для задачи на с++
- Как оптимизировать код, чтобы не было превышения по времени к задаче (C++, динамическое программирование)?
- Как оптимизировать код, чтобы не было превышения по времени к задаче (C++)?