Issue with figuring out function to compare 2 arrays

Write a program that will store 7 integers (entered by the user) into an array called list1. Next, prompt the user for 5 integers and put them into an array called list2. Now, list all of the numbers that are in BOTH lists by calling a function called inBoth and passing list1 and list2 to that function.

I understand how to create the 2 arrays, Im just really lost on how to program the function that will compare them and figure out if there are any of the same numbers.
You use a for loop that is the size of the biggest array, in this case 7.

1
2
3
4
5
6
7
for(int i = 0; i < 7; i++)
{
   if(list1[i] == list2[i])
    {
        //  Print out that number for example.
    }
}


Edit: You'd need a lested loop.
Last edited on
it has to look like this:
List 1
------
Enter an integer: 4
Enter an integer: 2
Enter an integer: 7
Enter an integer: 1
Enter an integer: 5
Enter an integer: 6
Enter an integer: 3

List 2
------
Enter an integer: 11
Enter an integer: 9
Enter an integer: 7
Enter an integer: 5
Enter an integer: 3

The numbers that are in both lists are:
3, 5, 7

and I dont know how to put that into a function and then call it.

This is the code I have so far:
int main() {

int list1[7];
int list2[5];

cout << "List 1" << endl;
cout << "------" << endl;

for( int x = 0; x < 7; x++){
cout << "Enter an integer: ";
cin >> list1[x];

}

cout << "List 2" << endl;
cout << "------" << endl;

for (int x = 0; x < 5; x++) {
cout << "Enter an integer: ";
cin >> list2[x];
}

@kle5208

Do you know how to pass parameters into functions, and how to call functions? Your function would probably look something like this:

1
2
3
4
void inBoth(int list1[], int list2[], unsigned int size1, unsigned int size2)
{
    //code for outputting the numbers here
}


And you would call it like this:

inBoth(list1, list2, 7, 5);

@TarikNeaj

That would lead to a segmentation fault since list2 only has 5 elements. It also doesn't work.
Last edited on
@fg109 Yeh lol, You'd need a nested loop. Also, Works fine for me, Ive never had segmentation fault when Im just trying to compare out-of-bounds arrays. Im using vs 13, I googled around and its normal that it works. Doesnt work with vectors though.
@TarikNeaj

I guess you're right. I usually don't work with out of bounds arrays so I thought that's what would happen, like with linked lists as such.

@kle5208

Here's an example of one way you can do it. You'll have to convert the code to work with arrays though, which I am sure would be a lot more trouble than doing it TarikNeaj's way. It doesn't really handle duplicates that well either.

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
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>

template <class T>
void inBoth(std::vector<T> a, std::vector<T> b)
{
	std::sort(a.begin(), a.end());
	std::sort(b.begin(), b.end());
	auto ita = a.begin();
	auto itb = b.begin();
	auto lasta = std::unique(a.begin(), a.end());
	auto lastb = std::unique(b.begin(), b.end());
	while (ita != lasta && itb != lastb)
	{
		if (*ita == *itb)
		{
			std::cout << *ita << ' ';
			++ita;
			++itb;
		}
		else if (*ita > *itb)
			++itb;
		else
			++ita;
	}
}

int main()
{
	srand(time(0));
	std::vector<int> list1;
	std::vector<int> list2;
	for (int i = 0; i < 7; ++i)
		list1.push_back(rand() % 10 + 1);
	for (int i = 0; i < 5; ++i)
		list2.push_back(rand() % 10 + 1);
	inBoth(list1, list2);
	return 0;
}

I originally wanted to use std::list but it doesn't have a random access iterator for std::sort.
Last edited on
Topic archived. No new replies allowed.