Thread: HELP:::MIX numbers in array

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    22

    HELP:::MIX numbers in array

    I am doing a hw assignment which is a puzzle game and i came to this problem.
    I have a 2d array and i have a set of numbers lets say (ex:1-100)
    I need to place these numbers randomely in the array.

    I initially tried to user srand() and rand() to fill the array with numbers upto 100 but it repeat numbers some times. so it doesn't work.

    This is what I have now. Thankx in advance guys. have a good day
    Code:
    
    	for (i=0;i<need_num;i++)
        {
    		srand(i);
            b=rand()%need_num;   // random number with in need_num
    
    		//temp storing the rand # in a array temp
    		
    				temp_array[i]=b;
    		
    	}
    	
    	i=0;
    
    	//filling actual array, we leav the emptyspaces  as 0's at the end of the 
    //array
    
    	for(n=0;n<dim;n++){
    		for(m=0;m<dim;n++){
    
    			array[n][m]=temp_array[i];
    			i++;
    		}
    	}
    
    }
    Last edited by ypramesh; 03-30-2006 at 05:00 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Repeat this for every number you need to enter:
    1 - Start walking through the array.
    2 - Count all empty squares as you go.
    3 - When your empty square count matches some random number, based on the total remaining empty squares, fill this square with the number you need to enter.

    This is a personal favorite method of mine for dealing with random insertion in arrays. I'm not sure why I like it so, but I just do.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    sorry I am totaly lost didn't get what you said.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Given an array big enough to hold all of the numbers we want to put into it, do the following:

    First, pick one of the numbers you wish to add from your list of "numbers to add". If all you're doing is inserting a consecutive list of numbers, just start at one end of your list of numbers, and work your way to the other end. To clarify: If you want to insert 1 to 100, start at either end, and go until you've inserted them all. To further clarify, using the example I just mentioned, start at 1, and insert, incrementing one at a time, until you've inserted number 100. In code, it might be as simple as:
    Code:
    for( x = 1; x < 101; x++ )
    This will start with number 1, and work all the way through the numbers you're inserting, through 100.

    Next, pick a random spot to insert it. In code, again, as simple as:
    Code:
    where = rand() % numbersleft;
    Here we pick a number from zero through the number of remaining numbers to insert. Naturally this will involve some form of counter to keep track how many we have left to insert. Depending on how you choose to do this, you can use your same counter above, with a slight tweak. Or just use a seperate one.

    Now we insert the number. To do this, walk through your array from start to finish, making note of how many you pass that are empty. Now that implies that you have some way to know if it's empty or not. Here, we might just set them all to zero first, and if it's non-zero, it's used. When our count reaches our random number above, we've reached our destination, so we'll use that spot. In code, it may be something like this:
    Code:
    for( rcount = walk = 0; walk < sizeof array / sizeof array[0]; walk++ )
    {
        if( array[ walk ] == 0 )
            rcount++;
        if( rcount == where )
            array[ where ] = x;
    }
    Something like that.

    Pick the number to insert. Pick where to insert it. Walk down the list until you find where to insert it. Insert it there.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    that rand function will give some repeating numbers. lets say I have 1-100 and i want to use rand to pick numbers randomley but it doesn't work because it will repeat some numbers with in 1-100. this happen even if i seed the rand with time

    thank you again for helping

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're not understanding what I'm doing. I'm not using rand to give me a number. I'm using it to pick where I put it.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    thats true i get that part. but if rand repeats then we will put two numbers in a one spot?? sorry i am doing it lab now and its driving me crazy

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    thanx so much for taking time to reply

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It can't repeat, because I'm not simply using the number as an index, I'm using the number to start from the beginning of the array, and walk down it, counting the empty (unused) spots. When my count reaches the same number as the "where" spot, then I put it there. Here:
    Code:
    int array[ 5 ] = { 0 };
    ...
    where = 2;
    
    array[ 0 ] == empty, so "where count = 1"
    array[ 1 ] == empty, so "where count = 2"
    since where == where count, we now know to stop looking, and use this spot.
    
    where = 2
    
    array[ 0 ] == empty, so "where count = 1"
    array[ 1 ] != empty, so "where count = 1" still. We can't use this, so don't count it.
    array[ 2 ] == empty, so "where count = 2"
    since where == where count, we now know to stop looking, and use this spot.
    I didn't say it was easy, or the best way. I said it was my favourite way.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    22
    is there any other ways to tackle this prob?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To Find Largest Consecutive Sum Of Numbers In 1D Array
    By chottachatri in forum C Programming
    Replies: 22
    Last Post: 07-10-2011, 01:43 PM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. getting numbers in an array
    By ct26torr in forum C Programming
    Replies: 6
    Last Post: 03-04-2003, 10:31 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM