I need to load an array with random integers, however, the numbers need to be unique.
The size of the array can vary based on user input, so using a static "if array[0] == array[1]", etc., is not going to work. I'm assuming I will need to do this with a for loop, I'm just not sure how to accomplish this.
I'm really not sure how I would use "count". I've never used it before.
kfex,
Not working. I gets stuck in an endless loop. The problem is that this loop will see a duplicate...the number that was just entered. If the number 5 was entered into array[3], then when the loop runs, it's going to see that array[i] == array[j] if i = 3.
I need something that will either check for a duplicate before the random number is entered into the array (as Bazzy suggested, I just don't know how to use count), or to have it ignore the current entry point.
int randomarray[ ARRAY_SIZE ];
int randomvalue;
do
{
randomvalue = rand(); // get random value
}while ( count ( randomarray, randomarray+ARRAY_SIZE, randomvalue ) ); // randomvalue is present
randomarray [ index ] = randomvalue; // randomvalue wasn't in the array so you can insert it
int number;
int pick[number], guessDigit[number];
int temp, n;
for (int n = 0; n < number; n++) {
do {
temp = rand() % 10;
}
while ( count ( pick, pick+number, temp ) );
temp = pick[n];
}