Hi there,

I'm writing a function that fills the array with numbers from 1 - 52 with no repetitions basically my main problem is the second set of loops or the nested loops are, check to see if it is repeated and if it is it puts a new number, now i have it so it changes it but i put a statement that resets 'i' so that it rechecks the whole thing to double check theres no repetitions, and that puts it into an infinite loop. Any help will be greatly appreciated. Here is the code

``````#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int fillDeck(int arry1[52]);

int main(){
int Deck[52];

fillDeck(Deck);

return 0;
}

int fillDeck(int arry1[52]){
srand ( time(NULL) );

for(int i = 0; i < 52; i++){

int num1 = (rand() % 52) + 1;
arry1[i] = num1;
}

for(int i = 0; i < 52; i++){
for(int j = 0; j < 52; j++){
if(arry1[i]==arry1[j]){
arry1[i] = (rand() % 52) + 1;
i = 0;
}
}

}

for(int i = 0; i < 52; i++){

printf("%d\n", arry1[i]);

}
return 0;
}``````
3
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by mikecolistro

To fill the array with unique int's from 1 to 52, just use:

``````for(i=0;i<52;i++)
array[i] = i+1;``````

Then you can swap random indeces around, until it's all shuffled

``````for(i=0;i<someNumberAtLeast25;i++) {
temp = array[randomInt];
array[randomInt] = array[randomInt2];
array[randomInt2] = temp;
}``````

Give that a try. ;)

If the poster will search this section, he'll find a post that shows how to randomly fill an array with unique values...The post was recent(1 - 7 days ago).

Edited by gerard4143: n/a

Thats a great idea, i cant believe i didnt think of that , lol thank you very much