Thread: Rearranging contents of an array

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    85

    Rearranging contents of an array

    I have a program to randomly pick 90 questions from a large database(295), so what I did is made an index array of ints of ints labelled 1-90, but I want the array to not have duplicates.

    Code:
    	srand(time(0));
    	for(int i=0;i<MAXQ;i++)
    		randoms[i] = rand()%295;
    theres a little snippit.
    I then sort the random array, but I get duplicates of an index. How can I arrange 295 questions into 90 but without duplicates?

    Thanks

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    You could try something like this:

    Code:
    int j = 0;
    for(int i = 0; i < MAXQ; i++) {
       randoms[i] = rand() % 295;
    		
       while(j < i) {
          if (randoms[i] == randoms[j])
             randoms[i] = rand() % 295;
          else 
             j++;
       }
    }
    Sent from my iPadŽ

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    I'm guessing this int array stores Identifiers for reading the database. You could check to see if one has already come up easily.

    Code:
    //!!!! FILL randoms WITH -1!!!!
    
    srand(time(0));
    int number = 0;
    bool picked = false;
    
    for(int i=0; i<MAXQ; i++)
    {
        number = rand()%295;
        for( int j=0; j<i; j++ )
        {
            if( randoms[j] == number )
                picked = true;
         }
    
         if( picked == true )
             i--; // will go through loop again
         else
             randoms[i] = number;
         
          picked = false
    }
    Last edited by durban; 11-07-2005 at 01:30 AM.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Code:
    #include <vector>
    #include <algorithm>  // for random_shuffle
    using namespace std;
    
    int main()
    {
    	vector<int> randoms(295);
    
    	/* Generate a vector containing the numbers 0..294 */
    	for (vector<int>::size_type i = 0; i < randoms.size(); i++) randoms[i] = i;
    
    	/* Shuffle */
    	random_shuffle(randoms.begin(), randoms.end());
    
    	/* Keep only the first 90 numbers */
    	randoms.resize(90);
    }

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    sweet, thanks guys, all good now.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    random_shuffle(). Nice.

    If you didn't want to destroy your vector with the 295 numbers, you could modify the program slightly:
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>  // for random_shuffle
    using namespace std;
    
    int main()
    {
    	vector<int> randoms(295);
    
    	/* Generate a vector containing the numbers 0..294 */
    	for (vector<int>::size_type i = 0; i < randoms.size(); i++) 
    		randoms[i] = i;
    
    	/* Shuffle */
    	random_shuffle(randoms.begin(), randoms.end());
    
    	/* Keep only the first 90 numbers */
    //	randoms.resize(90);
    
    	vector<int> ninety(90);
    	vector<int>::iterator begin = randoms.begin();
    	copy(begin, begin + 90, ninety.begin());  //(beginning of range, end of range, destination)
    	
    	//output the numbers to the screen:
    	copy(ninety.begin(), ninety.end(), ostream_iterator<int>(cout, "\n"));
    	
    	return 0;
    }
    Last edited by 7stud; 11-07-2005 at 06:14 AM.

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    As a reminder, you should still call srand at the beginning of your program when using the random_shuffle function. The random_shuffle function makes use of rand internally and if you don't call srand you will wind up with the same sequence of "random" values each time the program is run.

    [edit]Resizing the vector or copying the first 90 elements into another container after the shuffle doesn't hurt but it is unnecessary. You could just simply iterate/loop over the first 90 values and then just stop at that point.[/edit]
    Last edited by hk_mp5kpdw; 11-07-2005 at 06:22 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Also, while vectors are definitely preferred, random_shuffle works on C style arrays as well:
    Code:
    random_shuffle(randoms, randoms + 295); // assuming randoms has a size of 295

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    85
    what i came up with the help of u guys...
    Code:
    srand(time(0)); //generate random int array
    	for(int i=0;i<MAXQ;i++)
    	{
    		number = rand()%295;
    		for(int j=0;j<i;j++)
    		{
    			if(randoms[j] == number)
    				picked = true;
    		}
    		if(picked)
    			 i--; // will go through loop again
    		else
    			randoms[i] = number;
    		picked = false;
    	}
    sorted = new int[ASSIGNED];
    	for(int i=0;i<ASSIGNED;i++) //get 90 questions from random array
    		sorted[i] = randoms[i];
    	Bubblesort(sorted,ASSIGNED);// sort it

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    That method should work, but it is very inefficient. If you don't want to use the built-in solution of random_shuffle, then you can use the same technique it uses.

    Create an index variable and start it at the end (which is MAXQ-1). Get a random number from 0 to that index. Then swap the value at that index with the value at the index of that random number. Decrement the index and repeat the process until the index is 0.

    This will effectively shuffle the array without the inefficiency of looking up previously picked numbers. You can then use the first 90 numbers of the array (you can also modify the algorithm to only shuffle 90 numbers, but that is harder so you should start with the basic algorithm).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Getting the contents of edit boxes into an array
    By earth_angel in forum Windows Programming
    Replies: 3
    Last Post: 07-05-2005, 12:17 PM
  3. Using the debugger to see array contents
    By JohnnyCat in forum C++ Programming
    Replies: 2
    Last Post: 06-23-2005, 02:17 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  5. Code: An auto expanding array (or how to use gets() safely).
    By anonytmouse in forum Windows Programming
    Replies: 0
    Last Post: 08-10-2004, 12:13 AM