Thread: if statment ignoring condition.

  1. #1
    Registered User
    Join Date
    Apr 2019

    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.
    #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;
    Last edited by Salem; 04-10-2019 at 03:56 AM. Reason: Removed crayola

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2019
    ok thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple if statment need help
    By automagp68 in forum C Programming
    Replies: 9
    Last Post: 02-07-2012, 01:45 PM
  2. need help with go to statment
    By begginer in forum C Programming
    Replies: 15
    Last Post: 04-06-2011, 10:54 AM
  3. If statment qustion
    By Yizi in forum C Programming
    Replies: 5
    Last Post: 11-21-2009, 04:53 AM
  4. if-statment not working
    By IxTanGxI in forum C Programming
    Replies: 7
    Last Post: 02-21-2006, 12:52 AM
  5. if statment
    By rkjd2 in forum C++ Programming
    Replies: 1
    Last Post: 09-23-2001, 11:48 AM

Tags for this Thread