How to compare two vectors and exclude equal values from the vector with the highest number of characters? In "C"

Asked

Viewed 323 times

0

Generate and display sets A - B and B - A

A and B are both vectors with user-defined sizes

A -- allele values

B -- user defined


for example: vector A= (10),(5),(7),(8),(12)

vector B= (10),(8)

A-B

New Vector A= (5),(7),(12)

Code I thought ***

int pertinencia(int vetor[], int tam, int ele) {
  int i, pq = 0;
  for (i = 0; i < tam; i++) {
    if (vetor[i] == ele) {
      return 1; //Este elemento pertence a o vetor
    } else {
      pq++;
    }
  }
  if (pq == tam) {
    return 0; //Este elemento nao pertence a o vetor
  }

}

int main() {
  int a[] = {
    10,
    5,
    7,
    8,
    12
  };
  int b[] = {
    10,
    8
  }, c[12];
  int i;
  for (i = 0; i < 2; i++) {
    if (!pertinencia(a, 5, b[i])) {
      printf("%d ", a[i]);
    }
  }

}

1 answer

0


#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main(int argc, char** argv)
{

   int vetor_1[] = {10, 5, 7, 8, 12}, vetor_2[] = {10, 8}, indice = 0, resultado, i;

   while(indice != 2)  // indice  for diferente do tamanho do segundo vetor vai fazer
   {
     resultado = vetor_2[indice];
     for(i = 0; i < 5; i++)
     {
        if(vetor_1[i] == resultado)
        {
            vetor_1[i] = -1;       // se encontrar valor igual vai substituir por -1
            break;
        }
     }
    indice++;
   }

  for(i = 0; i < 5; i++)
  {
    if(vetor_1[i] >= 0)
    {
        printf("%d\n", vetor_1[i]);  // mostra os valores que são maiores que -1
    }
  }

   return 0;
}
  • Thanks!!! Very good the code, only had to adapt it pq the values for vector A, are random values between -50 and 50. great code!!

  • For nothing, any doubts post again

Browser other questions tagged

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