Array without twice the same number?

Reply

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Array without twice the same number?

 
0
  #1
Oct 22nd, 2004
Hi ladies and gentlemen,

Gotta question,

I have a program in wich I make a small array of 100 random numbers wich are smaller then 1000!

Like this:
# define MAX 100

void srand(int tabel[MAX]);

void srand(int tabel[MAX])
{
for (int i=0;i<MAX;i++)
do
tabel[i]=rand();
while (tabel[i]>1000);
}

This of course is only one part of the program and as you can see this array is made with a function! Now, this way I get random numbers but there are several numbers in that array wich are shown twice or even more times, how can I change this program so it doesn't use the same number twice?

Can anyone please help me out?

Thanks in advance!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 10
Reputation: Ejaz is an unknown quantity at this point 
Solved Threads: 0
Ejaz's Avatar
Ejaz Ejaz is offline Offline
Newbie Poster

Re: Array without twice the same number?

 
0
  #2
Oct 22nd, 2004
One solution can be, before inserting the new no. in the array, scan the array (iterate through it), to see whether the no. is already in the array or not, if not, insert it and yes, loop again.

You can either iterate by simple loop or if you manage to create the array in sorted order, then you can use binary search (which is much faster) to serve the purpose.

If you can use STL, then you can use vector and find algorithm, which can make your life much easy.
Regards,

Ejaz.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Array without twice the same number?

 
0
  #3
Oct 22nd, 2004
Hi Ejaz,

Thanks for the quick reply, but can you give me an example how I should write it because that's the problem, I understand that I have to use an iteration to control every single N., but how do I write this combination of iteration and selection

Thanks
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array without twice the same number?

 
0
  #4
Oct 22nd, 2004
The simple and not so great way of iterating through the array to see if it is a duplicate and discarding it may go something like this.
 #include <stdlib.h>
 #include <stdio.h>
 #include <time.h>
 
 #define SIZE 100
 #define MAX  1000
 
 int main(void)
 {
    int array[SIZE];
    size_t i,j;
    srand(time(NULL));
    for ( i = 0; i < sizeof array / sizeof *array; ++i )
    {
 REDO: array[i] = rand() % MAX;
 	  for ( j = 0; j < i; ++j )
 	  {
 		 if ( array[i] == array[j] )
 		 {
 			goto REDO;
 		 }
 	  }
 	  printf("%3d%c", array[i], i % 10 == 9 ? '\n' : ' ');
    }
    return 0;
 }
 
 /* my output
 138 327 659 825 356 700 248 195 615 253
 318 123 215 333 396 813 196 230 376 725
 139 446 985 816 383 427 557 411 523 277
 270 943 869 200 690 154 345 912 239  94
 937 374 882 665 762 938 681 956 961 673
 838 119 526 234 549 932 304 579 617 755
 578 409 543 766 727 571 471  48 574 394
 614 973 421 528 142 804 115 530 338 788
 691 688 883 878 887 350 661  64 998 749
 572 216 513  89 441 243 952 209 468 959
 */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Array without twice the same number?

 
0
  #5
Oct 22nd, 2004
Thanks very much Dave :p

This way I can see how you did it, and find a way to do it in my programm

Got a few questions tough, 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 :!:

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

2) sizeof array / sizeof *array, can you give me a brief explanation why you divide these

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

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

Anyway, thanks again for the example, it's going to help alot, if you don't have time to explain these questions, I understand :!:
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Array without twice the same number?

 
0
  #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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Array without twice the same number?

 
0
  #7
Oct 23rd, 2004
I really had trouble in finding the solution, Ive tried to find a solution on my own, but somehow I just couldn't see how I had to do it :-|

The srand ( , I saw this a bit to late :lol:

I don't only know how to solve the problem now, but also understand why you did it, thanks again :!:
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC