Guys Plz help me out of this.... i have to take common values from three arrays and if there is repetition of values code should display value only 1 time... plz help? here is my code`

#include<iostream>
using namespace std;
int main()
{
int count=0;
const int size=5;
int arr1[size];
int arr2[size];
int arr3[size];
int arr4[size];
cout<<"Enter first array:";
for(int i=0;i<size;i++)
cin>>arr1[i];
cout<<"Enter second array:";
for(i=0;i<size;i++)
cin>>arr2[i];
cout<<"Enter third array:";
for(i=0;i<size;i++)
cin>>arr3[i];
for(i=0;i<size;i++)
for(int j=0;j<size;j++)
{
if(arr1[i]==arr2[j] && arr1[i]==arr3[j])
{
arr4[count]=arr1[i];
count++;                
}
}   
if(count==0)
{
cout<<"No elements common found!"<<endl;
}
if (count!=0)
{
cout<<"Common elements are:";
for(int k=0;k<size;k++)
cout<<arr4[k]<<endl;
}
return 0;
}

Edited 3 Years Ago by zubair_z

I think the problem you're having is that arr4 isn't initialized fully, so some of the elements in it have random values. Try changing k<size to k<count

Edited 3 Years Ago by tinstaafl

Try adding another for loop like this:

    for(i=0; i<size; i++)
    {
         for(int j=0; j<size; j++)
        {
            for(int k = 0; k<size; k++)
            {
                if(arr1[i]==arr2[j] && arr1[i]==arr3[k])
                {
                    arr4[count]=arr1[i];
                    count++;
                }
            }

        }
    }

    if(count==0)
    {
        cout<<"No elements common found!"<<endl;
    }
    if (count!=0)
    {
        cout<<"Common elements are:";
        for(int k=0; k<count; k++)
            cout<<arr4[k]<<endl;
    }
This article has been dead for over six months. Start a new discussion instead.