random () and srand()

F

free4ziyi

Hi,

My function should takes an array containing the set of all integers in
the range of [a,b] and shuffles it into random order.

For example, a = 1, b = 5, n = 3 and the random orders:
1.[3,4,2,1,5] 2.[2,5,1,4,3] 3.[1,4,2,3,5]

Then the most similar pair of sequences is 1 and 3, since they have the
same value in 3 position.

My problem is should I do to declare the array without knowing the
array size? The compiler keeps getting error because I declare and
array like int array[];

Can anyone help me with this?

Cheers!

J
 
N

Neil Cerutti

My problem is should I do to declare the array without knowing the
array size? The compiler keeps getting error because I declare and
array like int array[];

Can anyone help me with this?

std::vector<int> is ready and willing to help you. Also, standing
over by the bar, is std::shuffle, looking forlorn.
 
K

Kai-Uwe Bux

Hi,

My function should takes an array containing the set of all integers in
the range of [a,b] and shuffles it into random order.

For example, a = 1, b = 5, n = 3 and the random orders:
1.[3,4,2,1,5] 2.[2,5,1,4,3] 3.[1,4,2,3,5]

Then the most similar pair of sequences is 1 and 3, since they have the
same value in 3 position.

I do not understand the above description of the problem. What exactly is
the input (a set of integers, or just the numbers a and b)? What exactly is
the output (a vector, an array)? And how does n enter the picture? Also,
what is this notion of similarity that all of a sudden pops up.

My problem is should I do to declare the array without knowing the
array size? The compiler keeps getting error because I declare and
array like int array[];

Try using std::vector. Those critters known their size.

Can anyone help me with this?

Most certainly. But it will be easier if you show the code you have.



Best

Kai-Uwe Bux
 
S

Sebastian Redl

Neil said:
std::vector<int> is ready and willing to help you. Also, standing
over by the bar, is std::shuffle, looking forlorn.

Also, std::fill_n and boost::count_iterator, for a geeky way to create the
sequence.
 
M

Marcus Kwok

My function should takes an array containing the set of all integers in
the range of [a,b] and shuffles it into random order.

For example, a = 1, b = 5, n = 3 and the random orders:
1.[3,4,2,1,5] 2.[2,5,1,4,3] 3.[1,4,2,3,5]

Then the most similar pair of sequences is 1 and 3, since they have the
same value in 3 position.

My problem is should I do to declare the array without knowing the
array size? The compiler keeps getting error because I declare and
array like int array[];

Can anyone help me with this?

In C++, the size of an array must be a compile-time constant. In C99,
they added Variable Length Arrays (VLAs), but that has not become
standard in C++ yet.

You could try using a std::vector<int> (so you do not have to worry
about the size), and then have a look at the random_shuffle() function
in <algorithm>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top