Matching numbers in two arrays

I'm working on some Uni tasks and I've made it 2 thirds of the way through the following but am stuck on the final part.

•Asks the user to enter 5 different numbers (between 1 and 20) and stores them in an array
•Asks the reader to enter another 5 numbers (between 1 and 20) into a second array
///////This is the one I'm stuck on , I've tried lots of different ways nut none seem to want to play ball!!
•Display both arrays on the screen as well as a list of the numbers that appear in both arrays (see example input and output below)


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
#include <iostream>
#include <Windows.h>

using namespace std;

int main()

const int NUM1 = 5;
const int NUM2 = 5;
int numbers1[NUM1];
int numbers2[NUM2];
int sum = 0;
int count = 0;


for (int firstArray = 0; firstArray < NUM1; firstArray++)
{
	cout << (firstArray + 1) << ". " << "Enter a number between 1 and 20 ";
	cin >> numbers1[firstArray];

	while ((numbers1[firstArray] < 0) || (numbers1[firstArray] >20))
	{
		cout << "Please enter a number between 1 and 20 you IDIOT! ";
		cin >> numbers1[firstArray];

	}

}
for (int secondArray = 0; secondArray < NUM2; secondArray++)
{
	cout << (secondArray + 1) << ". " << "Enter a number between 1 and 20 ";
	cin >> numbers2[secondArray];

	while ((numbers2[secondArray] < 0) || (numbers2[secondArray] >20))
	{
		cout << "Please enter a number between 1 and 20 you IDIOT! ";
		cin >> numbers2[secondArray];

	}
	
}

cout << "The numbers entered by you in the first array were:" ;

for (int i = 0; i < NUM1; i++)
{
	cout << numbers1[i]<< ", ";
}

cout << "\n\nThe numbers entered by you in the second array were:";

for (int j = 0; j < NUM2; j++)
{
	cout << numbers2[j]<< ", ";
}

	system("pause");
	return 0;
}


Thought would be hugely appreciated. I'm sure the solution is simple I just cant see the light through the trees right now :(
Last edited on
This is the one I'm stuck on , I've tried lots of different ways nut none seem to want to play ball!!


Why don't you show us what you've tried and maybe we can help you from there?
Last edited on
closed account (48T7M4Gy)
1. Enter a number between 1 and 20 2
2. Enter a number between 1 and 20 3
3. Enter a number between 1 and 20 4
4. Enter a number between 1 and 20 5
5. Enter a number between 1 and 20 6
1. Enter a number between 1 and 20 7
2. Enter a number between 1 and 20 8
3. Enter a number between 1 and 20 9
4. Enter a number between 1 and 20 3
5. Enter a number between 1 and 20 5
The numbers entered by you in the first array were:2, 3, 4, 5, 6, 

The numbers entered by you in the second array were:7, 8, 9, 3, 5, 
All I can see is you missed an opening brace after line 6
Display both arrays on the screen as well as a list of the numbers that appear in both arrays


I'm not sure I even know where to start with this in one tbh. I've just tried various random attempts.
closed account (48T7M4Gy)
I'm not sure I even know where to start with this in one tbh.

Well, you should start with some pseudocode, and here is a start at that:

- take first number from the first array
- does it appear in the second array?
- if it does then display it

- blah blah

You're really wasting your time if you don't have a plan before coding. :)
I understand the question but I don't know how to write the code. It's not really a lack of planning. More a case of purely and simply having hit a brick wall tbh. It's very rare that I ask for help, I feel a lot more satisfaction knowing I figured it out without having to ask but this time I really am stuck and I find arrays confusing at the best of times in any case.

Thanks for the advice though :)

What you said makes sense >> Does the number in the first slot of the first array match any of the numbers in array 2 memory slots? If so, remember those numbers so that I can output them when we have looped through all 5 numbers in the first array.

See I can say it, but I cant figure out how to code it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int count1 = 0;
int count2 = 0;
int k = 0;
int l = 0;

......
......

for (; k < 6; k++)
{
	for (; l < 6; l++)

	if (numbers1[1,2,3,4,5] == numbers2[1,2,3,4,5])

	{
		count1++;
		count2++;
	}
}


I sort of knew this want going to work but it was one of my attempts.

closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using std::cout;
using std::endl;

int main()
{
	int limit = 5;
	
	int array1[] = { 1,2,3,6,5 };
	int array2[] = { 4,1,2,5,8 };

	for (int i = 0; i < limit; i++)
	{
		for (int j = 0; j < limit; j++)
		{
			if (array2[j] == array1[i])
				cout << array2[j] << endl;
		}
	}
return 0;
}
1
2
5
 
Exit code: 0 (normal program termination)
Last edited on
1
2
3
4
5
6
7
8
9
10
11
cout << "\n\nThe matching numbers were ";

for (int i = 0; i < limit; i++)
{
	for (int j = 0; j < limit; j++)
	{
		if (numbers1[j] == numbers2[i])
			cout << numbers2[j] << ", ";
	}
}
cout << "\n\n";


kemort got it! Thank you so much :)
closed account (48T7M4Gy)
:)
Topic archived. No new replies allowed.