Hello, I have a problem with my program that I can´t figure out. I would appreciate if someone could give me some hints on what to do.
I am trying to write a program that checks how many random numbers less than a positive integer N one should generate before all integer values between 0 and N are produced at least once.
The code that I give here is not all of the code that I have. I have added a function that sorts the array in a ascending order from lowest to highest, so you can assume that the array is sorted.
What I need is for the program to loop until all the numbers from 0 -n have come up in the array at least once. So far it doesn´t loop automatically. I have tried to use a do while loop without success and manually loop it with cout<<"Do you want to try again?; But if I do that the program doesn´t remember what numbers have already come up in the array. Say if the first time I run the random numbers function for n=4 the numbers that come up are 0233, then I want to be able to run it again until the numbers 1 has come up.
Thank you.
//void selectionSort(int arr[], int size); For sorting the array
int main()
{
int n,isTrue=1;
cout<<"Put in the number: "<<endl;
cin>>n;
cout<<"There are: "<<n<<" elements in the array. "<<endl;
//To generate random numbers
srand((unsigned)time(0));
while(isTrue!=0){ // to generate a loop that will continue until all the elements in the array have appeared
int counter=0;
counter++;
int arr[n];
cout << "The numbers are: "<<endl;
for(int i=0; i<n; i++)
{
arr[i] = (rand()%n);
cout << arr[i]<<" ";
}
selectionSort(arr, n); // this sorts the array in ascending order from 0-N
int s,j;
for(s = 0; s < n && isTrue == 1; s++)
{
for( j = s+1; j < n; j++)
{
if( arr[s] >= arr[j] )
{
isTrue = 0;
cout<< ""<<endl;
cout<< "Not all of the numbers from 0-n came up!"<<endl;
}
}
}
}
return 0;
}