User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 422,410 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,966 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums

Array without twice the same number?

Join Date: Apr 2004
Posts: 3,613
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 17
Solved Threads: 142
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array without twice the same number?

  #6  
Oct 22nd, 2004
Originally Posted by JoBe
I don't want to just copy your solution without knowing why you did certain things and what the meaning of some of them is :!:
Bless you.

Originally Posted by JoBe
1) srand(time(NULL)); Ive read in a tutorial on this forum about it, does this make certain that the random numbers are allways different? Why are there two )'s at the end, think this was not intentional right
srand(time(NULL));

Originally Posted by JoBe
2) sizeof array / sizeof *array, can you give me a brief explanation why you divide these
To get the number of elements. Total array size in bytes divided by the individual element size in bytes yields the number of elements.



Originally Posted by JoBe
3) REDO, I understand the English words and can see what it does, but can you tell me what it is Meaning, could I use another word to get my variable into the iteration again IT's probably and most certainly the goto that does it right, but just want to be certain
It is the label that the goto goes to.
REDO: array[ i ] =rand() % MAX;
      	  for ( j = 0; j < i; ++j )
      	  {
      		 if ( array[ i ] == array[ j ] )
       		 {
       			goto REDO;
       		 }
       

Originally Posted by JoBe
4) When you use array[ j ], this is just the next number in the array wich comes after [ i ] correct and could you use something like this:

if ( array[ i ] == array[ i ] -1) because this way you are referring to the previous number in the array aswell correct
The counter j goes from zero to the current value of i.
   for ( i = 0; i < sizeof array / sizeof *array; ++i )
           {
        REDO: array[ i ] = rand() % MAX;
        	  for ( j = 0; j < i; ++j )
 	 {
Just think of and example case when you're partway through. Let's say i is 40: you've already filled up array[0] to array[39] and just obtained a new candidate for array[40]. You'll want to compare array[0], array[1], array[2], ... to make sure that the current pick for array[40] isn't the same as any of the previous ones. If array[40] is the same as array[22], you'll want to redo the random value.
Reply With Quote  
All times are GMT -4. The time now is 9:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC