Author Topic: VC++ - Intersection of two arrays - finding the common elements  (Read 770 times)

kathir

  • Administrator
  • Sr. Member
  • *****
  • Posts: 283
Here is the VC++ 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.



#include <stdio.h>
#include <iostream>

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)
    {
        this->elements = new int[len];
        this->len = len;
        for(int i = 0; i < len; i++)
            this->elements = data;
    }
    _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;

// 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 == p2->elements[j])
            {
                count++;
                break;
            }
        }
    }

    Array *result = new Array(count);
    count = 0;
    for(int i = 0; i < p1->len; i++)
    {
        for(int j = 0; j < p2->len; j++)
        {
            if(p1->elements == p2->elements[j])
            {
                result->elements[count++] = p1->elements;
                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)
            continue;
        else
        {
            element = p->elements;
            count++;
        }
    }

    Array *result = new Array(count);
    count = 0;
    element = p->elements[0];
    result->elements[count++] = element;
    for(int i = 1; i < p->len; i++)
    {
        if(element == p->elements)
            continue;
        else
        {
            element = p->elements;
            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);

    std::cout << "\n\nUnique Sorted Elements in Array1: ";
    for(int i = 0; i < p1->len; i++)
        std::cout << p1->elements << " ";
    std::cout << "\n\nUnique Sorted Elements in Array2: ";
    for(int i = 0; i < p2->len; i++)
        std::cout << p2->elements << " ";
    std::cout << "\n\nCommon (Intersection of) Elements in Array1 and Array2: ";
    for(int i = 0; i < result->len; i++)
        std::cout << result->elements << " ";
    std::cout << "\n\n";

    delete a1, a2, p1, p2, result;
    return 0;
}
« Last Edit: July 07, 2011, 03:10:15 pm by kathir softwareandfinance »

 


Disclaimer: This web site is for educational and informational purposes only. Click here to read the Disclaimer.
Content copyright 2010-2014. Kathiresan. All rights reserved.
This page is using SMF version 2.0.1