Repeat loop is not running

Asked

Viewed 250 times

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++;
            }
  • 1

    Remember that arrays in C start in the Dice 0, and go to the Indian N - 1.

2 answers

2


You are not restarting the variable num which is used as an accountant. It has two solutions:

while (num < n1) {
    printf ("%d ", vet[num]);
    num++;
}
num = 0;
while (num < n1) {
    if (vet[num] == 0) {
        vet[num] = x1;
    }
   num++;
}

I could also do the same with for, then avoid this confusion:

for (int num = 0; num < n1; num++) {
    printf ("%d ", vet[num]);
}
for (int num = 0; num < n1; num++) {
    if (vet[num] == 0) {
        vet[num] = x1;
    }
}

Note that I’m starting the num with 0, seems to me to be the most correct but I can’t guarantee without seeing the rest. If the right is 1 even, just change.

But to avoid this problem can also take advantage and do everything in one loop:

for (int num = 0; num < n1; num++) {
    printf ("%d ", vet[num]);
    if (vet[num] == 0) {
        vet[num] = x1;
    }
}

I put in the Github for future reference.

  • Thanks, Bigown, it worked, and the code got thinner. For me, I will have to change my logic and reorganize the code, because, as I did (comparing and replacing what is 0) he is putting the value typed in all 0’s and not only in the first one of them. One more thing, this is with all the elements in one line is C or C++? Because I have to implode in C.

  • 1

    Both. It’s C, so it’s C++ too. It’s always preferable when it can. I actually know programmer who only uses for and never uses while. I think it’s an exaggeration but it’s possible. In your case the for is quite intuitive because you clearly have a variable initialization, a condition to indicate the end and a processing that must be performed at each step. It deals better with scope. http://www.ime.usp.br/~elo/Introducaocomputacao/Comando%20de%20repeticao%20for.htm Do you know English? http://www.tutorialspoint.com/cprogramming/c_for_loop.htm http://www.learn-c.org/en/For_loops Remember the [tour].

0

        while (num <= n1)
        {
            printf ("%d ", vet[num]);
            num++;
        }

At the end of this first cycle, the value of num will be n1 + 1

        while (num <= n1) //roda o laço e caso encontre uma posição do vetor igual a zero, subtitui

this cycle will never perform because the first time the condition is false (num == n1 + 1).

  • Pmg, but that first loop you referred to is just the loop to print the current list on the screen. It’s running right. The problem is in the next loop, the one you placed the first line. Thank you

  • 1

    At the end of the first cycle, the value of the variable num is n1 + 1. The condition of the second cycle is false, so the second cycle does not run once.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.