I have an array of 4 numbers and I'm having trouble checking for duplicates. I had the impression that if i randomize numbers for arr[i], then for arr[j], I can compare them for duplicates and randomize again if true. How do I go about checking that element of the array is not repeated/duplicated?
int arr[4];
srand(time(0));
for(int i=0; i<4; i++)
{
arr[i] = rand() % 10;
for(int j=i+1; j<4; j++)
{
arr[j] = rand() % 10;
if(arr[j] == arr[i])
{
arr[i] = rand() % 10;
}
}
}
for(int i=0; i<4; i++)
cout << arr[i] << endl;
Aucun commentaire:
Enregistrer un commentaire