4
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 which elements are repeated. This is 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 is not a mistake, since I did not put a condition, but I would like you to show only once.
As 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 the repeaters, he prints the answer twice (since he checks by a[0]b[0]
and then by a[1]b[0]
):
The number 5 is in the two vectors. There is some way to show that it is in the two vectors only once?
I tried with
int a[5] = { 110, 2, 99, 5, 106 }, b[7] = { 2, 7, 8, 9, 110, 99 }
. The result was that he found the numbers 110 and 2 in the vectors, but he couldn’t find 99. http://ideone.com/stueRn The reason is that the character 'c' has the code 99, but he has found the 110 before.– Victor Stafusa
@Victor Now that I can change, so I’ve created a specific case for 99 as well.
– axell-brendow