我知道这可能看起来像一个硬件问题,并且作为我的客人来对待它,因为这是一个自学练习。
测试多个数组中是否存在特定值的最简单方法是什么?
例如:
伪代码
if array a contains a value of 4 and
array b contains a value of 2 and
array c contains a value of 6
then procede to procedure x
到目前为止我在实现中有什么
#include<stdio.h>
void x(void){/* do stuff */}
int main()
{
char fndA = 0;
char fndB = 0;
char fndC = 0;
int a[5] = {1,2,3,5,6};
int b[5] = {1,2,2,3,4};
int c[5] = {1,3,4,5,6};
for(int i=0;i<5;i++)
{
if(a[i]==4){fndA=1;}
}
//repeat for-loop for b/fndb and c/fndC
if (fndA && fndB && fndC) {x();}
return 0;
}
在此示例中,x()
永远不会被调用,因为数组 a
中不存在 4。但是我最终是否必须为每个要测试的数组构建一个 for
循环?谢谢
最佳答案
是的。但是您显然可以创建一个执行循环的函数。您需要将数组、它的大小和您要查找的元素传递给该函数。
int array_contains_int(int *arr, size_t size, int value) {
int i;
for(i = 0; i < size; i++)
if(arr[i] == value)
return 1;
return 0;
}
关于c - 测试多个数组中是否存在某些值的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10691807/