2

Here I am comparing two array element , If array element are equal SETPASSWORD=1; else SETPASSWORD=0; But Here It is always printing password set status 1 even array are not equal

#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;
}
4
  • Show us the array elements you have tried. Commented Mar 13, 2014 at 7:38
  • numbersA=1 2 3 4 number B= 1 0 0 0 Commented Mar 13, 2014 at 7:40
  • 1
    You set password_set every loop iteration so the result is going to be telling you whether the 10th numbers equal or not. Commented Mar 13, 2014 at 7:41
  • 1
    Using C++: password_set = std::equal(numbersA, numbersA + ARY_SIZE, numbersB); Commented Mar 13, 2014 at 8:02

3 Answers 3

4

Your logic is wrong. You must exit the loop as soon as one pair of numbers is non equal.

And you also need only one loop :

password_set = 1 ;

for (int j = 0; j < ARY_SIZE; j++)
{
  if (numbersA[j] != numbersB[j])
  {
    password_set = 0;
    break ;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Then where to set password_set = 1; if i set globally as 1 it will be always 1. if array are equalto 1 then only set to 1 else set to zero
You set password_set = 1 just before the loop as shown in the answer. If the two arrays are different password_set will be set to 0, otherwise it will stay 1.
1

Your compare each value of the array in your for (if (numbersA[i] == numbersB[j])

It resets the value of password_set at each iterations. It means that the result printed will be the last index of your array.

And btw you need only one loop

Comments

0

Actually, you comparison is wrong. Because you want to test two arrays is equal or not, you just test each number of these two arrays.

like this:

    password_set = 1;

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

2 Comments

The password_set flag should really not be set back to 1 if just one pair happens to be equal (that would overwrite an unequality found before). You either initialize it to 1 and exit when the first inequality is found, or for god's sake continue to the end of the loop, but don't overwrite the 0 later with a 1 in the loop.
Perhaps apple has a job offer for you if you are interested in safety critical software ;-) (scnr)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.