Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C - C++ Learning > Random array of...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 11 Topic 4058 of 4218
Post > Topic >>

Random array of unique numbers

by Will <wburchell@[EMAIL PROTECTED] > Mar 7, 2008 at 07:29 PM

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.
 




 11 Posts in Topic:
Random array of unique numbers
Will <wburchell@[EMAIL  2008-03-07 19:29:36 
Re: Random array of unique numbers
"osmium" <r1  2008-03-07 11:41:47 
Re: Random array of unique numbers
Francis Glassborow <fr  2008-03-08 00:24:16 
Re: Random array of unique numbers
Will <wburchell@[EMAIL  2008-03-08 11:47:19 
Re: Random array of unique numbers
Ian Collins <ian-news@  2008-03-08 08:45:47 
Re: Random array of unique numbers
"Jim Langston"   2008-03-07 23:10:35 
Re: Random array of unique numbers
Richard Heathfield <rj  2008-03-08 09:17:25 
Re: Random array of unique numbers
Will <wburchell@[EMAIL  2008-03-08 12:06:07 
Re: Random array of unique numbers
Francis Glassborow <fr  2008-03-08 12:28:18 
Re: Random array of unique numbers
Francis Glassborow <fr  2008-03-08 12:25:33 
Re: Random array of unique numbers
stan <smoore@[EMAIL PR  2008-03-08 15:45:52 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 19:25:18 CDT 2008.