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}
I need to find all zero windows(gaps) and choose one of them randomly. The size of the gap (needed) is chosen according to a uniform distribution between 1 and 20.
needed = op_dist_load ("uniform_int", 1, 20);
for example If we assume that the rest of the element are equal to 1 in the Array[100], as you can see, I have 8 zero windows (the size of the window =2). I need to find them.
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). Do you have any suggestions for writing the code of find_gap_randomly(int needed) function in C language without using random library, preferably for OPNET simulator?
static void find_gap_randomly(int needed)
{
//The macros “FIN” and “FOUT” are used in OPNET functions to enable the OPNET
//debugging kernel to print out function information.
FIN(find_rr_gap());
/* ... */
FOUT;
}
If you are still working on finding the gaps in
Array, (a gap defined as the number of sequential zero elements of at leastneededlength), then a simple, straight-forward way of finding all gaps is to move a "sliding-window" ofneededlength down the array checking whether all values within the window are zero. If they are all zero, you have found a gap, if not, move to the next index inArrayand repeat.The concept if fairly simple, but a picture may help (or attempted picture)
Shown above you have the first nine elements of your
Arrayshown, and the corresponding index for each element underneath. Since yourneededis2you have a window that spans2-elementsthat you will move from the beginning to end ofArraychecking the values within. This can be done simply with two nested loops, the outer loop looping whilei = needed; i < num_elements; i++and then the inner loop iterating fromj = i - needed; j < i; j++.To capture where the gaps within
Arrayare, you use a second array (I calledgaps) containing the same number of elements asArrayinitialized All zero. When you find a region withinArraywhere there aneedednumber of sequential elements, you simply incrementgaps[j]++;to set the value atgaps[j]from0to1. (depending on the scope ofj, you may need to incrementgaps[i-needed]++;ifjhas gone out of scope.When you are done moving your sliding window from beginning to end,
gapswill have a value of1at each index where the beginning of a gap was located inArray.A simple function implementing the sliding window could be written like:
(note: the inner loop can be replaced with
memcmpto check against a value will all bytes set to zero to locate the gaps)Go through the operation of
find_gapsabove, using the diagram as your guide and make sure you understand exactly what is going on. If not, let me know.Putting it altogether in a short example that takes
neededas the 1st argument to the program (or uses2by default if no argument is given), you could do something like the following:(note: you should add checks that
neededis less thannelem, but that and any additional validations are left as an exercise for you)Example Use/Output
Checking for gaps of at least 3 zeros:
There are several ways to approach this problem, but a sliding-window is probably one of the most straight-forward, and the sliding-window has many, many other applications in C, so it is well worth adding to your toolbox. Let me know if you have further questions.