0
How do I load an array, let the user enter all values, which cannot have repeated numbers?
For example, a vector of 20 positions that asks the user to enter each value, and, when giving a value equal to any previous one, a message would appear and it would be necessary to inform another value.
My code so far:
main()
{
printf("Insira os dados do vetor A\n");
for(i = 0; 20 > i; i++)
{
scanf("%d",va[i]);
auxva = va[i];
for(j = 0; 20 > j; j++)
{
if (va[j] == auxva)
{
printf("Sem valores repetidos\n");
scanf("%d",&auxva);
}
if(va[j] != auxva)
continue;
}
}
}
At each given value, you will need to traverse the entire current vector by checking whether the value matches at least one value present in the vector. If yes, re-request the number, if not, insert it. Do you have any attempt that did and went wrong?
– Woss
main(){ printf("Enter vector data A n"); for(i=0;20>i;i++){ scanf("%d",va[i]); auxva = va[i]; for(j=0;20>j;j++){ if (va[j]==auxva){ printf("No repeat values n"); scanf("%d",&auxva); } if(va[j]!=auxva) continue; } } } I had done something like this, I’m still a little lost, but I think the idea is more or less right, thanks!
– Beto Oliveira
Beto, I added your code directly to the question. Since it’s new here, I recommend you do the [tour] to understand the basics of how the site works. There you will find everything you need to use the site well, such as tips on how to ask, how to format questions and answers, etc.
– Woss
Three comments: 1. You have not declared any variable in your example, so it has no way to compile. 2. that
if (va[j] != auxva) continue;
is redundant: if you take it out completely does not change the behavior of the code. 3. It was your teacher that she taught to write20 > i
and20 > j
in the bondsfor
?– Wtrmute