Writing unique numbers to an array

This is a discussion on Writing unique numbers to an array within the C Programming forums, part of the General Programming Boards category; Hi, Am writing this program to write only unique random numbers to an array. The randomly generated numbers isnt the ...

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    Writing unique numbers to an array

    Hi,
    Am writing this program to write only unique random numbers to an array. The randomly generated numbers isnt the problem, its writing only unique numbers to the array thats a bother. Any ideas?

    Code:
    /*A program to generate random numbers and then write them to an array ONLY IF no such number exists in the array*/
    
    #include <stdio.h> 
    #include <stdlib.h> 
    
    int GetRand(int min, int max);
    
    int const MAX=5;
    
    int main(void)
    {
      int i, r, j,x;
      int array[MAX];
    
      for (i = 0; i < MAX; i++)
      {
       	r = GetRand(0, 4); //generate random numbers btwn the 2 numbers indicated
      	printf ("the random number is %d\n", r);
       
    	if(i == 0 ) //writing first generated # to the array automatically for obvious reasons
    	{
    		array[0]=r;
    	 	printf("array[0]: %d\n\n",array[0]);/*this line works*/
    	}//end of if statement
    
    	if( i > 0 )//comparing from the 2nd # & upwards generated to whats in the array already
    	{
    		for( j=1; j<MAX+1; j++ )
    		{
    			
    			if(r == array[j-1])
    			{
    				i=i-1;
    				break;
    			}
    			else x=1;
    
    
    		}
    		
    		if( x = 1 )
    		array[j]=r;
    				
    	}//end of if statement
      }//end of for loop
    
    	
      
      return(0);
    }
    
    
    int GetRand(int min, int max) //function to generate random numbers
    {
    	static int Init = 0;
    	int rc;
    	
    	if (Init == 0)
    	{
    	
    	srand(time(NULL));
    	Init = 1;
    	}
    	
    	
    	rc = (rand() % (max - min + 1) + min);
    	
    	return (rc);
    }//end of GetRand function

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    11
    below 'for' loop is the culprit

    Code:
    if( i > 0 )//comparing from the 2nd # & upwards 
    {
        for( j=1; j<MAX+1; j++ )

  3. #3
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Make an array of n numbers containing the numbers 1 to n. Then, from there, randomly shuffle that array. Then take the amount of number you want from there.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    What exactly is wrong with the for loop? Or is it in the wrong place?

  5. #5
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    If j is equal to max+1, array[j] will cause a seg fault. But you should stick to what I told you and search the boards. This question has been answered a million times already.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    if( x == 1 )
         array[j]=r;
    looks like u have over complicated the code. The problem is just straight forward, follow Happy_Reaper post.

    ssharish2005

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    Thanks guys. I got everything working. The completed code might help somebody else:

    Code:
    /*A program to generate random numbers and then write them to an array ONLY IF no such number already exists in the array*/
    
    #include <stdio.h> 
    #include <stdlib.h> 
    
    int GetRand(int min, int max);
    
    int const MAX = 10;
    int i;
    int numAlreadyFound = 0;
    int r;
    
    int main()
    {
    	int unique[MAX];
    
    	//the first r is automatically entered into unique[0];
    	r = GetRand(0, 9);
    	unique[0] = r;
    	numAlreadyFound++;
    	
    	
    	//as long as total number is less than MAX
    	while(numAlreadyFound < MAX)
    	{
    	//generate new r
    	r = GetRand(0, 9);
    	
    	//check each value in array against current r value
    	for(i = 0; i < numAlreadyFound ; ++i)
    	{
    	//if current r already exists in the array
    	if(unique[i] == r)
    	//stop looking at values in array 
    	i = 1000;
    	}
    	
    	//if current r not found in array
    	if(i == numAlreadyFound)
    	//add it to the end of the values already found and increment 
    	//numAlreadyFound
    	unique[numAlreadyFound++] = r;
    	}
    	
    	//print the elements of the array
    	for( i = 0;i < MAX; i++ )
    	printf("elements of unique are:%d\n",unique[i]);
    
    }
    
    int GetRand(int min, int max) //function to generate random numbers
    {
    	static int Init = 0;
    	int rc;
    	
    	if (Init == 0)
    	{
    	
    	srand(time(NULL));
    	Init = 1;
    	}
    	
    	
    	rc = (rand() % (max - min + 1) + min);
    	
    	return (rc);
    }//end of GetRand function

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Filling an array with random numbers
    By euclid in forum C Programming
    Replies: 21
    Last Post: 11-28-2007, 05:53 PM
  2. Writing an array of struct to file
    By stellastarr in forum C Programming
    Replies: 10
    Last Post: 03-25-2006, 05:59 PM
  3. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21