Comparing two arrays.

I am trying to compare two arrays to find duplicate values in them and be able to display them. I can't seem to be able to display the duplicate values between them. Here is my code any help is appreciated.
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "stdafx.h"
#include <conio.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

void sort(int x[], int n);
int main()
	{
	string filename, filename1;
	int c, d, x = 0, y = 0;
	int chiwarehouse[25],detwarehouse[25];
	cout << "How many numbers do you want to enter? ";
    cin >> c;
	cout << "How many numbers do you want to enter? ";
    cin >> d;
	cout << "Enter the name of the Chicago file that holds the product numbers.\n";
	cin >> filename;
	ifstream Chicago(filename.c_str());
		if(Chicago. fail())
			{
			cerr << "could not open input file\n" << filename << endl;
			cout << endl << "Press any key to continue...";
			_getch();
			return 0;
			}

	cout << "Enter the name of the Detroit file that holds the product numbers.\n";
	cin >> filename1;
	ifstream Detroit(filename1.c_str());
		if(Detroit. fail())
			{
			cerr << "could not open input file\n" << filename1 << endl;
			cout << endl << "Press any key to continue...";
			_getch();
			return 0;
			}

	ofstream Intersection("E:\\intersection.txt", ios::app);
		if(Intersection. fail())
			{
			cerr << "could not open output file\n" << Intersection << endl;
			cout << endl << "Press any key to continue...";
			_getch();
			return 0;
			}	

		for(int j = 0; j < c; ++j)
			{
			Chicago >> chiwarehouse[j];
			}
		for(int j = 0; j < d; ++j)
			{
			Detroit >> detwarehouse[j];
			}
		for(int i = 0; i < c; i ++)
			{
			sort(chiwarehouse, c);
			cout << "\n" << setw(6) << i << "  " << chiwarehouse[i]; 
			}		
		for(int i = 0; i < d; i ++)
			{
			sort(detwarehouse, d);
			cout << "\n" << setw(6) << i << "  " << detwarehouse[i];
			}	
		while (x < c && y < d) 
			{
			if (chiwarehouse[c] == detwarehouse[d]) 
				{
				 cout << "\n" << detwarehouse[y];
				 x++;
				 y++;
				}
			}

	Chicago.close();
	Detroit.close();
	Intersection.close();
	cout << endl << "Press any key to continue...";
	_getch();
	return 0;
	}

void sort(int x[], int n)
{
int m;
int hold;
for (int k=0; k<=n-2; ++k)
{
	m=k;
	for (int j=k+1; j<=n-1; ++j)
	{
		if (x[j] < x[m])
			m=j;
	}
	hold = x[m];
	x[m] = x[k];
	x[k] = hold;
}
return;
}

Took me ages to see this but the problem is here:
1
2
3
4
5
6
7
8
9
10
		
while (x < c && y < d) 
{
     if (chiwarehouse[c] == detwarehouse[d])  //using wrong variables for the check
    {
        cout << "\n" << detwarehouse[y];
        x++;
        y++;
    }
}

It should be:
if (chiwarehouse[x] == detwarehouse[y])

PS - Will this code work if the user enter different valuesfor the Chicago and Detroit numbers????
It should work with other numbers but it's not working at the moment. When I did what you told me to do the program would just sit there and I would have to close it out manually. After asking for some advice someone told me to try this instead but this doesn't work properly as it only displays the first number in the chiwarehouse array when is sorted and not the duplicate values between them.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (chiwarehouse[x] == detwarehouse[y]) 
				{
				 x++;
				 y++;
				}
			else if (chiwarehouse[x] < detwarehouse[y]) 
				{
				x++;
				}
			else 
				{
				y++;
				}
			}
				 cout << "\n" << chiwarehouse[x];
I got it to work thanks for all the help.
Ah, Its Sunday, my brain must have been having a day off.
I should have noticed that your x++ and y++
was in the wrong place, and was only being increment if there was a duplicate match - that was why the program was hanging

So for the record - the original should have been:
1
2
3
4
5
6
7
8
9
10
11
while (x < c && y < d) 
{
     if (chiwarehouse[x] == detwarehouse[y])      
    {
        cout << "\n" << detwarehouse[y];
    }
   // x and y should be updated here.
   x++;
    y++;

}

Topic archived. No new replies allowed.