1. if statment ignoring condition.

Hi Guys,

I'm new to programming and new to this forum so hello everyone.

Can anyone point me in the right direction, and let me know where I'm going wrong in this code below.

I’m trying to match the numbers of two arrays and then print the numbers that are the same. My problem is this code prints all the integers of array_1 when it should be only printing the integers that match (10 20 40 50) it looks like the condition of the if statement is being ignored.
Code:
#include <stdio.h>

int main(void)
{

int array_1[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
int array_2[] = { 10, 20, 30, 40, 50, 6, 7, 8, 9, 10 };
int i;
int match;
i = 0;

/* Array_1 loops from element 0 to 9, comparing all elements of array_2 for a match */

for (i = 0; i < 10; ++i) {

if (array_1[i] == array_2[0] || array_2[1] || array_2[2] || array_2[3]
|| array_2[4] || array_2[5] || array_2[6] || array_2[7] || array_2[8]
|| array_2[9])

/* Print matches to screen */
{
match = array_1[i];
printf("Interger %d is a match\n", match);
}
}

return 0;
}

2. If you're looking for an efficient solution for arbitrarily long arrays, then sort both arrays and do a single pass on both arrays simultaneously.

For your homework problem though, I would expect you to use nested loops: the outer loop would loop over one array and the inner loop would loop over the other array.

Note that a == b || c is not equivalent to a == b || a == c.

3. ok thanks