Answer the question
In order to leave comments, you need to log in
C++ - how to fill a dynamic array with elements less than 6 from another dynam. array?
You need to enter a sequence of numbers and create a dynamic array, and then form another array, in which there will be elements of the first array, only less than 6. I made a random number generator that generates new numbers when it starts. There is a counter that is equal to the number of entered numbers and a counter for the 2nd array. All this to create these dynamos. arrays.
Now the problem itself: I don’t know how, in the FillNewArray function, to implement filling the second array with elements of the first one, the values of which are less than 6. I attach the code below:
#include "stdafx.h"
#include <iostream>
#include <ctime>
using namespace std;
int FillNewArray(int Array[], int count, int LesArray[], int Lesser)
{
for (int i = 0; i < count; i++)
{
if (Array[i] < 6)
{
LesArray[i] = Array[i];
}
}
return LesArray[Lesser];
}
void ShowArray(int Array[], int count)
{
for (int i = 0; i < count; i++)
{
cout << Array[i] << "\t";
}
}
int main()
{
srand(time(0));
int count;
int Lesser = 0;
cout << "Input the size of your array: ";
cin >> count;
int *Array = new int[count];
for (int i = 0; i < count; i++)
{
Array[i] = 10 - rand() % 20;
if (Array[i] < 6)
{
Lesser++;
}
}
cout << "The array: ";
ShowArray(Array, count);
int *LesArray = new int[Lesser];
FillNewArray(Array, count, LesArray, Lesser);
cout << "\nChanged array: ";
ShowArray(LesArray, Lesser);
system("pause>>void");
return 0;
}
Answer the question
In order to leave comments, you need to log in
int FillNewArray(int Array[], int count, int LesArray[], int Lesser)
{
int j = 0;
for (int i = 0; i < count; i++)
{
if (Array[i] < 6)
{
LesArray[j++] = Array[i];
}
}
return LesArray[Lesser];
}
Do you have C++:
#include <algorithm>
...
std::copy_if(std::begin(srcArray), std::end(srcArray), std::begin(dstArray),
[](auto && elem) { return elem < 6; });
back_inserter
в случае вектора может быть. Кстати, в С++ надо использовать vector
или array
.Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question