如何在C中随机找到包含一些连续元素的子数组?
我有一个包含100个元素的数组。数组元素是一组1和0。例如:如何在C中随机找到包含一些连续元素的子数组?,c,opnet,C,Opnet,我有一个包含100个元素的数组。数组元素是一组1和0。例如: 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} 我需要找到所有零窗口(间隙),并随机选择其中一个。根据1和20之间的均匀分布选择间隙的大小(需要) needed = op_dist_load ("uniform_int", 1, 20); 例如,如果我们假设元素的其余部分等于数组[100]中的1,如您所见,我有8个零窗口(窗口的大
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}
我需要找到所有零窗口(间隙),并随机选择其中一个。根据1和20之间的均匀分布选择间隙的大小(需要)
needed = op_dist_load ("uniform_int", 1, 20);
例如,如果我们假设元素的其余部分等于数组[100]中的1,如您所见,我有8个零窗口(窗口的大小=2)。我需要找到他们
find_gap_random
函数选择两个连续的零元素(具有两个零元素的子数组表示我的程序中存在间隙)。您是否有任何建议,可以在不使用随机库的情况下用C语言编写find\u gap\u random(需要int)
函数的代码,最好是用于OPNET模拟器
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;
}
如果您仍在查找
数组中的间隙(间隙定义为所需长度至少为的连续零元素数),则查找所有间隙的简单、直接的方法是移动“滑动窗口”ofneeded
数组长度,检查窗口中的所有值是否为零。如果它们都为零,则表示您发现了一个间隙,如果没有,则移动到数组中的下一个索引,然后重复
这个概念很简单,但一张图片可能会有所帮助(或尝试图片)
(注意:内部循环可以替换为memcmp
,以检查值是否将所有字节设置为零以定位间隙)
以图表为指导,完成上面的查找差距操作,并确保您完全理解所发生的事情。如果没有,请告诉我
将其放在一个简短的示例中,该示例将needed
作为程序的第一个参数(如果没有给出参数,则默认使用2
),您可以执行以下操作:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/** find_gaps locates each sequence of all zero within 'array' of
* at least 'needed' length, the corresponding index within 'gaps'
* is incremented to identify the start of each gap, returns the
* number of gaps of 'needed' length in 'array'.
*/
int find_gaps (int *gaps, int *arr, int nelem, int needed)
{
int ngaps = 0; /* count of gaps found */
/* add code to validate parameters here */
memset (gaps, 0, nelem * sizeof *gaps); /* zero gaps array */
for (int i = needed; i < nelem; i++) { /* loop needed to end */
for (int j = i - needed; j < i; j++) { /* loop previous needed */
if (arr[j] != 0) /* if non-zero value, get next in array */
goto next; /* lowly 'goto' works fine here */
}
gaps[i-needed]++; /* increment index in gaps */
ngaps++; /* increment no. of gaps found */
next:;
}
return ngaps; /* return no. of gaps found */
}
int main (int argc, char **argv) {
int array[] = { 0,0,1,0,1,0,1,1,1,0,0,0,0,0,1,1,0,1,0,0,
0,1,1,0,1,0,0,0,1,0,1,1,0,0,1,0,1,0,0,1 },
nelem = sizeof array / sizeof *array, /* number of elements */
gaps[nelem], /* a VLA is fine here C99+, otherwise allocate */
ngaps = 0, /* no. of gaps found */
needed = argc > 1 ? strtol (argv[1], NULL, 0) : 2; /* (default: 2) */
if (errno) { /* validate strtol conversion succeeded */
perror ("strtol-argv[1]");
return 1;
}
/* find the number of gaps, storing beginning index in 'gaps' array */
ngaps = find_gaps (gaps, array, nelem, needed);
printf ("gaps found: %d\n", ngaps); /* output number of gaps */
for (int i = 0; ngaps && i < nelem; i++) /* output index of gaps */
if (gaps[i])
printf (" gap at array[%2d]\n", i);
return 0;
}
检查间隙是否至少为3个零:
$ ./bin/findgaps 3
gaps found: 5
gap at array[ 9]
gap at array[10]
gap at array[11]
gap at array[18]
gap at array[25]
有几种方法可以解决这个问题,但是滑动窗口可能是最直接的方法之一,而且滑动窗口在C语言中有许多其他应用程序,因此它非常值得添加到您的工具箱中。如果您还有其他问题,请告诉我。在0
和99-需要的
(含)之间选择一个随机索引,查看从该索引开始是否有需要的
零数。如果没有,则选择另一个随机索引,直到找到匹配项。谢谢。。。但是需要的值是固定的(即2),问题是数组中可能有许多零对,如何找到零对并随机选取其中一对。我想这正是一些人已经回答的问题。什么是FIN()
和FOUT
。他们在展示你的问题时扮演什么角色?请学习a的概念。宏“FIN”和“FOUT”用于OPNET函数中,以使OPNET调试内核能够打印出函数信息。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
/** find_gaps locates each sequence of all zero within 'array' of
* at least 'needed' length, the corresponding index within 'gaps'
* is incremented to identify the start of each gap, returns the
* number of gaps of 'needed' length in 'array'.
*/
int find_gaps (int *gaps, int *arr, int nelem, int needed)
{
int ngaps = 0; /* count of gaps found */
/* add code to validate parameters here */
memset (gaps, 0, nelem * sizeof *gaps); /* zero gaps array */
for (int i = needed; i < nelem; i++) { /* loop needed to end */
for (int j = i - needed; j < i; j++) { /* loop previous needed */
if (arr[j] != 0) /* if non-zero value, get next in array */
goto next; /* lowly 'goto' works fine here */
}
gaps[i-needed]++; /* increment index in gaps */
ngaps++; /* increment no. of gaps found */
next:;
}
return ngaps; /* return no. of gaps found */
}
int main (int argc, char **argv) {
int array[] = { 0,0,1,0,1,0,1,1,1,0,0,0,0,0,1,1,0,1,0,0,
0,1,1,0,1,0,0,0,1,0,1,1,0,0,1,0,1,0,0,1 },
nelem = sizeof array / sizeof *array, /* number of elements */
gaps[nelem], /* a VLA is fine here C99+, otherwise allocate */
ngaps = 0, /* no. of gaps found */
needed = argc > 1 ? strtol (argv[1], NULL, 0) : 2; /* (default: 2) */
if (errno) { /* validate strtol conversion succeeded */
perror ("strtol-argv[1]");
return 1;
}
/* find the number of gaps, storing beginning index in 'gaps' array */
ngaps = find_gaps (gaps, array, nelem, needed);
printf ("gaps found: %d\n", ngaps); /* output number of gaps */
for (int i = 0; ngaps && i < nelem; i++) /* output index of gaps */
if (gaps[i])
printf (" gap at array[%2d]\n", i);
return 0;
}
$ ./bin/findgaps
gaps found: 11
gap at array[ 0]
gap at array[ 9]
gap at array[10]
gap at array[11]
gap at array[12]
gap at array[18]
gap at array[19]
gap at array[25]
gap at array[26]
gap at array[32]
gap at array[37]
$ ./bin/findgaps 3
gaps found: 5
gap at array[ 9]
gap at array[10]
gap at array[11]
gap at array[18]
gap at array[25]