Intersection of vectors in C

Asked

Viewed 1,308 times

1

In the code below the intention is to intersect two vectors, but when it does not check if the value is 0 in that passage

if(vet1[i] == vet2[j]) 
{
    n = vet1[i];
}else
{
    C[i] = '\0'; 
}

I check if the values are equal for the intersection, but when I get to the printf

for(int i=0; i < 5;i++)
{
    if(C[i] != '\0')
    printf("os dois vetores tem em comum %d.\n", C[i]);
}

How do I verify that there is an intersection of the number 0 ? I used the \0 so that when there is no intersection it adheres to a null value.

Entire code below:

#include <iostream>
int main() 
{
    int n;
    int vet1[5]= {1,8,9,4,5}, vet2[8] = {8,3,6,5,4,3,2,1}, C[5];
    
    for(int i=0; i < 5;i++)
    {
        n=0;
        for(int j=0;j < 8;j++)
        {
            // verifica se na posiçao i o vetor é 
            if(vet1[i] == vet2[j])
            {
                n = vet1[i];
            }else
            {
                C[i] = '\0'; 
            }
        }
        C[i] = n;

    }
    for(int i=0; i < 5;i++)
    {
        if(C[i] != '\0')
        printf("os dois vetores tem em comum %d.\n", C[i]);
    }
    for
    
    return 0;
}

1 answer

1


The problem in the solution you have refers to logic and the "null" value. There is no null value for integer, and \0 is incorrect also because it is a char and only has application in strings. It turns out that the compiler will allow you to do this but will put a normal zero. So it would be the same as doing C[i] = 0. This is also not correct because it can intersect with the value 0, and then cannot distinguish whether it is valid or "null".

The solution is to control the size of the intersection vector and increase each time you realize that an element exists in the two vectors. This way you can be sure that you only have valid values in the vector

Solution:

#include <iostream>
int main()
{
    int vet1[5]= {1,8,9,4,5}, vet2[8] = {8,3,6,5,4,3,2,1}, C[5];
    int tamC = 0; //novo tamanho para o vetor C

    for(int i=0; i < 5;i++)
    {
        for(int j=0;j < 8;j++)
        {
            if(vet1[i] == vet2[j])
            {
                C[tamC] = vet1[i]; //coloca o elemento no vetor de interseccao
                tamC++; //aumenta a quantidade no vetor de interseccao
                break; //termina a procura
            }
            //sem else
        }
    }

    for(int i=0; i < tamC;i++) //percorre até ao tamC
    {
        std::cout << "os dois vetores tem em comum " << C[i] << std::endl;
    }

    return 0;
}

I commented on all the adjustments I made in the code.

The logic within the if recording the value in the intersection vector:

if(vet1[i] == vet2[j])
{
    C[tamC++] = vet1[i]; // <--
    break;
}

But I chose to leave in the most extensive form to be clearer, and not to generate any confusion even to beginners.

Watch it work on Ideone

Browser other questions tagged

You are not signed in. Login or sign up in order to post.