doubts in array

0

My program needs to get two vectors, A and B. This defines the set

  

A pikachu B

as the set formed by the elements that appear in A or B. It also has the set

  

A chikorita B

As the set consisting of the elements that appear in both A and B .

But the outpout is going wrong. It would have to be for example:

  

input
      3 4
      4 6 2
      3 5 7 9

     

output
      pikachu: {2,3,4,5,6,7,9}
      chikorita: {}

ps: I did not finish the code, I only did the part where the two vectors are the same, but still it is not printing correctly, what can it be?

My code:

#include <stdio.h>    
void levet(int x, long long int vetor[]){    
    int i;  
    for(i=0;i<x;i++)  
    {  
        scanf("%lld",&vetor[i]);  
    }  
}

void arrumarvet(long long int vetor[], int n){  
    int i,j,aux;  

    for(i=0;i<n;i++){
        for(j=0;j<n-1;j++){
            if(vetor[j] > vetor[j+1]){
                aux=vetor[j];
                vetor[j]=vetor[j+1];
                vetor[j+1]=aux;
            }
        }
    }
}   

int main(){   
    int i,j,n,m,cont=0;  
    long long int vetA[10000], vetB[10000];  
    long long int vetaux[10000];  
    scanf("%d %d",&n, &m);  
    levet(n,vetA);  
    levet(m,vetB);  
    arrumarvet(vetA,n);  
    arrumarvet(vetB,m);  

    for(i=0;i<n; i++){
        for(j=0;j<m;j++){
            if(vetA[j]== vetB[j]){
                cont++;
            }
            else if(vetA[j] != vetB[j]){
            vetaux[j]=vetA[j];
            }
        }
    }

    if(cont== m){
        printf("pikachu: {");
        for(i=0;i<m;i++)
        {
            printf("%d,",vetA[i]);
        }
        printf("}\n");
        printf("chikorita: {");
        for(i=0;i<m;i++)
        {
            printf("%d,",vetA[i]);
        }
        printf("}\n");
    }

    return 0;
}
    
asked by anonymous 12.11.2018 / 19:38

1 answer

0

An easy way to solve would be to first order the input vectors, after that it would be nice to delete repeated numbers, although ... if it is indeed sets, it is not possible to have repeated elements.

For union you would just put the elements of the two vectors into an auxiliary vector and eliminate the repeated elements.

For the intersection maybe something like this

void interseccao(int vetA[],int vetB[], int vetR[]){
    int i =0, j= 0, k = 0;

    for (i = 0; i<strlen(vetA); i++){
        for (j = 0; j<strlen(vetB); j++){
            if (vetA[i] == vetB[j]){
               vetR[k] = vetA[i];
               k = k + 1;
               break; 
            }

        }
    }
}

Since the vecR [] is the resulting vector, the strlen function returns the vector size

    
12.11.2018 / 22:08