Refactor. Your code tries to do too many things at once.
(1)
Keep track of how many items are in your array.
1 2 3 4
|
int main() {
const int MAX_ARR_SIZE = 5;
int arr[MAX_ARR_SIZE];
int arr_size = 0;
|
Whenever you add an element to the array, simply increment the
arr_size
. Don’t let
arr_size
ever become larger than
MAX_ARR_SIZE
.
(2)
You should have a function that determines whether or not a number is currently stored in the array. You could call it "is_element_of" or something:
bool is_element_of( int x, int xs[], int size )
This is where you run through the array and test for uniqueness.
Hint: you do not need a variable “isUnique” for this.
(Good job for the good variable name, though.)
Hint2: “b” is a terrible variable name for a size/count/non-random,unpaired integer.
Hint3: You do not need an additional array.
(3)
Populate the array. Pull random numbers until
arr_size
==
MAX_ARR_SIZE
.
Only add elements to the array iff the random value you get is not already in the array.
(4)
Congratulations, you have a randomly-populated array of
arr_size
==
MAX_ARR_SIZE
elements.
Print them.
Hope this helps.