Thread: Array Fillin Question

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

    Array Fillin Question

    I have written a function to fillin a array with random numbers within a given range, say 1-16. any of the empty elments (dim*dim - need_num) should have the value 0 in them. The way I have written the function put all the empty elements (0s) at the end of the array.

    But empty elements (0s) should be randomly distributed inside the array. I am pretty lost at this stage. I would really appreciate any help. Thanks in adavnce!

    Code:
    void rand_gen(int need_num, int **array, int dim){
    
    	int n, m, *temp_array;
    	int i=0, b, g, new1 ;
    	int q,w,h;
    	temp_array = (int *)malloc((need_num+1) * sizeof(int));
    	
    	for(h=0;h<(need_num+1);h++){
    		temp_array[h]=0;}
      
    	srand(time(NULL));
    
    	for (i=0;i<(need_num);i++){
    	b=rand()%(need_num)+1;   // random number within need_num
    				
    	//temp storing the rand # in a array temp
    		new1 = 1;
    		for(g=0;g<i;g++)
    		     if(temp_array[g]==b){new1 = 0;}
    			if (new1 == 1){temp_array[i]=b;}
    			    else	i--;
    
    
    			}
    
    	//initialize array to 0 
    	
    	 for(q=0;q<dim;q++){
    	      for(w=0;w<dim;w++){
    		array[q][w] = 0;}}
    
                    //filling actual array,
    	for(n=0;n<dim;n++){
    		for(m=0;m<dim;m++){
    			if(i<need_num){
    			array[n][m]=temp_array[i];
    			i++;
    			}
    			
    		}
    	}
    }
    Sample out put
    dim*dim=16
    need_num=13


    Code:
    1     3     2   4 
    8     6     7   10
    13   11   9    12  
    5      0    0    0
    //I have numbers from 1-12 here but 0s at the end
    //I want sothing like
    1     3     0    4 
    8     0     7   10
    13   11   9    12  
    5      2    6   0
    //So 0s are randomly distributed with other numbers
    Last edited by ypramesh; 04-07-2006 at 04:15 PM.

  2. #2
    tall guy
    Join Date
    Aug 2005
    Location
    in bed
    Posts
    4
    I would suggest:
    1. Give each element in the array a random number between 1 and 16
    2. Pick a random element
    3. If this element is already 0 go back to previous step
    4. set this element to 0
    5. repeat from step 2 until you have enough 0s.


    If you are processing large lists with many 'empties' (0s), it may be more efficent to assign every element to 1, then pick the elements and replace each '1' with a random number.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well the first problem is learning how to format code - random indentation and arbitrary placement of braces does not make for easy to follow code.

    Eg, this is yours reformatted
    Code:
    void rand_gen(int need_num, int **array, int dim)
    {
      int n, m, *temp_array;
      int i = 0, b, g, new1;
      int q, w, h;
    
      temp_array = (int *) malloc((need_num + 1) * sizeof(int));
      for (h = 0; h < (need_num + 1); h++) {
        temp_array[h] = 0;
      }
    
      srand(time(NULL));
    
      for (i = 0; i < (need_num); i++) {
        b = rand() % (need_num) + 1;  // random number within need_num
    
        //temp storing the rand # in a array temp
        new1 = 1;
        for (g = 0; g < i; g++)
          if (temp_array[g] == b) {
            new1 = 0;
          }
        if (new1 == 1) {
          temp_array[i] = b;
        } else
          i--;
      }
    
      //initialize array to 0
      for (q = 0; q < dim; q++) {
        for (w = 0; w < dim; w++) {
          array[q][w] = 0;
        }
      }
    
      //filling actual array,
      for (n = 0; n < dim; n++) {
        for (m = 0; m < dim; m++) {
          if (i < need_num) {
            array[n][m] = temp_array[i];
            i++;
          }
        }
      }
    }
    > temp_array = (int *) malloc((need_num + 1) * sizeof(int));
    1. you cast the result of malloc - see the FAQ for why this is bad
    2. you don't free it at the end - this is a memory leak
    3. you don't need it anyway.

    > srand(time(NULL));
    Typically, you do this ONCE at the beginning of main.
    If you called this code within the same second, you're going to end up with exactly the same results.

    Try this approach
    Code:
    void rand_gen(int need_num, int **array, int dim)
    {
      int i, r, c;
    
      /* fill with zeros */
      for ( r = 0 ; r < dim ; r++ ) {
        for ( c = 0 ; c < dim ; c++ ) {
          array[r][c] = 0;
        }
      }
    
      /* put 1..need_num in random places */
      for ( i = 1 ; i <= need_num ; i++ ) {
        r = rand() % dim;   /* pick a spot */
        c = rand() % dim;
        if ( array[r][c] == 0 ) {
          array[r][c] = i;  /* it's empty, fill it */
        }
      }
    }
    A better way would be to just put 1..need_num in the first positions of the matrix (and fill the rest with zeros), then implement a shuffle.
    A nice easy exercise for you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 07:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 06:27 PM