If you're allowed to, I highly recommend learning how to use std::set, which will act like an array but prevent duplicate values automatically for you. It is also generally more efficient than using an array.
Here's one way. After generating each value, store it in an array.
When generating subsequent values, search through the previously-used values.If you find it, try again.
int arr[10]; // Array to hold up to 10 values
for (int i=0; i<10; i++)
{
int r;
bool found = true;
while (found)
{
found = false; // begin by assuming the value is not found
r = rand() % 10 + 1; // the random number
for (int j=0; j<i; j++) // search the previously used values
{
if (r == arr[j]) // did we already use this?
{
found = true; // yes, need to try again
break; // no need to continue search
}
}
}
cout << r << " ";
arr[i] = r; // store the used value
}
Here's another way. Store our wanted values in an array.
The first time we can select any value from the entire array.
Then replace the used value with the last item in the array. Next time, select from just the first nine items, then from the first eight and so on.
1 2 3 4 5 6 7 8
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
for (int size=9; size>=0; size--)
{
int ix = rand() % (size+1); // select an item from 0 to size
cout << arr[ix] << " "; // output it
arr[ix] = arr[size]; // replace the selected item with item[size]
}