I have another problem. My activity this time is to create and read the elements of two vectors, A and B, with 5 and 7 values, respectively. Then the program will show you what elements are repeated. Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int a[5], b[7], i, j;
for (i=0; i<5; i++) {
printf("Digite o %d elemento do vetor A: ",i+1);
scanf("%d",&a[i]);
}
for (j=0; j<7; j++) {
printf("Digite o %d elemento do vetor B: ",j+1);
scanf("%d",&b[j]);
}
printf("\n");
for (i=0; i<5; i++) {
for (j=0; j<7; j++) {
if (a[i]==b[j]) {
printf("O numero %d esta nos dois vetores\n",a[i]);
break;
}
}
}
}
My problem is, if in one of the vectors the value is repeated, the program prints the information twice. It's not a bug, since I did not put a condition, but I'd like it to show only once.
How it happens:
a [0] = 5 In the first vector I put equal terms in the first 2 elements.
a [1] = 5
.
.
.
b [0] = 5 In the second, I repeated the term.
.
.
.
When printing repeats, it prints the response twice (since it checks for a[0]b[0]
and then a[1]b[0]
):
The number 5 is in both vectors. Is there a way to show that you are on both vectors just once?