Thread: Intersection of 2 arrays

  1. #1
    Registered User
    Join Date
    Jan 2015
    Location
    Bucharest, Romania, Romania
    Posts
    15

    Intersection of 2 arrays

    Hello guys, I've been watching this forum for the past 2 weeks and I can say you do a nice job here.
    I know this have been posted a million times, but i still can't solve my problem.

    Code:
    #define _CRT_SECURE_NO_WARNINGS#include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    
    
    {
        int a[10],b[10],c[10],i,j,k,n,m;
        printf ("Enter the size of the first vector: ");
        scanf ("%d", &n);
        for ( i=0 ; i<n ; i++)
        {
            printf ("a[%d]= ", i);
            scanf ("%d", &a[i]);
        }
        printf ("Enter the size of the second vector: ");
        scanf ("%d", &m);
        for ( j=0 ; j<m ; j++)
        {
            printf ("b[%d]= ", j);
            scanf ("%d", &b[i]);
        }
        if ( a[i] == b[j] )
        {
            printf ("a[%d]", i);
        }
        system("pause");
    }
    Now, this is what I've been able to do so far, i know it's very basic but I just started learning programming! I tried different methods found on internet but none of them work for me. I want to find the intersection of arrays a & b then put them in array c(or just display them?)

    Thanks in advance!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your idea for solving this?
    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
    Jan 2015
    Location
    Bucharest, Romania, Romania
    Posts
    15
    I was thinking of making a for
    (a[i] == b[j])
    then copy a[i] into c[k] or something like that, I'm not sure if it's possible...
    Or if it's the best way...

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You are heading in a right direction. What you need are nested loops.

    It wouldn't be the most efficient way, but that can come later.
    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

  5. #5
    Registered User
    Join Date
    Jan 2015
    Location
    Bucharest, Romania, Romania
    Posts
    15
    Should i check for "nested loops" and inform about them then try to implement ?
    Or find a more efficient way ?

    I appreciate the help, and i know it's better to find this out on my own... But I'm not sure I can, i'm a real newbie
    So if you could give me and example and a little explanation so i could try and understand it, it would be great!

    Thanks again for your time !

  6. #6
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    You have a mistake on line 22. The loop is counting 'j', but you set 'b[i]'.

    The user enter the size on line 10/11 and 17/18, but you don't check if the value is greater then 10.
    If the user enter a higher value, your loops overrun the array bounderies.

    The main-function should return a integer.
    Your function definition is right, but you have no 'return 0;' at end of the function.
    Last edited by WoodSTokk; 01-13-2015 at 08:41 AM.
    Other have classes, we are class

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Cazan Adelin
    Should i check for "nested loops" and inform about them then try to implement ?
    A nested loop just means that it is a loop within another loop. In this case, the outer loop will loop over one array, and the inner loop will loop over the other array to check for a match.

    Quote Originally Posted by Cazan Adelin
    Or find a more efficient way ?
    If you want an efficient method, then sort the two arrays. Once the arrays are sorted, you can iterate over them in parallel, copying over to the third array only those elements that have the same value.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Intersection of two arrays in O(n) complexity
    By malwaregeek in forum C Programming
    Replies: 6
    Last Post: 12-31-2012, 12:06 AM
  2. Intersection of two arrays
    By jacob_76505 in forum C Programming
    Replies: 3
    Last Post: 10-17-2011, 11:45 PM
  3. Intersection of 2 Arrays.
    By lexy in forum C Programming
    Replies: 4
    Last Post: 03-21-2010, 11:21 PM
  4. (C#) Ray->Box intersection
    By Devils Child in forum Game Programming
    Replies: 6
    Last Post: 12-18-2008, 01:02 AM
  5. Line Intersection
    By Highland Laddie in forum C++ Programming
    Replies: 13
    Last Post: 10-13-2005, 04:14 PM