Function reverses numbers in array

Asked

Viewed 2,495 times

1

How this function works in c reversing the numbers?

void inverte(int vetor[MAX], int n) {
  int i,aux;

  for (i = 0; i < n/2; i++) {
    // troca posicao i por n-1-i
    aux = vetor[i];
    vetor[i] = vetor[n-1-i];
    vetor[n-1-i] = aux;
  }
  return;
}

I can’t understand you.

  • Can you read code, understand the resources used? What you’re not getting?

  • In the part after.

  • It’s hard to try to explain without knowing what you don’t understand. Then you’d have to teach a class, which is not the point here.

  • How could you possibly understand?

  • @bob explain the part you understand, at least, then it makes it easier for the community to say what’s missing. You know what variables are, what are indexes? You know what the for ago?

  • ali primeiro no for ele divide n/2; being that it is an odd number(vector[5], n= 5 after it does those assignments[n-1-i], I don’t know how it manages to sweep all the elements of the array.

  • @bob the n you are the one who has to provide, and it has to be equal the size of the "array" for the function to act as expected. He doesn’t need to sweep all the elements, only half.

  • yes n is 5, and the vector is size 5, was used there only for better understanding.

  • the elements have already been declared. http://pastebin.com/TFfdzuTY

  • the function goes from the 1st to the 2nd item, and changes with the last and the antepenultimate. If you scan the entire array, when you get to the last one you will be "unlocking" it, then you have to stop before the middle. The medium does not need to move, because it is already in the middle. With 5 items, 2 cycles are enough. 1 -> exchange 1st with 5th, 2 -> exchange 2nd with 4th. Ready, "array" inverted. If there were 6 or 7 items, just do the same with the first 3 and the last 3 items.

  • vector[0] = vector[5-1-0]; it is the same thing as vector[0] = vector[4]. Likewise vector[1] = vector[5-1-1] is the same as vector[1] = vector[3]. When i is the first State (zero), [n-1-i] will be the last (four) when it is the second (one), [n-1-i] will be the penultimate (three). Regardless of the size of the "array", the logic is the same.

  • I think I get it. Thank you @Bacco

  • @bob posted an answer, hope it helps.

Show 8 more comments

1 answer

4


I’ll take this data as an example:

vetor = 71,72,73,74,75,76,77
n     = 7 (que é o tamanho de "vetor")

Thus the loop will cause i go from 0 and stop when you arrive at 7/2, that is 3 (how it was used i < n/2, the 3 will not be iterated).

Based on this, see each of the iterations in action, already changing the n for 7, in [n-1-i] and applying the i correspondent.

These instructions will be executed 3 times, with i availing 0, 1 and 2:

aux = vetor[i]; vetor[i] = vetor[7-1-i]; vetor[7-1-i] = aux;

We will apply the loop, with the i and the [7-1-i] of the 3 iterations in the operations:

i = 0 portanto 7-1-i = 6 -> aux = vetor[0]; vetor[0] = vetor[6]; vetor[6] = aux;
i = 1 portanto 7-1-i = 5 -> aux = vetor[1]; vetor[1] = vetor[5]; vetor[5] = aux;
i = 2 portanto 7-1-i = 4 -> aux = vetor[2]; vetor[2] = vetor[4]; vetor[4] = aux;

We will replace the right side of the allocations with their values:

i = 0 portanto 7-1-i = 6 -> aux = 71; vetor[0] = 77; vetor[6] = 71;
i = 1 portanto 7-1-i = 5 -> aux = 72; vetor[1] = 76; vetor[5] = 72;
i = 2 portanto 7-1-i = 4 -> aux = 73; vetor[2] = 75; vetor[4] = 73;

Therefore, after performing the 3 steps of the loop:

vetor[0] = 77
vetor[1] = 76
vetor[2] = 75
vetor[3] = 74 (isso nao mudou)
vetor[4] = 73
vetor[5] = 72
vetor[6] = 71
  • I get it, it’s pretty simple.

Browser other questions tagged

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