Thread: Intersection of two arrays

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    New Mexico
    Posts
    10

    Question Intersection of two arrays

    I am trying to find a the union and intersection of arrays A and B. The union portion of my code is working, but the intersection is not working. Can anyone please help me figure out what is wrong with my intersection code?

    Code:
    int main(void)
    
    
    {
    
    
    
    
    int A[5], B[5];
    int C[5];
    int lengthA = 0;
    int lengthB = 0;
    int i, j, k,sizeAuB,temp;
    
    
     // Code that takes in user input for set A
    
    
    for (i = 0; i < 5; ++i)
    {
     printf("Please input a number between [0, 10] into set A: ");
     scanf("%i", &temp);
    
    
      if (temp < 0 || temp > 10)
        {
          break;
        }
       else
        {
          A[i] = temp;
          lengthA = lengthA + 1;
        }
       }
    
    
    
    
     printf("The values for set A are:\n{");
      for (i = 0; i < lengthA; ++i)
      {
       printf(" %i", A[i]);
      }
    
    
      printf(" }\n\n");
    
    
    //Code to enter elements into set B
    
    
      for (i = 0; i < 5; ++i)
      {
       printf("Please input a number between [0, 10] into set B: ");
       scanf("%i", &temp);
    
    
      if (temp < 0 || temp > 10)
        {
          break;
        }
    
    
       else
       {
         B[i] = temp;
         lengthB = lengthB + 1;
        }
    
    
       }
    
    
    
    
      printf("The values for set B are:\n{");
      for (i = 0; i < lengthB; ++i)
      {
        printf(" %i", B[i]);
      }
    
    
        printf(" }\n\n"); printf(" }\n\n");
    
    
        printf("Set A is:\n{");
    
    
        for (i = 0; i < lengthA; ++i)
    
    
        {
          printf(" %i", A[i]);
        }
    
    
    
    
        printf(" }\n\n");
    
    
        printf("Set B is:\n{");
        for (i = 0; i < lengthB; ++i)
        {
          printf(" %i", B[i]);
        }
    
    
        printf(" }\n\n");
    
    
        // Finds the Union of A and B
    
    
       for (k = 0; k < lengthA; ++k)
        {
          C[k] = A[k];
        }
    
    
      for (i = 0; i < lengthB; ++i)
        {
          for (j = 0; j < lengthA; ++j)
         {
    
    
            if (B[i] == C[j])
            break;
         }
    
    
          if (j == lengthA)
          {
            C[k] = B[i];
    
    
            k = k + 1;
            sizeAuB = k;
    
    
          }
    
    
    
    
        }
    
    
    // Sorts the elements that are Union of A and B from smallest to largest number.
    
    
        for (i = 0; i < sizeAuB - 1; ++i)
        {
          for (j = i + 1; j < sizeAuB; ++j)
        {
         if (C[j] < C[i])
            {
              temp = C[j];
              C[j] = C[i];
              C[i] = temp;
            }
         }
        }
    
    
    
    
        printf("The union of A and B is:\n{ ");
        for (i = 0; i < sizeAuB; ++i)
        {
    
    
           printf(" %i", C[i]);
        }
    
    
    
    
        printf(" }\n\n");
    
    
    
    
    printf(" The intersection is:\n{");
      while (i<lengthA && j<lengthB)
      {
          if (A[i] < B[j])
          i++;
       else if (B[j] < A[i])
         j++;
       else
       {
           printf ("%i", B[j++]);
           i++;
       }
      }
       printf(" }\n\n");
    
    
     return 0;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    All those nice %i s you have in there... try %d instead.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try a little different logic, for the intersection part.

    Code:
    int num1[5] = {0, 1, 3, 5, 7};
    int num2[5] = {4, 6, 7, 8, 9};
    
       for(i=0,j=0;i<5 && j<5;) {
          if(num1[i] < num2[j]) 
             ++i;
          else if(num1[i] > num2[j])
             ++j;
          else {
             printf("%3d ", num1[i]);
             ++i;
          }
       }

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Style suggestion: you've got way, way, way too many blank lines.
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Intersection of 2 Arrays.
    By lexy in forum C Programming
    Replies: 4
    Last Post: 03-21-2010, 11:21 PM
  2. intersection of two STL lists
    By misterMatt in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2009, 12:25 AM
  3. (C#) Ray->Box intersection
    By Devils Child in forum Game Programming
    Replies: 6
    Last Post: 12-18-2008, 01:02 AM
  4. Vector intersection
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 09-27-2006, 11:23 PM
  5. intersection and union
    By BaAa3 in forum C Programming
    Replies: 2
    Last Post: 02-29-2004, 04:39 AM