How to generate 50 distinct numbers in an array?
I have the code for generating 50 numbers but thay are not different. I get same number sometimes twice or three time.
Here is my code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int num;
int main()
{
int array[50];
for(int i = 0; i<50; i++)
{
array[i] = rand()%99;
}
for(int i = 0; i<50; i++)
{
cout<<array[i]<<" ";
}
cout <<"\nEnter an integer between 0 & 99: ";
cin >> num;
int flag = 0;
for(int k = 0; k < 50; k++)
{
if(array[k] == num)
{
cout << "Number found in array index " << k << "."
<< endl;
flag += 1;
}
}
if(flag < 1)
{
cout << "number not found." << endl;
}
return 0;
}