T
T
tkachuk22222016-07-25 13:36:50
C++ / C#
tkachuk2222, 2016-07-25 13:36:50

How to write the same values ​​from two arrays to the third one?

task:
there are two arrays, write the same values ​​​​to the third
one, it seems to be written to me, but "garbage values" appear,
here is my code

#include <iostream>
#include <time.h>
using namespace std;

void main()
{
  const int size = 10;
  int massA[size], massB[size],massC[size];
  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] << " ";
  }
  cout << endl << "spil'ni elementy:" << endl;
  for (int j=0, i = 0, z=0; i < size,j<size,z<size; i++,j++,z++)
  {
    if (massA[i] == massB[j]) {
      massC[z] = massA[i];
      cout << massC[z] << " ";
    }
  }
  cout << endl;
  system("pause");
}

when at the beginning for massC[size] I assign {0,0,0,0,0,0,0,0,0,0} - then zeros are displayed at the end
according to the logic everything seems to be ok, but it doesn't work properly
please help
otherwise, from this hitch with garbage values, I can’t continue to do tasks

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Movchan, 2016-07-25
@tkachuk2222

Take a closer look at this code:

for (int j=0, i = 0, z=0; i < size,j<size,z<size; i++,j++,z++)
{
    if (massA[i] == massB[j]) {
        massC[z] = massA[i];
        cout << massC[z] << " ";
    }
}

I think you don't understand how for works . You assign the same value to three variables and increment each one by one at the same time. It's the same as if you wrote the following:
for (int i = 0; i < size; ++i)
{
    if (massA[i] == massB[i]) {
        massC[i] = massA[i];
        cout << massC[i] << " ";
    }
}

Agree, it doesn't make sense. To do what you have in mind, you need to place one loop inside another:
int k = 0;
for (int j = 0; i < size; ++i)
{
    for (int j = 0; j < size; ++i)
    {
        if (A[i] == B[j])
        {
            // Здесь можно сделать так:
            C[k] = A[i];
            ++k;

            // или одной строчкой
            // C[k++] = A[i]
        }
    }
}

But note that such an algorithm will have one drawback: if one array has two identical elements, and the other has two identical ones, there will be four of them in the new array. More generally, if one array has a 's of the same elements and another has b 's, you end up with a*b of them, which can also cause your array to overflow.
In general it is strange that your example was compiled. You must be using an outdated compiler. In C++, the main function must return an int . Using void results in an error:
Also note that if you use C header files in C++, they are usually named with the c prefix and without the .h
extension . In general, C++ has random .
Also, never use the systemsystem("pause"); function , which pauses your program and starts the one given in the argument. People who don't use Windows won't be able to run your code because they don't have the pause program .

S
saltydogd, 2016-07-25
@saltydogd

1. Are the same values ​​equal in value and position? in the specified implementation it seems that it is, but we know that...
2. massC[z] == massA[i]; this is a comparison operation with an unused result - assignment 3 is more appropriate
. To compare and find the same elements, a nested loop is required, most likely, friendly

S
SiZen72, 2016-07-25
@SiZen72

1. In your version, only values ​​that are in the same positions are compared and written to the same position in the 3rd array. This is to the question whether it is necessary to look for identical only in value or identical in value and position.
2. Since the array is essentially a pointer to a number of memory cells, without its initialization, when accessing any element, the value stored in this cell will be returned. And in the absence of initialization, there are no guarantees that there will be no garbage data in this cell, which happens almost 100%.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question