C++/Checking for doubles
Expert: Joydeep Bhattacharya - 10/9/2009
QuestionExpert,
I'm working on a program that is trying to fill an array with 10 random numbers. I understand how to get the random numbers 1-100
/*my code for getting random numbers and filling the array */
void draw (int rand_num[], int max_size)
{
int tmp;
srand(time(NULL));
for (int index = 0; index < max_size; index++)
{
tmp = rand () % 100;
if (tmp == 0)
tmp++;
checker (rand_num[],
rand_num[index] = tmp;
}
}
The problem i am having is i need to create another function to check if the next number that is being added is not a double (ie to make sure that the array doesnt have two '5' or three '47'). I have the basic idea that i need a for loop to run through the array and then an if statement { if (tmp == rand_num[index]) }. how would i go about changing the tmp in the if statement and then checking it again.
AnswerHi Scottzozer
Check this might help you but the performance may be questioned here but this would solve your purpose for sure
void draw (int rand_num[], int max_size)
{
int tmp, index, j, flag;
srand(time(NULL));
for (index = 0; index < max_size; index++)
{
do{
flag = 0;
tmp = rand () % 100;
for(j=0;j<index;j++)
{
if(rand_num[j]==tmp)
flag = 1;
}
}while(flag);
rand_num[index] = tmp;
}
}
In case of any doubt please feel free to get back to me.
Do visit my personal website for many search programs that might help you
regards
Joydeep Bhattacharya
http://www.scodz.com