find common elements from two arrays

I try to find the common elements from two arrays. The output shows incomplete common elements. Sometimes it also shows repeating elements.

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

using namespace std;
int main(){
	
	int num1[10]; 
	int num2[10];
	srand(time(NULL));
	cout<<"array 1"<<endl;
	for (int i = 0; i <10; i++){
		num1[i] = rand() % 10;
		
		cout<<num1[i]<<endl;
	}
	cout<<"array 2"<<endl;
	for (int j = 0; j < 10; j++){
		num2[j] = rand() % 10;
		
		cout<<num2[j]<<endl;
	}
	
	cout<<"common elements"<<endl;
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++){
			if (num1[i] < num2[j])
			{
			i++;
			}
			
			else if (num2[j]< num1[i])
			{
			j++;
			} 
			
			else if (num1[i] == num2[j])
			{
			
			cout<<num1[i]<<endl;
			i++;
			j++;
			 
			}
			
		
		}		
	}
	

		return 0;
}


the output:
array 1
7
2
9
0
3
5
0
3
0
6
array 2
4
0
5
6
2
0
2
8
5
0
common elements
5
0
6
your for loops were reading 10 instead of the size of it




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

using namespace std;
int main() {

	int num1[10];
	int num2[10];
	srand(time(NULL));
	cout << "array 1" << endl;
	for (int i = 0; i <(sizeof(num1) / sizeof(num2[0])); i++) {
		num1[i] = rand() % 10;

		cout << " " << num1[i];
	}
	cout << endl;

	cout << "array 2" << endl;
	for (int j = 0; j < (sizeof(num1) / sizeof(num2[0])); j++) {
		num2[j] = rand() % 10;

		cout << " " << num2[j];
	}

	cout << endl;

	cout << "common elements" << endl;
	for (int i = 0; i < (sizeof(num1) / sizeof(num2[0])); i++) 
	{
		{
			for (int j = 0; j < (sizeof(num1) / sizeof(num2[0])); i++)
			{
				if (num1[i] < num2[j])
				{
					i++;
				}

				else if (num2[j] < num1[i])
				{
					j++;
				}

				else if (num1[i] == num2[j])
				{

					cout << " " << num1[i];
					i++;
					j++;

				}
			}
		}

	}

	system("pause");

	return 0;
}
Thank you.

What is the function of "sizeof(num1) / sizeof(num2[0])" ?
What is the function of "sizeof(num1) / sizeof(num2[0])" ?

sizeof(num1) returns the size of the array in bytes.
sizeof(num2[0]) returns the size of the first element in bytes.
Dividing the size of the array by the size of the first elment gives you the number of elements in the array.

IMO, rezy3312's approach is overly pedantic and subject to subtle errors. Note that (sizeof(num1) / sizeof(num2[0]) refers to two different arrays. If the type of one of the two arrays is changed in the future, the result will be wrong. If you're going to use this approach, it is better to use the same array. (sizeof(num1) / sizeof(num1[0]) and (sizeof(num2) / sizeof(num2[0]) when referring to the second array.

A simpler and shorter approach is to create a constant defining the size of the arrays.
 
  const int ARRAY_SIZE = 10;

Now, anywhere you would use 10 or (sizeof(num1) / sizeof(num1[0]), you use ARRAY_SIZE instead.

Last edited on
Thank you AbstractionAnon.

I think the program is correct now. I need to remove repeating elements from the common elements. (e.g. 4,5 instead of 4,4,5,5).



1
2
3
4
5
6
7
8
9
10
11
12
13
	cout<<"common elements"<<endl;
	for (int i = 0; i <size; i++){
		for (int j = 0; j <size; j++){
			
				if (array1[i] == array2[j]){
				cout<<" "<<array1[i];
				}
				/* if (array1[i]==array1[i-1]){
                                array 1[i]==0;                           //the code did not work
                                }*/
				
		}
	}	
Topic archived. No new replies allowed.