doubt in array

Asked

Viewed 70 times

-1

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

To Pikachu B

as the set formed by the elements that appear in A or B. In addition, it has the set

To Chikorite B

as the set formed by the elements that appear both in To how much in B.

But the outpout is coming out 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}
Chikorite: {}

ps: I have not finished the code, I have only done the part where the two vectors are equal, but even so it is not printing correctly, which may 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;
}
  • If I understood correctly you are trying to implement union and inter-interest in sets is this ? In what the Pikachu would be union and the intersection Chikorita ?

  • That, Pikachu would be it plus Chikorita. And Chikorita would be only if it appeared in itself and in Pikachu

1 answer

0

an easy way to solve would first sort the input vectors, after that it would be good to eliminate repeated numbers, although... if it is in fact sets, it is not possible to have repeated elements.

For the union it would be enough to place the elements of the two vectors in an auxiliary vector and eliminate the repeated elements.

To 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 vetR[] is the resulting vector, the strlen function returns the size of the vector

Browser other questions tagged

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