c++ - 整数的数组比较

标签 c++ c

这里我比较两个数组元素,如果数组元素相等SETPASSWORD=1;,否则SETPASSWORD=0; 但这里总是打印​​密码设置状态1即使数组不相等

#include <stdio.h>
#define ARY_SIZE 4
int password_set;
int main (void)
{
  //Local Declarations
  int numbersA[ARY_SIZE];
  int numbersB[ARY_SIZE];
  int i;
  int j;

  //Statements
  printf("Please Enter 10 Integers For Array A\n");
  for (int i = 0; i < ARY_SIZE; i++)
    scanf("%d", &numbersA[i]);

  printf("\nPlease Enter 10 Integers For Array B\n");
  for (int j = 0; j < ARY_SIZE; j++)
    scanf("%d", &numbersB[j]);

  for (int i = 0; i < ARY_SIZE; i++)
  {
    for (int j = 0; j < ARY_SIZE; j++)
    {
      if (numbersA[i] == numbersB[j])
        password_set=1;
      else
        password_set=0;
    }
  }
  printf(" password setstaus =%d",password_set);
  return 0;
}

最佳答案

你的逻辑是错误的。一旦一对数字不相等,您必须立即退出循环。

而且您也只需要一个循环:

password_set = 1 ;

for (int j = 0; j < ARY_SIZE; j++)
{
  if (numbersA[j] != numbersB[j])
  {
    password_set = 0;
    break ;
  }
}

关于c++ - 整数的数组比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22371999/

相关文章:

c++ - 通过服务启动进程后 SHGetKnownFolderPath 出现错误 0x80070005

c++ - 将 "this"对象的 shared_ptr 获取到另一个函数 : giving run time exception

c - 使用 C 的数学 101

c - 为什么这个小程序输出True? GCC有溢出保护吗?

c - 如何在不同的头文件中具有相同类型和名称的两个结构而不发生冲突?

c++ - 具有增加的所有者线程优先级的同步原语

c++ - 函数类型之间的区别

c++ - 获取参数包中函数指针的返回类型并将其保存为与其他参数的连接元组

c++ - 从源代码中使用 SQL lite - 如何创建数据库文件并将一些数据放入其中?

C中的跨平台单元测试