You are here:

C++/Checking for doubles

Advertisement


Question
Expert,

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.

Answer
Hi 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

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Joydeep Bhattacharya

Expertise

TSR, Socket and Very Complex Mathematical Problem

Experience

Worked on many OOP projects with design patterns

Organizations
http://www.scodz.com

Publications
http://www.scodz.com

Education/Credentials
Master of Computer Applications

Awards and Honors
Being a proud owner of a website of programming called http://www.scodz.com

Past/Present Clients
http://www.funforu.com http://www.dhoondho.com http://analysingc.50webs.com

©2016 About.com. All rights reserved.