Software & Finance





Turbo C++ - Intersection of Two Arrays





Here is the Turbo C++ source code intersection of two arrays which means finding the common elements between two arrays. The good thing about this coding is it will allocate only the required memory. During the first iterator it will count how many number of unique elements and common elements are available in an array. Then use the count to allocate the memory and fill in the elements in its second iteration.


Source Code


#include <stdio.h>

#include <iostream.h>

 

typedef enum boolean {

      false, true

} bool;

 

int array1[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,

                 95, 85, 75, 65, 55, 45, 35, 25, 15, 05,

                 10, 15, 20, 25, 30, 35, 40, 45, 50, 55

};

 

int array2[] = { 15, 25, 35, 45, 55,

                 12, 22, 32, 43, 52,

                 15, 25, 35, 45, 55

};

 

typedef struct _Array

{

    int *elements;

    int len;

    _Array()

    {

        elements = NULL;

        len = 0;

    }

    _Array(int *data, int len);

    _Array(int len)

    {

      this->len = len;

      this->elements = new int[len];

    }

 

    ~_Array()

    {

      if(this->elements != NULL)

      {

          delete [] this->elements;

          this->elements = NULL;

          this->len = 0;

      }

    }

 

} Array;

 

_Array::_Array(int *data, int len)

{

      this->elements = new int[len];

      this->len = len;

      for(int i = 0; i < len; i++)

          this->elements[i] = data[i];

}

 

 

// Find the array Intersection = Common Elements from two arrays

Array* Find_Common_Elements(Array *p1, Array *p2)

{

    int count = 0;

    for(int i = 0; i < p1->len; i++)

    {

      for(int j = 0; j < p2->len; j++)

      {

          if(p1->elements[i] == p2->elements[j])

          {

            count++;

            break;

          }

      }

    }

 

    Array *result = new Array(count);

    count = 0;

    for(i = 0; i < p1->len; i++)

    {

      for(int j = 0; j < p2->len; j++)

      {

          if(p1->elements[i] == p2->elements[j])

            {

                result->elements[count++] = p1->elements[i];

                break;

            }

        }

    }

 

    return result;

}

 

Array* BubbleSort(Array *p)

{

    for(int i = 1; i < p->len; i++)

    {

        for(int j = 0; j < p->len - i; j++)

        {

            if(p->elements[j] > p->elements[j + 1])

            {

                int temp = p->elements[j];

                p->elements[j] = p->elements[j + 1];

                p->elements[j + 1] = temp;

            }

        }

    }

 

    return p;

}

 

Array* Find_Unique_Elements(Array *p)

{

    BubbleSort(p);

    int element = p->elements[0];

    int count = 1;

    for(int i = 1; i < p->len; i++)

    {

        if(element == p->elements[i])

            continue;

        else

        {

            element = p->elements[i];

            count++;

        }

    }

 

    Array *result = new Array(count);

    count = 0;

    element = p->elements[0];

    result->elements[count++] = element;

    for(i = 1; i < p->len; i++)

    {

        if(element == p->elements[i])

            continue;

        else

        {

            element = p->elements[i];

            result->elements[count++] = element;

        }

    }

    return result;

}

 

int main()

{

    Array *a1 = new Array(array1, sizeof(array1) / sizeof(array1[0]));

    Array *a2 = new Array(array2, sizeof(array2) / sizeof(array2[0]));

 

    Array *p1 = Find_Unique_Elements(a1);

    Array *p2 = Find_Unique_Elements(a2);

    Array *result = Find_Common_Elements(p1, p2);

 

 

    cout << "\n\nUnique Sorted Elements in Array1: ";

    for(int i = 0; i < p1->len; i++)

            cout << p1->elements[i] << " ";

    cout << "\n\nUnique Sorted Elements in Array2: ";

    for(i = 0; i < p2->len; i++)

            cout << p2->elements[i] << " ";

    cout << "\n\nCommon (Intersection of) Elements in Array1 and Array2: ";

    for(i = 0; i < result->len; i++)

            cout << result->elements[i] << " ";

    cout << "\n\n";

 

    delete a1, a2, p1, p2, result;

    return 0;

}

 

 

Output