How to fill an array with random numbers?
How to write a receiving function some given number(n) generates an array of n-th dimension with numbers from 1 to n, thus these numbers are in random order
For example: input : n=5
output 5 4 3 2 1
input: n=10
output: 6 2 3 1 4 5 7 10 9 8
how to create such a collection?
3 answers
Fill the array with integers from 1 to n. Then swap two elements with random indices.
Search it in Google "shuffle the numbers from 1 to n". Should be at least 4-5 well-known algorithms, but of different complexity.
it is necessary to make the function inside which to create a array and fill it with random numbers ( random function), and return the function will return a pointer to this array.
In the rules that says jobs are not the issue.
Find more questions by tags C++C
int main()
{
std::vector v;
int n;
std::cin >> n;
std::generate_n(std::back_inserter(v), n, []{static int i = 0; return i++;});
std::random_shuffle(v.begin(), V. end());
std::copy(v.cbegin(), V. cend(), std::ostream_iterator(std::cout, " "));
} - nikki_Bashirian commented on September 19th 19 at 12:37