common elements about two array c++

Pages: 12
I am searching a code (i am studing at school) for find and print common elements about of two arrays. This print two arrays if thery arent equal but for the common elements? Thank you very much

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 #include <iostream>
using namespace std;

int main() {
     int v1[3] = {1, 5, 7,0, 8 };
   int v2[3] = {5, 3, 2, 6, 4};
   int i = 0;

   for(i=0; i<5; i++) {
      if (v1[i] != v2[i])
     {


     cout << v1[i] << "   " << v2[i] << endl;
      }
   }

   return 0;
}
Last edited on
To print common elements within two arrays, you can use two for-loops.

1
2
3
4
5
6
7
8
9
10
11
int v1[5] = { 1, 5, 7, 0, 8 };
int v2[5] = { 5, 3, 2, 6, 4 };
for(int i = 0; i < 5; i++) {
  for(int j = 0; j < 5; j++) {
    if(v1[i] == v2[j]) {
      cout << "Index " << i << " for v1 equals value v2 at index " << j << endl;
      cout << "v1 right now is: " << v1[i] << endl;
      cout << "v2 right now is: " << v2[j] << endl;
    }
  }
}


Also, your arrays should set to 5, not 3.
Last edited on
closed account (E0p9LyTq)
If I understand your question about common elements correctly......

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
#include <iostream>

int main()
{
   const int SIZE = 5;

   int array1[SIZE] = { 1, 5, 7, 0, 8 };
   int array2[SIZE] = { 5, 3, 2, 6, 4 };

   std::cout << "Mismatched elements:\n";

   for (size_t i = 0; i < SIZE; i++)
   {
      if (array1[i] != array2[i])
      {
         std::cout << array1[i] << "   " << array2[i] << '\n';
      }
   }
   std::cout << '\n';

   for (size_t i = 0; i < SIZE; i++)
   {
      bool in_common = false;

      for (size_t j = 0; j < SIZE; j++)
      {
         if (array1[i] == array2[j])
         {
            in_common = true;
         }
      }

      if (in_common)
      {
         std::cout << array1[i] << " in common in both arrays\n";
      }
   }
}
Mismatched elements:
1   5
5   3
7   2
0   6
8   4

5 in common in both arrays
Hello mpg,

fiji885' code for the for loops is correct. The "cout" statements look good for understanding what is actually happening.

You will find this to work better:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

//using namespace std;  // <--- Best not to use.

int main()
{
	constexpr size_t MAXSIZE{ 5 };

	int v1[MAXSIZE] = { 1, 5, 7, 0, 8 };
	int v2[MAXSIZE] = { 5, 8, 2, 1, 4 };

	for (size_t arr1 = 0; arr1 < MAXSIZE; arr1++)
	{
		for (size_t arr2 = 0; arr2 < MAXSIZE; arr2++)
			if (v1[arr1] == v2[arr2])
				std::cout << v1[arr1] << "   " << v2[arr2] << std::endl;
	}


	return 0;
}

and produces the output of:

1   1
5   5
8   8



Hope that helps,

Andy
Is ok the code of FurryGuy but if i search to use srand it's no good...
What is wrong?

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>

int main()
{
   const int SIZE = 5;

   int array1[SIZE] ;
   int array2[SIZE] ;
srand(time(NULL));
   cout << "Mismatched elements:\n";

   for (int i = 0; i < SIZE; i++)
   { array1[i] = rand() % 10;
   array2[i] = rand() % 10;



      if (array1[i] != array2[i])
      {
         cout << array1[i] << "   " << array2[i] << '\n';
      }
   }
   cout << '\n';

   for (size_t i = 0; i < SIZE; i++)
   {
      bool in_common = false;

      for (int j = 0; j < SIZE; j++)
      {
         if (array1[i] == array2[j])
         {
            in_common = true;
         }
      }

      if (in_common)
      {
         cout << array1[i] << " in common in both arrays\n";
      }
   }
}
Intersection of sets:
http://www.cplusplus.com/reference/algorithm/set_intersection/


BTW, what is common in these?
1
2
{1, 2, 1, 5, 2}
{3, 1, 4, 1, 2}

(There are at least two "correct" answers.)
closed account (E0p9LyTq)
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 <ctime>    // for std::time
#include <cstdlib>  // for std::srand & std::rand

int main()
{
   // seed the random number generator with the current time
   // the static_cast stops the compiler from complaining about the output of std::time
   // not being what std::srand needs as an argument, possible loss of data
   std::srand(static_cast<unsigned>(std::time(nullptr)));

   const int SIZE = 5;

   int array1[SIZE];
   int array2[SIZE];

   // fill the arrays with random numbers between 1 & 10
   for (size_t i = 0; i < SIZE; i++)
   {
      array1[i] = std::rand() % 10 + 1;
      array2[i] = std::rand() % 10 + 1;
   }

   std::cout << "array1:";
   for (size_t i = 0; i < SIZE; i++) std::cout << ' ' << array1[i];
   std::cout << '\n';

   std::cout << "array2:";
   for (size_t i = 0; i < SIZE; i++) std::cout << ' ' << array2[i];
   std::cout << "\n\n";

   std::cout << "Mismatched elements:\n";

   for (size_t i = 0; i < SIZE; i++)
   {
      if (array1[i] != array2[i])
      {
         std::cout << array1[i] << "   " << array2[i] << '\n';
      }
   }
   std::cout << '\n';

   for (size_t i = 0; i < SIZE; i++)
   {
      bool in_common = false;

      for (size_t j = 0; j < SIZE; j++)
      {
         if (array1[i] == array2[j])
         {
            in_common = true;
         }
      }

      if (in_common)
      {
         std::cout << array1[i] << " in common in both arrays\n";
      }
   }
}
array1: 5 8 5 9 2
array2: 7 1 2 3 1

Mismatched elements:
5   7
8   1
5   2
9   3
2   1

2 in common in both arrays


If you are going to write C++ code why not use the C++ random engines?
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
#include <iostream>
#include <chrono>   // for std::system_clock
#include <random>   // for random engines & distributions

int main()
{
   // construct a trivial random generator engine from a time-based seed:
   unsigned seed = static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count());
   std::default_random_engine generator(seed);

   // construct an int distibution from 1 to 10 inclusive
   std::uniform_int_distribution<int> distribution(1, 10);
   const int SIZE = 5;

   int array1[SIZE];
   int array2[SIZE];

   // fill the arrays with random numbers between 1 & 10
   for (size_t i = 0; i < SIZE; i++)
   {
      array1[i] = distribution(generator);
      array2[i] = distribution(generator);
   }

   std::cout << "array1:";
   for (size_t i = 0; i < SIZE; i++) std::cout << ' ' << array1[i];
   std::cout << '\n';

   std::cout << "array2:";
   for (size_t i = 0; i < SIZE; i++) std::cout << ' ' << array2[i];
   std::cout << "\n\n";

   std::cout << "Mismatched elements:\n";

   for (size_t i = 0; i < SIZE; i++)
   {
      if (array1[i] != array2[i])
      {
         std::cout << array1[i] << "   " << array2[i] << '\n';
      }
   }
   std::cout << '\n';

   for (size_t i = 0; i < SIZE; i++)
   {
      bool in_common = false;

      for (size_t j = 0; j < SIZE; j++)
      {
         if (array1[i] == array2[j])
         {
            in_common = true;
         }
      }

      if (in_common)
      {
         std::cout << array1[i] << " in common in both arrays\n";
      }
   }
}
array1: 7 9 10 6 7
array2: 5 4 9 10 5

Mismatched elements:
7   5
9   4
10   9
6   10
7   5

9 in common in both arrays
10 in common in both arrays


When writing C++ code IMO it is better to use the C++ constructs. Even for such trivial uses as this. The C library functions have problems.
At school I didn't study "std::srand(static_cast<unsigned>(std::time(nullptr))) " and also
I dont use "std::"
The program that I must use is codeblock and with it I have another result....
particularry for Mismatched elements.
See the link:
https://www.dropbox.com/s/zt59ymj9u7icyfe/elements.jpg?dl=0

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 <ctime>    // for std::time
#include <cstdlib>  // for std::srand & std::rand

int main()
{
   // seed the random number generator with the current time
   // the static_cast stops the compiler from complaining about the output of std::time
   // not being what std::srand needs as an argument, possible loss of data
   std::srand(static_cast<unsigned>(std::time(nullptr)));

   const int SIZE = 5;

   int array1[SIZE];
   int array2[SIZE];

   // fill the arrays with random numbers between 1 & 10
   for (size_t i = 0; i < SIZE; i++)
   {
      array1[i] = std::rand() % 10 + 1;
      array2[i] = std::rand() % 10 + 1;
   }

   std::cout << "array1:";
   for (size_t i = 0; i < SIZE; i++) std::cout << ' ' << array1[i];
   std::cout << '\n';

   std::cout << "array2:";
   for (size_t i = 0; i < SIZE; i++) std::cout << ' ' << array2[i];
   std::cout << "\n\n";

   std::cout << "Mismatched elements:\n";

   for (size_t i = 0; i < SIZE; i++)
   {
      if (array1[i] != array2[i])
      {
         std::cout << array1[i] << "   " << array2[i] << '\n';
      }
   }
   std::cout << '\n';

   for (size_t i = 0; i < SIZE; i++)
   {
      bool in_common = false;

      for (size_t j = 0; j < SIZE; j++)
      {
         if (array1[i] == array2[j])
         {
            in_common = true;
         }
      }

      if (in_common)
      {
         std::cout << array1[i] << " in common in both arrays\n";
      }
   }
}
Last edited on
For the other codes if I use two equal numbers the codes aren't good.....

You can try these and see the problems:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()

{
	constexpr size_t MAXSIZE{ 5 };

	int v1[MAXSIZE] = { 1, 1, 7, 0, 8 };
	int v2[MAXSIZE] = { 5, 1, 2, 1, 4 };

	for (size_t arr1 = 0; arr1 < MAXSIZE; arr1++)
	{
		for (size_t arr2 = 0; arr2 < MAXSIZE; arr2++)
			if (v1[arr1] == v2[arr2])
				std::cout << v1[arr1] << "   " << v2[arr2] << std::endl;
	}


	return 0;
}




or this
1
2
3
4
5
6
7
8
9
10
11
int v1[5] = { 1, 1, 7, 0, 8 };
int v2[5] = { 5, 1, 1, 7, 4 };
for(int i = 0; i < 5; i++) {
  for(int j = 0; j < 5; j++) {
    if(v1[i] == v2[j]) {
      cout << "Index " << i << " for v1 equals value v2 at index " << j << endl;
      cout << "v1 right now is: " << v1[i] << endl;
      cout << "v2 right now is: " << v2[j] << endl;
    }
  }
}
Last edited on
Those two versions are in practice logically identical.

What "problems"?

What do you want to happen in the "repeated values" case? Be exact.
For example:

array1: 7 9 6 9 5
array2: 5 4 9 10 5

Mismatched elements:
7 5
9 4
6 9
9 10
5 5


So the results must be only
9 in common in both arrays
5 in common in both arrays

I don't know if I explain well...
Last edited on
Unique values that occur in both sets.

http://www.cplusplus.com/reference/algorithm/unique/

I.e. something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int v1[MAXSIZE];
int v2[MAXSIZE];
// add data

std::sort( v1, v1 + MAXSIZE );
auto u1 = std::unique( v1, v1 + MAXSIZE );
std::sort( v2, v2 + MAXSIZE );
auto u2 = std::unique( v2, v2 + MAXSIZE );
int v3[MAXSIZE];
auto u3 = std::set_intersection( v1, u1, v2, u2, v3 );

// show common
for ( int* e = v3; e < u3; ++e ) {
  std::cout << *e << '\n';
}


But ... you "don't do that" ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int v1[NA];
int v2[NB];

int set[NA];
size_t ns=0; // size of set
for ( int x : v1 ) {
  // if set does not contain x
  // then append x to set
}

for ( size_t e=0; e < ns; ++e ) {
  // if v2 contains set[e]
  // then print set[e]
}
Last edited on
http://www.cplusplus.com/forum/beginner/250293/#msg1102487

I wish a code more simple, at school we haven' t study "auto" or "set_intersection(" "int set"

I have done this but I noticed if by chance two of the numbers are 6 and 0 in the first array and in the second array of the 5 numbers there is twice 0 as a result I get out 6 0 0, should come instead 6 and 0 as common I do not understand how I should correct ...

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

using namespace std;
int main(){

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

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

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

    cout<<"elementi comuni"<<endl;

    for (int i = 0; i <5; i++){
        for (int j = 0; j <5; j++){

                if (num1[i] == num2[j])
                    {
                cout<<" "<<num1[i];
                }
               
        }
    }
}
Last edited on
Hello mpg,

I think this may be simple enough for what you know:
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
#include <iostream>

//using namespace std;  // <--- Best not to use.

int main()
{
	constexpr size_t MAXSIZE{ 5 };  // <--- or "const unsigned int MAXSIZE{ 5 };" either will work.

	int v1[MAXSIZE]{ 1, 5, 7, 10, 8 };
	int v2[MAXSIZE]{ 5, 8, 22, 1, 4 };
	int dups[MAXSIZE]{};

	// <--- Finds numbers that match.
	for (size_t arr1 = 0; arr1 < MAXSIZE; arr1++)
	{
		for (size_t arr2 = 0; arr2 < MAXSIZE; arr2++)
			if (v1[arr1] == v2[arr2])
			{
				//std::cout << v1[arr1] << "   " << v2[arr2] << std::endl;  // <--- Used for testing.
				dups[arr1] = v1[arr1];
			}
	}

	std::cout << "\n Mismatched numbers:" << std::endl;

	for (int lc = 0; lc < MAXSIZE; lc++)
		if (v1[lc] != v2[lc])
			std::cout << ' ' << (v1[lc] < 10 ? " " : "") << v1[lc] << "   " << (v2[lc] < 10 ? " " : "") << v2[lc] << std::endl;

	std::cout << std::endl;

	for (int lc = 0; lc < MAXSIZE; lc++)
	{
		if (dups[lc])
			std::cout << ' ' << dups[lc] << " is common in both arrays." << std::endl;
	}

	return 0;
}

"constexpr" (from C++11 on) is about the same as "const". I am trying to use the C++11 standards and not the older versions. If "constexpr" does not work for you then your compiler would need to be adjusted to use the C++11 standards. I found this to be needed with the Dev C++ IDE. Not sure about CodeBlocks.

The "size_t" is usually a typedef for "unsigned int", but I have seen setups that give this a type def of "long" or "int_64", ( same as a long).

When defining a constant it is generally accepted to use capital letters for the variable name.

The {}s whether empty or containing a number is called an "uniform initializer" available from C++11 on. Very handy. The "=" are not needed.

You can see in defining the arrays and in the for loops how using "MAXSIZE" makes things much easier. This way you only have one place to make changes and it is used and changes everywhere in the program.

Defining variables as a constant is useful for things like "SALESTAX", some kind of "TAXRATE" or "DISCOUNTRATE" as examples.

I thought someone mentioned this earlier, but I must be thinking about something else.

The first nested for loops are used to check each element of "v1" with every element of "v2" to see if there is a matching number. I used the extra array to store these matches for later use.

The next for loop prints out the mismatched elements of both arrays. The if statement only prints elements of the array elements that do not match.

The last for loop prints out the numbers that match based on the contents of the "dups" array. The if statements works on the principal that what is between the () evaluates to either false (0) or true (1). Actually the (1) can be any number greater than zero.

This may be new to you (v1[lc] < 10 ? " " : ""). It uses the ternary operator "?" and ":". you can think of it as a shortened if/else statement equilivant to:
1
2
3
4
if (v1[cl] < 10)
    std::cout << " ";  // <--- prints a space.
else
    std::cout << "";  // <--- prints nothing. 

This may help: http://www.cplusplus.com/articles/1AUq5Di1/ It is the first link I found when doing a search here on "ternary operator". Worth looking into.

The point is that it will help line up the numbers in the output.

 Mismatched numbers:
  1    5
  5    8
  7   22
 10    1
  8    4

 1 is common in both arrays.
 5 is common in both arrays.
 8 is common in both arrays.



And when an element of each array have the same number I get this:

 Mismatched numbers:
  1    5
  5    8
 10    1
  8    4

 1 is common in both arrays.
 5 is common in both arrays.
 22 is common in both arrays.
 8 is common in both arrays.


The third element of each array is 22.

At school I didn't study "std::srand(static_cast<unsigned>(std::time(nullptr))) "

Not a problem.

"srand" as defined void srand (unsigned int seed);.

http://www.cplusplus.com/reference/cstdlib/srand/?kw=srand

As you can see the value passed to "srand" needs to be an "unsigned int". The static_cast<type>. In this "type" can be replaced with "unsigned", "unsigned int" or "size_t". This is needed because "time()" does not return an "unsigned int" as "srand" needs.

When calling the "time" function what is passed could be zero or "NULL", but as I have recently learned "nullptr" is the best choice.

and also I don't use "std::"


If you are going to use the line using namespace std; then you will need to learn everything that is in the standard namespace so you do not use a variable or function name that is in the standard name space because it will give you an error. Or you could learn to qualify things like

std::cin
std::cout
std::endl
std::string when including the header file "string"
std::vector when including the header file "vector"

std::setw() These when including the header file "iomanip"
std::fixed
std::setprecision
std::left
std::right


These, I would say are the most common, you are likely to use for now. The compiler and a good IDE can help you learn others. I know that you have paid a lot of money for someone to teach you the incorrect or easy way to write source code. In the end this is not helping you. And what happens when you finish all your schooling and your first employment says: "you will no use using namespace std; in any code you write. If you have not learned slowly over time you will have to learn it all at once.

If you do a search here there ate many posts to cover this topic that are worth finding and reading.

All the examples given earlier are good and worth copying and going back to in the future when you understand more.

Hope that helps,

Andy
Thank you for the great explaination !!

For me the code is more simple like this. I live in Italy and I must use what the teacher teach me, but really thank you for the solution.
The only thing that I don't understand well is this:"cout << ' ' << (v1[lc] < 10 ? " " : "") << v1[lc] << " " << (v2[lc] < 10 ? " " : "") << v2[lc] << endl;"
Is it possible to change this in another method because I can't use this because I don't study it at school so I can't use...

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

using namespace std;
//using namespace std;  // <--- Best not to use.

int main()
{
	int const MAXSIZE{ 5 };  // <--- or "const unsigned int MAXSIZE{ 5 };" either will work.

	int v1[MAXSIZE]{ 1, 5, 7, 10, 8 };
	int v2[MAXSIZE]{ 5, 8, 22, 1, 4 };
	int dups[MAXSIZE]{};

	// <--- Finds numbers that match.
	for (int i = 0; i < MAXSIZE; i++)
	{
		for (int j = 0; j< MAXSIZE;  j++)
			if (v1[i] == v2[j])
			{
				//std::cout << v1[arr1] << "   " << v2[arr2] << std::endl;  // <--- Used for testing.
				dups[i] = v1[i];
			}
	}

	cout << "\n Mismatched numbers:" << std::endl;

	for (int lc = 0; lc < MAXSIZE; lc++)
		if (v1[lc] != v2[lc])
			cout << ' ' << (v1[lc] < 10 ? " " : "") << v1[lc] << "   " << (v2[lc] < 10 ? " " : "") << v2[lc] << endl;

	cout << endl;

	for (int lc = 0; lc < MAXSIZE; lc++)
	{
		if (dups[lc])
	 cout << ' ' << dups[lc] << " is common in both arrays." << endl;
	}

	return 0;
}
Last edited on
Also this really is good (of FurryGuy):


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 <ctime>    // for std::time
#include <cstdlib>  // for std::srand & std::rand
using namespace std;


int main()
{
   const int SIZE = 5;

   int array1[SIZE] = { 6, 5, 3, 0, 8 };
   int array2[SIZE] = { 5, 3, 5, 6, 4 };

    cout << "Mismatched elements:\n";

   for (size_t i = 0; i < SIZE; i++)
   {
      if (array1[i] != array2[i])
      {
      cout << array1[i] << "   " << array2[i] << '\n';
      }
   }
   cout << '\n';

   for (size_t i = 0; i < SIZE; i++)
   {
      bool in_common = false;

      for (size_t j = 0; j < SIZE; j++)
      {
         if (array1[i] == array2[j])
         {
            in_common = true;
         }
      }

      if (in_common)
      {
     cout << array1[i] << " in common in both arrays\n";
      }
   }
}


Last edited on
keskiverto wrote:
Unique values that occur in both sets.

I would read what keskiverto wrote twice, and also carefully: if you are able to describe the problem accurately, you can write the required code as well.

You have already seen how to compare two arrays:
1
2
3
4
for every value in array 'one'
    compare that value to all the values in array 'two'
    if they match...
        ...do something


If you want to discard all the matches after the first, you need to perform a third loop:
1
2
3
4
5
6
7
for every value in array 'one'
    compare that value to all the values in array 'two'
    if they match
        if I have already met such a value...
            ...do nothing
        else
            ...do something

Hello mpg,

If you are not ready for (v1[lc] < 10 ? " " : "") yet that is understandable you can just leave it out for now.

The code from FurryGuy is good. He did in two for loops what I showed you in three.

What I did was to give you a better idea of how it worked.

Andy
Hello mpg,

I adjusted the program for some fun.

This: cout << ' ' << (v1[lc] < 10 ? " " : "") << v1[lc] << " " << (v2[lc] < 10 ? " " : "") << v2[lc] << endl;

Can be replaced with:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for (int lc = 0; lc < MAXSIZE; lc++)
{
	if (v1[lc] != v2[lc])
	{
		std::cout << ' ';

		if (v1[lc] < 10)
			std::cout << " ";

		std::cout << v1[lc] << "   ";

		if (v2[lc] < 10)
			std::cout << " ";

		std::cout << v2[lc] << std::endl;
	}
}


Hope that helps,

Andy
There was nothing too novel in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int v1[NA];
int v2[NB];

int set[NA];
size_t ns=0; // size of set
for ( int x : v1 ) {
  // if set does not contain x
  // then append x to set
}

for ( size_t e=0; e < ns; ++e ) {
  // if v2 contains set[e]
  // then print set[e]
}

except perhaps the range-based for loop syntax.


We can expand:
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
// a function that takes an array foo
// of n integers and returns true, if at least one of them equals v
bool contains( const int* foo, size_t n, int v );

int main() {
  constexpr size_t NA = 5;
  constexpr size_t NB = 7;
  int v1[NA];
  int v2[NB];
  // I don't care how you put values into the arrays

  int set[NA];
  size_t ns=0; // size of array set
  for ( size_t e=0; e < NA; ++e ) {
    if ( ! contains( set, ns, v1[e] ) {
      set[ns] = v1[e];
      ++ns;
    }
  }

  int common[NA];
  size_t nc=0;
  for ( size_t e=0; e < ns; ++e ) {
    if ( contains( v2, NB, set[e] ) {
      common[nc] = set[e];
      ++nc;
    }
  }

  // mismatches in v1:
  for ( size_t e=0; e < NA; ++e ) {
    if ( ! contains( common, nc, v1[e] ) {
      // print v1[e]
    }
  }

  // mismatches in v2:
  for ( size_t e=0; e < NB; ++e ) {
    if ( ! contains( common, nc, v2[e] ) {
      // print v2[e]
    }
  }

  // common values:
  for ( size_t e=0; e < nc; ++e ) {
    // print common[e]
  }
}


bool contains( const int* foo, size_t n, int v )
{
  for ( size_t e=0; e < n; ++e ) {
    if ( foo[e] == v ) {
      return true;
    }
  }
  return false;
}
Pages: 12