It is necessary to combine two arrays into one and leave only non-common elements in it. For example, arr1 {1,2,3,4} + arr2 {1,1,5,6}, the result is arr3 {3,4,6}.

const int SIZE1 = 8; const int SIZE2 = 5; int arr1[SIZE1] = { 0 }; int arr2[SIZE2] = { 0 }; cout << "First array\n"; for (int i = 0; i < SIZE1; i++){ arr1[i] = rand() % 20; cout << arr1[i] << '\t'; } cout << "\nSecond array\n"; for (int i = 0; i < SIZE2; i++){ arr2[i] = rand() % 20; cout << arr2[i] << '\t'; } cout << endl; const int SIZE3 = SIZE1 + SIZE2; int arr3[SIZE3] = { 0 }; cout << "Third array\n"; for (int i = 0; i < SIZE3; i++){ arr3[i] = arr1[i]; if (i >= SIZE1) arr3[i] = arr2[i-SIZE1]; cout << arr3[i] << '\t'; } cout << endl; const int SIZE4 = SIZE3; int arr4[SIZE4] = { 0 }; int size4 = 0; for (int i = 0; i < SIZE4; i++){ for (int j = 0; j < SIZE4; j++){ if (arr3[i] == arr3[j]&&i!=j) break; else if (arr3[i] != arr3[j]&&j<SIZE4-1) continue; else if (arr3[i] != arr3[SIZE4-1]&&j==SIZE4-1) { arr4[size4] = arr3[i]; size4++; } } } cout << "Sorted array\n"; for (int i = 0; i < size4; i++){ cout << arr4[i] << '\t'; } cout << endl; 

}

  • one
    In general, in C ++, std :: set_difference is intended for this. - PinkTux
  • one
    @PinkTux not, std :: set_symmetric_difference () - gbg
  • @gbg, yes, yes, I haven’t been at school for a long time :) - PinkTux
  • Thinking again, I decided to simply merge the two arrays into the third. And then make a check on the inequality of the elements And write the new values ​​in the 4th array. - Anastasiia Melnyk

3 answers 3

In C++ all the necessary functionality is already there, you just need to use it.

The sought is called "Symmetric set difference".

 #include <iostream> #include <set> #include <vector> #include <algorithm> #include <iterator> using std::set; using std::vector; using std::set_symmetric_difference; using std::ostream_iterator; using std::cout; int main() { vector<int> first={7,1,1,2,3}; vector<int> second={6,1,2,5,6,7}; const set<int> alpha(begin(first),end(first)); const set<int> beta(begin(second),end(second)); ostream_iterator<int> out(cout,", "); set_symmetric_difference(begin(alpha),end(alpha),begin(beta),end(beta),out); return 0; } 

IDEONE

First we set our arrays first and second .

Then, convert to set - std::set

Then, we find the difference and immediately issue it to the terminal;

Afterword

I understand perfectly well that this is a “training” task aimed at inventing a bicycle, but. Such a task does not prepare for practice. Because in reality, all C++ programmer jobs require STL knowledge. And our bad education STL ignores.

  • four
    And our bad education STL ignores I would put it differently. If you prepare an artisan coder, yes, he just needs to give ready-made solutions and beat his hands for trying to understand . But in order for a person to understand STL, he must not only ride a bicycle during his studies, but also invent it - only this way he will understand what works and when and what should be used. The invention of a bicycle under the guidance of a teacher is a very good approach. IMHO, of course. You have beautifully and effectively, and even effectively solved the problem, but the main thing is to teach - it remains beyond ... :( - Harry
  • 3
    @Harry just needs to seamlessly integrate STL into the learning process. In mathematics, for some reason, students are not forced to reprint all the analysis on their own, but here - please. - gbg
  • 3
    And our bad education STL ignores - our education is inert to the horror. Manuals are not just of the last century, but of the last century with a tail, this is normal. And hell there is a step to the right, a step to the left. Not to mention the fact that an associate professor, who drags on himself a dozen or two students, and receiving with all compensations 20 thousand ... To whom it fell ... - PinkTux
  • @gbg well, I would argue, I remember the proofs of the main theorems from the course of Matalysis ... - pavel

So, the solution that I implemented:

 const int SIZE1 = 5; const int SIZE2 = 7; int arr1[SIZE1] = { 0 }; int arr2[SIZE2] = { 0 }; for (int i = 0; i < SIZE1; i++) cout << (arr1[i] = rand() % 10) << "\t"; cout << endl; for (int i = 0; i < SIZE2; i++) cout << (arr2[i] = rand() % 10) << "\t"; cout << endl; const int SIZE3 = SIZE2+SIZE1; int arr3[SIZE3] = { 0 }; int size3 = 0; //сравниваем 1 со вторым, разницу записыв в 3 for (int i = 0; i < SIZE1; i++) { for (int j = 0; j < SIZE2; j++) { if (arr1[i] == arr2[j]) { break; } else if (arr1[i] != arr2[j]) { bool exist = false; for (int k = 0; k < size3; k++) { if (arr1[i] == arr3[k]) { exist = true; break; } } if (exist == false&&j==SIZE2-1) { arr3[size3] = arr1[i]; size3++; break; } } } } //сравниваем 2 массив с первым, разницу записываем в 3 с места где закончилась запись сравнения 1 и 2 for (int i = 0; i < SIZE2; i++) { for (int j = 0; j < SIZE1; j++) { if (arr2[i] == arr1[j]) { break; } else if (arr2[i] != arr1[j]) { bool exist = false; for (int k = 0; k < size3; k++) { if (arr2[i] == arr3[size3-k]) { exist = true; break; } } if (exist == false && j == SIZE1 - 1) { arr3[size3] = arr2[i]; size3++; break; } } } } cout << "Array 3\n"; for (int i = 0; i < size3; i++) { cout << arr3[i] << "\t"; } 

Thank you all for the help!

    Well, if the task is educational, and you are still satisfied with the effectiveness of O(n*m) , then you can do so - when comparing, you immediately add to the output array:

     bool search(int value, int * begin, int * end) { for(;begin != end; ++begin) if(*begin == value) return true; return false; } int main(int argc, const char * argv[]) { srand(time(nullptr)); const int SIZE1 = 10; const int SIZE2 = 5; int arr1[SIZE1] = { 0 }; int arr2[SIZE2] = { 0 }; int arr [SIZE1+SIZE2] = { 0 }; int size = 0; for (int i = 0; i < SIZE1; i++) cout << (arr1[i] = rand() % 15) << "\t"; cout << endl; for (int i = 0; i < SIZE2; i++) cout << (arr2[i] = rand() % 15) << "\t"; cout << endl; for (int i = 0; i < SIZE1; i++) { if (search(arr1[i],arr2,arr2+SIZE2)) continue; arr[size++] = arr1[i]; } for (int i = 0; i < SIZE2; i++) { if (search(arr2[i],arr1,arr1+SIZE1)) continue; arr[size++] = arr2[i]; } for (int i = 0; i < size; i++) cout << arr[i] << "\t"; cout << endl; } 

    True, we do not remove duplicates in one array (but if they are valid in the input, why shouldn't they be in the output?) - but it’s just, you just need to add to the search cycles also by the output array.

    • Short review: 1. A bad way to create a limited set of random numbers - taking the remainder gives an uneven distribution. Then the student will copy it to where it is important and it will be embarrassing. There is uniform_int_distribution 2. The names of variables that do not speak about anything. Student and this approach scopipastic. A hypothetical colleagues then make a matoma explosion. - gbg
    • @gbg I just repeated the filling of the question by the author, since there was not a word in the question about the method of filling :) I think that just in the educational task, where a person solves the simplest problem and he has little, it turns out, using the same uniform... will only show that this code is simply written off :) Going up from time to time on Reshaem.net, I try to always take into account the level of the student, and this is justified. I would rather change the algorithmic approach itself, but again - it’s important not only to solve the problem effectively and beautifully, but to help the student understand how this is done. - Harry
    • Alas, this conniving. With this approach, you personally will certainly benefit from the educational process, but the student level will only degrade, since there will be no incentive for growth. - gbg
    • one
      @gbg Degradation will be at the approach "take the ready and do not go inside - smart uncles have decided everything for you." I said that everyone has their own level of understanding , and when I solve a problem at the level of a student’s understanding, I know that he will not just go and give to the teacher, but will understand what he gives. At your approach, the student will either fly by, failing to explain how the problem is solved, or the teacher will silently make a decision, knowing full well where it came from. I am for the blonde behind the wheel to understand how the car goes, and not just turned the steering wheel. - Harry
    • one
      For me the main thing is not only to understand the work of the program, but after a few lessons to be able to use the solution approach on another task without a hitch. I agree with @gbg, which is better explained at the student level. And in order to avoid degradation, when explaining, it is necessary to direct it to the solution, and not to give ready-made code. Unfortunately, I am not yet familiar with STL, functions and pointers. I understand that for such a task my code is very large, but for me the main thing now is to solve it. A code optimization can be done later. - Anastasiia Melnyk 8:52 pm