Will wrote:
> Hi, I'm trying to take the value of elements in one array and scramble
> them up so that a new array contains a random version of the first
> with out duplicates. I thought the following code would do but I'm not
> geeting the second array filled with the first ones elements.
>
> #include <iostream.h>
> #include <time.h>
> void main()
> {
> int i = 0, j = 0;
> int intSeq[7] = {1,2,3,4,5,6,7};
> int intRand[7] = {0,0,0,0,0,0,0};
>
> for (i = 0; i <= 6; i++)
> {
> while(intRand[j] == 0)
> {
> srand(time(NULL));
> j = random(7);
> intRand[j] = intSeq[i];
> }
> }
> /*From here on is code for output to display*/
> cout << "intSeq[] = ";
> for (i = 0; i <= 6; i++)
> {
> cout << intSeq[i] << " ";
> }
> cout << endl;
> cout << "intRand[] = ";
> for (i = 0; i <= 6; i++)
> {
> cout << intRand[i] << " ";
> }
> cout << endl << endl;
>
> system("pause");//Function for closing salutation.
> }
>
> The output I get from my code shows assignment of one element only,
> does anyone know where I'm going wrong or have an easy scramble
> algotithm?
> Many thanks for reading, Will.
Here is a more C++ish version of the program that works.
#include <iostream>
#include <ctime>
#include <algorithm>
int main()
{
int intSeq[7] = {1,2,3,4,5,6,7};
srand(time(NULL));
std::random_shuffle( intSeq, intSeq + 7 );
std::cout << "intSeq[] = ";
for ( std::size_t i = 0; i <= 6; i++)
{
std::cout << intSeq[i] << " ";
}
std::cout << std::endl;
system("pause");//Function for closing salutation.
}
What you are attempting to write is a shuffle. Shuffles can be a little
difficult because using rand returns a number from 0 to RAND_MAX which may
be something like 32767 or higher. Then you need to convert this to a
unique number from 0 to 6 which is difficult, how do you make sure you
didn't duplicate a number, etc?
Well, the standard template library already has it for you, located in
<algorithm> it is called random_shuffle. You give it the beginning
iterator
(in this case using a simple pointer) and ending interator. Note that the
ending iterator is one PAST the last element.
Other notes: <istream.h> is obsolete, use <iostream>, <time.h> a c header
can be found moved to the stl with <ctime> most (all?) of the standard C
headers can be found by removeing the .h extention and adding a c in
front.
<math.h> becomes <cmath> etc...
I could make this more C++ish by using a std::vector, although vectors can
be a bit of a pain to initialize. My understanding is that the next
version
of C++ will allow vectors to be initilaized with a list (if not they
definately need to be) but currently the program would become:
#include <iostream>
#include <ctime>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> intSeq;
for ( int i = 0; i < 7; ++i )
{
intSeq.push_back( i );
}
srand(time(NULL));
std::random_shuffle( intSeq.begin(), intSeq.end() );
std::cout << "intSeq[] = ";
for ( std::size_t i = 0; i <= 6; i++)
{
std::cout << intSeq[i] << " ";
}
std::cout << std::endl;
system("pause");//Function for closing salutation.
}
Now, for the output we can make it more C++ish, one thing we could chose
to
do is instead of using a count use the size of the vector for the output:
std::cout << "intSeq[] = ";
for ( std::vector<int>::const_iterator it = intSeq.begin(); it !=
intSeq.end(); ++it)
{
std::cout << *it << " ";
}
std::cout << std::endl;
Now, we can even go farther than this. Instead of iterating through the
vector and using std::cout << for each element, we can use std::copy to do
it.
std::cout << "intSeq[] = ";
std::copy( intSeq.begin(), intSeq.end(),
std::ostream_iterator<int>(std::cout, " ") );
std::cout << std::endl;
although now we're getting into the black magic part of C++ where it is a
more advanced topic, but good to learn from the start.
Lastly, you have the system("pause"); in there because you are using
Microsoft Visual C++ and you are running the program with F5 and the
window
closes before you can see the output. Instead of adding the
system("pause"); just use ctrl-F5 when you run the program and it will
pause
for you. So the program becomes:
#include <iostream>
#include <ctime>
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> intSeq;
for ( int i = 0; i < 7; ++i )
{
intSeq.push_back( i );
}
srand(time(NULL));
std::random_shuffle( intSeq.begin(), intSeq.end() );
std::cout << "intSeq[] = ";
std::copy( intSeq.begin(), intSeq.end(),
std::ostream_iterator<int>(std::cout, " ") );
std::cout << std::endl;
}
--
Jim Langston
tazmaster@[EMAIL PROTECTED]


|