在 C 中将单个整数与整数数组进行比较?

标签 c mplab xc8

如何使用 C 语言将单个整数与包含 10 个整数的数组进行比较,以确定该单个整数是否包含在数组中?如果原始问题不清楚,我很抱歉,而滑动等于我想要输出 PORTD=0b10000000 的数组中的任何数字。谢谢!

short a[10]={10, 11, 12, 13, 14, 15, 16, 17, 18, 19}; //Array of accepted passwords.
short array[10];
int i;
for(i=0; i<10; i++){
    array[i]=a[i];
}

srand(clock());

while(1){
int swipe=rand() % 20; /* Simulated code read from card swipe, in this
                        * instance we used a random number between
                        * 1 and 20.*/
for(i=0; i<10; i++){    
    if(swipe == array[i]) {
        PORTD=0b10000000;
    } else {
        PORTD=0b00001000;
    } //If swiped code evaluates as one of the approved codes then set PORTD RD7 as high.
}

char Master=PORTDbits.RD7;

这似乎已经解决了......感谢您的帮助!

for(i=0; i<10; i++){    
if(swipe == array[i]) {
    PORTD=0b10000000;
    break;
} else {
    PORTD=0b00001000;
    }
}

最佳答案

您需要根据接受的密码数组中的所有十个值来测试您的滑动值。

例如像下面这样

for(i=0; i<10; i++)
  if(swipe == array[i]) {
    set a true flag (and maybe exit the for loop)
  }
Depending on the flag, set the output

关于在 C 中将单个整数与整数数组进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35756799/

相关文章:

c - 类似于SIMD指令的宏

c - 在方法中传递数组并获取它的大小

c程序功能困惑

compiler-errors - 更换计算机后,MPLABX Project停止编译

c - execl 和 execv 有什么区别?

c - 定义未定义数组并从 c 中的结构返回时出现段错误

c - 如何将整数转换为 void 指针?

c - 如何在void函数中插入for循环?

c - 将指针传递给结构数组