Arrays problem

I have to write a program which would compare two arrays and then displays the elements which are same in two arrays. I have written this program but i am not getting the right result. Kindly help me!

#include <iostream.h>
#include <conio.h>


int main()
{
int i,j,m,k[3],count=0;
int array[3];
cout<<"please enter the elements of first array"<<endl;

for (i=0; i < 3; i++)
{
cin>>array[i];
}
cout<<"please enter the elements of second array"<<endl;
for ( j=0; j<3; j++)
{
cin>>array[j];
}

for (i=0;i<3; i++)
{
for(j=0; j<3; j++)
{
for(m=0; m<3; m++)
{
if(array[i] == array[j])

{
k[m] = array[i];
}

}
}
}

for(m=0; m<3; m++)
{
cout<<"the number of same values is"<<k[m]<<endl;
}

}
* you read in the two sets of numbers in the same array
* you don't count the number of "same values"


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream.h>
#include <conio.h>


int main()
{
int i,j,m,k[3],count=0;
int array[3];
int array2[3];
int sameCount = 0;
cout<<"please enter the elements of first array"<<endl;

for (i=0; i < 3; i++)
{
cin>>array[i];
}
cout<<"please enter the elements of second array"<<endl;
for ( j=0; j<3; j++)
{ 
cin>>array[i];
cin>>array2[j];
}

for (i=0;i<3; i++)
{
for(j=0; j<3; j++)
{

for(m=0; m<3; m++)
{
if(array[i] == array[j])
if(array[m] == array2[m])
{
k[m] = array[i];
++sameCount;
}

}
}
}


for(m=0; m<3; m++)
{
cout<<"the number of same values is"<<k[m]<<endl;
cout<<"the number of same values is"<<sameCount<<endl;
}

}
but that just gives the number of same values... can we display the same elements aswell? p.s i m a business student... i do this just for fun so dont mind the silly mistakes :D
closed account (yUq2Nwbp)
you must just write

if( array[m] == array2[m])
std::cout<<array[m];

...that is all
Thank you Onur and David!
closed account (yUq2Nwbp)
you are quite welcome
Topic archived. No new replies allowed.