you need to add the same values ​​from the first and second arrays to the array, it seems that the values ​​are entered, but when the third array is output, the "garbage values" are output

#include <iostream> #include <time.h> using namespace std; void main() { const int size = 10, size1 = 20; int massA[size], massB[size], massC[size1]; srand(time(NULL)); for (int i = 0; i < size; i++) { massA[i] = rand() % 10; cout << massA[i] << " "; } cout << endl; for (int i = 0; i < size; i++) { massB[i] = rand() % 10; cout << massB[i] << " "; } for (int z=0, i = 0; z<size, i < size; i++,z++) { for (int j = 0; j < size; j++) { if (massA[i] != massB[j]) { massC[z] = massA[i]; cout << massA[i] << " "; } } } for (int i = 0; i < size1; i++) { cout << massC[i] << " "; } system("pause"); } 
  • there instead of size1 - size - tkachuk
  • if I write it like this: <code> if (massA [i]! = massB [j]) {massC [z] = massA [i]; cout << massC [z] << ""; } </ code> nothing changes - tkachuk
  • one
    By the way, if -858993460 is represented as hex, then you get 0xCCCCCCCC , which usually indicates that the memory is not initialized. In different systems in different ways. - αλεχολυτ
  • Just wondering - why for (int z=0, i = 0; z<size, i < size; i++,z++) - complete and absolute duplication of i and z ? - Harry
  • when at the beginning for massC [size] I assign {0,0,0,0,0,0,0,0,0,0,0} - then at the end zeros are displayed according to the logic, everything seems to be okay, but it does not work properly help pliz but I cannot continue to do the tasks from this hitch with junk values - tkachuk

1 answer 1

your massC array is populated with values ​​in

 if (massA[i] != massB[j]) { massC[z] = massA[i]; cout << massA[i] << " "; } 

therefore, not always, therefore, garbage values ​​will remain in it, since you declared, but did not initialize arrays. in the other arrays, you rewrite the values ​​with your own data.

question-answer about array initialization with zeros