Thread: Intersection of 2 Arrays.

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Intersection of 2 Arrays.

    I need help figuring out whats wrong with my intersection function.
    I have to get 2 sets of arrays from the user then find the intersection and print it out.
    Code:
    #include "stdafx.h"
    
    /*function prototype*/
    void getSet1(void);
    void getSet2(void);
    int findint(int inter[]);
    void printInter(int Array[]);
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	getSet1();
    	getSet2();
    	findint();
    	printInter(findint);
    
     
    return 0;
    
    }
    void getSet1(void)/* Set1 */
    {
    int i;
    int set1[10];
    
    printf("Enter First Set");
    for(i=0;i<10;i++)
    scanf("%d",&set1);
    
    }
    
    
    void getSet2(void)/*Set 2*/
    {
    int i;
    int set2[10];
    
    printf("Enter Second Set");
    for(i=0;i<10;i++)
    scanf("%d",&set2);
    }
    
    
    
    int findint(int inter[])/*intersection*/
    {
    int i;
    int j;
    int k;
    getSet1();
    getSet2();
    
    
    
    
    for(i=0;i<10;i++)
    {
    for(j=0;j<10;j++)
    {
    }
    if(getSet1[i]==getSet2[j]);
    {
    k++;
    }
    if (getSet1[i] != getSet2[j])
    {
    k=NULL;
    }
    }
    return k;
    }
    
    
    void printInter(int Array[])/*print out intersection*/
    {		
    	int k;
    
    		for(k=0;k<10<;k++)
    		
    		printf("%d",Array[k]);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You add things to k (although then you take them away again, by setting k to NULL), but you never put anything in inter[].

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    14
    remove k=null and u can also remove the whole else do u want to print same elements too?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Delete the semi-colon on the end of the if statement.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by lexy View Post
    I need help figuring out whats wrong with my intersection function.
    I have to get 2 sets of arrays from the user then find the intersection and print it out.
    Code:
    #include "stdafx.h"
    
    /*function prototype*/
    void getSet1(void);
    void getSet2(void);
    int findint(int inter[]);
    void printInter(int Array[]);
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	getSet1();
    	getSet2();
    	findint();
    	printInter(findint);
    
     
    return 0;
    
    }
    void getSet1(void)/* Set1 */
    {
    int i;
    int set1[10];
    
    printf("Enter First Set");
    for(i=0;i<10;i++)
    scanf("%d",&set1);
    
    }
    
    
    void getSet2(void)/*Set 2*/
    {
    int i;
    int set2[10];
    
    printf("Enter Second Set");
    for(i=0;i<10;i++)
    scanf("%d",&set2);
    }
    
    
    
    int findint(int inter[])/*intersection*/
    {
    int i;
    int j;
    int k;
    getSet1();
    getSet2();
    
    
    
    
    for(i=0;i<10;i++)
    {
    for(j=0;j<10;j++)
    {
    }
    if(getSet1[i]==getSet2[j]);
    {
    k++;
    }
    if (getSet1[i] != getSet2[j])
    {
    k=NULL;
    }
    }
    return k;
    }
    
    
    void printInter(int Array[])/*print out intersection*/
    {		
    	int k;
    
    		for(k=0;k<10<;k++)
    		
    		printf("%d",Array[k]);
    }
    You're assuming that the only problem is in the findInt function, when in actual fact getSet1 and getSet2 are equally as faulty. There are too many wrong here to simply list the problems with this code. You've just written random stuff and expected it to do something remotely sensible.

    You need to start with something simpler. First learn how to fill in an array with value supplied by the user, and then print them out afterwards from a separate function.
    Hint, pass the array into the function that fills the array in.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create and manipulate Terabyte size Arrays with Win32API
    By KrishnaPG in forum Windows Programming
    Replies: 1
    Last Post: 11-05-2009, 04:08 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

Tags for this Thread