Please help me with the task. From array A, delete odd elements that are not equal to the odd elements of array B.
Example: Array A = 3, 4, 5; array B = 1, 5, 6 new array A = 4 5.
My thoughts are:

void main() { int i, n, x, a[100], b[100]; for (i = 0; i < n; i++) //n-количество элементов { cin >> a[i]; } for (i = 0; i < n; i++) { cin >> b[i]; } x = n; i = 0; for (i = 0; i < n; i++) { if (a[i] % 2 != 0 != b[i] % 2 !=0) { //Дальше надо начать удалять нечетные элементы неравные В } } getch(); } 
  • Uh ... And where do you get the value of the variable n ? - VladD

3 answers 3

Threw you a solution in a hurry. Naturally, there are no checks for overflow of the array, etc., this is already add. If anything is not clear, ask! =)

 #define _CRT_SECURE_NO_WARNINGS #include "stdafx.h" #include "stdio.h" #include "conio.h" #include <iostream> using namespace::std; int poisk(int B[], int temp, int n2) { int k=0; for (int i=0; i<n2; i++) { if (B[i]!=temp) k++; } if (k==n2) return 1; else return 0; 

}

  void del(int A[], int &n1, int i) { for (i; i<n1; i++) { A[i]=A[i+1]; } A[n1-1]=0; n1--; } int _tmain(int argc, _TCHAR* argv[]) { setlocale(LC_ALL, "Russian"); int A[10]; int B[10]; int n1=0,n2=0; int temp; cout<<"Введите количество элементов первого массива: "; cin>>n1; cout<<endl<<"Введите количество элементов второго массива: "; cin>>n2; cout<<"Вводим значения первого массива:"<<endl; for (int i=0; i<n1; i++) { cin>>A[i]; } cout<<"Вводим значения второго массива:"<<endl; for (int i=0; i<n2; i++) { cin>>B[i]; } cout<<endl; for (int i=0; i<n1; i++) { if (A[i]%2!=0) { temp=A[i]; if (poisk(B, temp, n2)) { del(A, n1, i); i--; } } } for (int i=0; i<n1; i++) { cout<<A[i]<<endl; } _getch(); return 0; } 
  • After del (A, n1, i), the following odd elements will seem to be even? (it is treated by viewing from the tail) - alexlz
  • I tried to run your code, but it does not display the correct answers ( - slim
  • Replace the Poisk function with: int poisk (int B [], int temp, int n2) {int k = 0; for (int i = 0; i <n2; i ++) {if (B [i]! = temp) k ++; } if (k! = n2-1) return 1; else return 0; } Tests: First array: 1 3 4 6 8 Second: 1 2 4 6 8 Result: 1 4 6 8 - xeeqqw
  • Tests: First array: 1 3 4 6 8 Second: 1 2 4 6 8 Result: 1 4 6 8 - xeeqqw
  • 3
    @xeeqqw, I'm afraid to upset you, but the poisk() function is not only extremely inefficient, but simply wrong . If the number is found in the B[] array several times, then it will return 1, not 0. When testing, be careful, test on more diverse data sets. - avp

You can do something like this: In a loop, run through the array A. If the array element is even, then write it into the resulting array. If it is odd, then in a nested loop, run through array B and compare the current value from A with values ​​of B. If B has a similar element, then write it into the resulting array and interrupt the nested loop

Upon completion of all these actions, array A becomes equal to the resulting array.

  • @DreamChild: (excuse me for cluttering up your question, there is a limit of comments) First, helping with trivial problems such as careful handling of indexes is a deliberately impasse path. A person must draw cubes and arrows himself in order to understand how it should be. If you give him a ready-made solution, it will not be better, but worse. Secondly, a “high-level” explanation can push a person to study the right means adequate to the language, which is already good. Thirdly, such an answer can help other site participants to learn how best to solve similar problems in C ++. - VladD

Use standard containers.

 std::unordered_set exceptset(except.begin(), except.end()); std::copy_if(src.begin(), src.end(), std::ostream_iterator<int>(std::cout, " "), [&exceptset](const int i) { return !(i % 2 != 0 && exceptset.find(i) == exceptset.end()); }); 

Input remains as an exercise.

  • @VladD, decided to intimidate a novice and turn away from programming? In any case, if he does not learn how to manipulate bitics in memory now, then in a code like yours you will always make different mistakes. - avp
  • one
    Also correct. By the way, what bothers me in such tasks is the requirement to remove the array element . What does it mean, guess what you want ... - avp
  • one
    @VladD, you forgot exceptset to add a closure. In addition, passing an int by reference is more expensive than by value. - dzhioev
  • one
    @VladD is, of course, great, but you do not forget that this task is most likely given by the teacher, and this very teacher most likely will not accept such a decision, saying that "we did not pass it." And in general, it will be right, since the task is most likely intended to consolidate the elementary knowledge of cycles and arrays - DreamChild
  • one
    @VladD, the solution is, of course, beautiful. But, isn’t C ++ pushing it onto a path already covered by languages ​​like Prolog and NPS? It seems to me that even if beautiful, but incomprehensible programs simply frighten the "masses" away. It turns out something elitist ... and just stops developing. Yes, the inertia accumulated by the crosses is very large, and stagnation and oblivion are no longer threatened. - Returning to the task. If we assume that deleting in the array is a “right” shift to the left, is it possible to express such an algorithm as elegantly as in your answer, and in one pass? - avp