Let’s start with what’s not right.
Here you got it wrong and used 1
instead of i
:
if(vet[1]==valor)
{
c++;
}
Further down did a for
that is not useful:
int cc=0;
if(c==0)
{
for(i=0;i<10;i++)
{
cc++;
}
vet[cc] = valor;
}
Note that you are always counting the same amount of elements in for
, which are 10. Regardless of the values that are there and add the element in an invalid position, outside the array. Instead you want to add from the top, and increase one unit each time you insert.
Let’s correct these two points I mentioned, keeping the code as close to what I had:
#include <stdio.h>
#include <stdlib.h>
int main() {
int valor,vet[10],i, ultima_posicao = 0; //variavel para ultima posicao começa a 0
while(1) {
printf("valor:");
scanf("%d",&valor);
if(valor==99) {
break;
}
int c=0;
for(i=0; i<10; i++) {
if(vet[i]==valor) {
c++;
}
}
if(c==0) {
vet[ultima_posicao++] = valor; //apenas isto para inserir o valor
}
}
for (i=0; i < ultima_posicao; i++) { //mostra só até os que inseriu
printf("%d ", vet[i]);
}
return 0;
}
The instruction vet[ultima_posicao++] = valor;
allows you to put the value in the right position, and increases it so that in the next insertion it is also in the correct location.
Personally I think it is much more organized, if you create a function to check if the value exists in the array. And it makes it easier even at the logical level, since it doesn’t even have to count, and it can do return
straightforward:
#include <stdio.h>
#include <stdlib.h>
int existe(int vet[10], int valor) {
int i;
for(i=0; i<10; i++) {
if(vet[i]==valor) {
return 1;
}
}
return 0;
}
int main() {
int valor,vet[10],i, ultima_posicao = 0;
while(1) {
printf("valor:");
scanf("%d",&valor);
if(valor==99) {
break;
}
if(!existe(vet, valor)) {
vet[ultima_posicao++] = valor;
}
}
for (i=0; i < ultima_posicao; i++) {
printf("%d ", vet[i]);
}
return 0;
}
Check it out at Ideone
I didn’t put it here in the answer, but it is useful to also prevent the user from entering more values than the size of the array. You can do this by testing if the variable ultima_posicao
has a value of 10 or more, in which case the program ends.
There are several things there that don’t make sense. Do you want to do what exactly ? Go adding elements at the end of the array ? If it is that it is very simple, and it manages to do it only in an instruction:
vet[ultima_posicao++] = valor;
. Since you need to initialize this last position to 0 at the beginning of the program. Naturally you also have to test if you have already entered more than the available quantity, otherwise you will have indefinite behavior– Isac
Yes, but the program should not add repeated elements. I will try to use this form you taught to add
– taynan
You need to control the current insertion position in the vector and test if the read value already exists among these positions. The way you did it you try to test all the numbers in the array by entering the first number. I also think you changed the
i
for1
. Focé loops to increment the variablecc
that will always be worth10
at the end of the loop, which is an invalid index for the array.– anonimo
I understood friend, thank you very much. I will try to study more, is that I am on time to deliver a project
– taynan