jeudi 21 juin 2018

How can I find a subarray with arbitrary elements randomly?

I have an array with 100 elements. Array elements are a set of ones and zeros. For example, Array[100] = {0,0,1,0,1,0,1,1,1,0,0,0,0,0,1,1,0,1,0,0,1,0,0,0,1,1,....,1}

If we assume that variable needed has the value of 2, then the "find_gap_randomly" function selects 2 contiguous zero elements (A subarray with 2 zero elements means that there is a gap in my program).

Now, I am curious to know how I can write this program in a more readable code with or without using random library, preferably for OPNET network simulator.Do you have another suggestions for implementing the "find_gap_randomly" function in C ?

static void find_gap_randomly(int needed)
        {
FIN(find_rr_gap());

         int logic=0;
         int first_f=0;
         int s=1;

         for ( int i=1 ; i<=100 ; i++)
             {
                if ( array[i]==0)
                    {
                      if (logic==0)
                          {
                            first_f=i;
                            logic= logic+1;
                          }
                       else
                           {
                            logic= logic+1;
                           }

                       if (i==100)
                           {
                             if (logic >= needed-1)
                                 {
                                     ary_rr[s].first= first_f;
                                     ary_rr[s].last=i;
                                     ary_rr[101].first=ary_rr[101].first+1;
                                 }
                           }
                    }
                else
                    {
                      if (logic >= needed && logic!=0)
                          {
                                ary_rr[s].first= first_f;
                                ary_rr[s].last=i-1;
                                ary_rr[101].first=ary_rr[101].first+1;
                                s=s+1;
                          }
                      logic=0;
                      first_f=0;
                    }
             }

            return;
            FOUT;
        }




Aucun commentaire:

Enregistrer un commentaire