1
I am making a code in which there is a vector (with 15 positions) with predetermined values (10 values any and 5 times the value 0), and where the user will inform a new value to go to the end of the list (replacing 0).
It seems to me that everything is right here, but the vector does not "catch" the entered value. It runs everything straight, no mistake, just does not replace 0 with the new value.
Follows the code (as part of a larger code, a switch
with 7 options, I will put only this part with error to not get too big and with unnecessary parts):
case 1 : printf("Qual valor voce quer inserir no final? ");
scanf ("%d",&x1);
n1 = 15;//posições do vetor
num = 1;
x2 = 1;
printf("lista atual:\n"); //Exibe como está a lista
while (num <= n1)
{
printf ("%d ", vet[num]);
num++;
}
while (num <= n1) //roda o laço e caso encontre uma posição do vetor igual a zero, subtitui
{
if (vet[num] == 0)
{
vet[num] = x1;
}
num++;
}
printf ("\n\nNova lista:\n");
while (num <= n1)
{
printf ("%d", vet[num]);
num++;
}
Remember that arrays in C start in the Dice
0
, and go to the IndianN - 1
.– pmg