0
Follows the statement:
Read two integer vectors X and Y, cadao one with 5 elements (assume the user does not report repeated elements). Calculate and show the resulting vectors in each case below:
Sum between X and Y: sum of each element of X with the element of the same position in Y;
Product between X and Y: multiplication of each element of X with the element of the same position in Y;
Difference between X and Y: all elements of X that do not exist in Y;
Intersection between X and Y: only elements appearing in the two vectors;
Union between X and Y: all elements of X, and all elements of Y which are not in X.
code:
#include <stdio.h>
#include <locale.h>
#define TAM 5
int main(){
setlocale(LC_ALL,"Portuguese");
int x[TAM], y[TAM], dif[TAM], c, c2, c3, n=0, uniao[TAM*2], aux=0;
printf("Lendo o vetor X: \n");
for (c=0; c<TAM; c++){
scanf("%d", &x[c]);
}
printf("\n\nLendo o vetor Y: \n");
for (c=0; c<TAM; c++){
scanf("%d", &y[c]);
}
system("CLS");
printf("Soma entre X e Y:\n");
for (c=0; c<TAM; c++){
printf("%d\n", x[c] + y[c]);
}
printf("\n\nProduto entre X e Y:\n");
for (c=0; c<TAM; c++){
printf("%d\n", x[c] * y[c]);
}
printf("\n\nDiferenca ente X e Y:\n");
for(c=0; c<TAM; c++){
for(c2=0; c2<TAM; c2++){
if(x[c] == y[c2])
break;
}
if(c2==TAM){
for(c3=0; c3<n; c3++){
if(x[c] == dif[c])
break;
}
if(c3 == n)
dif[n++] = x[c];
}
}
for (c=0; c<n; c++){
printf("%d\n", dif[c]);
}
printf("\n\nIntersecção entre X e Y:\n");
for (c=0; c<TAM; c++){
for (c2=0; c2<TAM; c2++){
if (x[c] == y[c2]){
printf("%d\n", x[c]);
}
}
}
printf("\n\nUnião entre X e Y:\n");
}
I am not managing to make the union without repetition of elements.. Can anyone help me? Thank you
Its algorithm is in quadratic order. The strategy needs to be done in a totally different way to be executed in linear order
– Jefferson Quesado