Inversion of array (positions)

Asked

Viewed 159 times

2

I’m trying to do an array inversion in the language C.

For example: I have an array [1, 2, 3], I need to invert the numbers in the SAME array, giving an output like this: [3,2,1]. I’m trying, but when I get to the second position of the array, it starts to mirror. Follow the code and thank you from now:

int main(int argc, char** argv) {
    int x[4];
    int i, j, aux;

    for (i = 0; i <= 3; i++){
        printf("\nEscreva um numero\n");
        scanf("\n%d", & x[i]);  
    }

    i = 0;
    for (j = 3; j >= 0; j = j - 1){
        aux = x[i];
        x[i] = x[j];
        x[j] = aux;

        i++;
    }

    for (i = 0; i <= 3; i++){
        printf("\n numero: %d\n", x[i]);    
    }       
}
  • Is mirrored pq you are not using the variable aux to store the value you are moving....

  • Guys, I redid the "program", but it’s still mirroring... for example, I put (1, 2, 3, 4) the correct output would be (4, 3, 2, 1). But the output is being (4, 3, 3, 4).

  • I edited with the new code (which still continues to mirror);

  • Try to replace the line x[j] = x[i]; for x[j] = aux;

  • right, you’re using the aux to receive the value but is not resetting the value of aux in the vector

  • Guys, sorry for my ignorance, I’ve redone as requested, but the output is repeating the input array. For example: Coloco (1, 2, 3, 4)... the output is being (1, 2, 3, 4). Will the output loop I made is wrong?

Show 1 more comment

2 answers

4

The answer to the question comes down to one line:

You cannot pass half the vector in your loop, but you are reversing and "deflecting" again as you pass through the middle.


Just as an add-on, follow code:

Forget the z and do it right on x

for (j = tamanho-1; j > (tamanho/2); j = j - 1){
    aux = x[i];
    x[i] = x[j];
    x[i] = aux;        
    i++;
}

See working on IDEONE.

If you want to optimize a little (and make the size more flexible), see another example:

int main(int argc, char** argv) {
    int x[7];
    int i, aux;
    int tamanho = sizeof(x)/sizeof(int);

    for (i = 0; i < tamanho; i++){
        printf("\nEscreva um numero\n");
        scanf("\n%d", & x[i]);  
    }

    for (i = 0; i <= (tamanho/2); i++){
        aux = x[i];
        x[i] = x[tamanho-i-1];
        x[tamanho-i-1] = aux;
    }

    for (i = 0; i < tamanho; i++){
        printf("numero: %d\n", x[i]);    
    }
}

Also working on IDEONE.

Note that the answer of Daniel Grillo (who already received my +1) also has an elegant solution, which is to check the comparing loop j with i, and could be optimized for for (j = 3; j > i; j--){

  • Bacco, thank you for answering my question... but on the way out, you’re still repeating the input values. :(

  • I put a functional demo on IDEONE

  • and one more with improvements

  • @Matheusminguini tried to highlight the important part, which is not to go beyond the middle of the vector, so I hope you can understand why the demos in IDEONE work, and compare with your test. Understand the j < (tamanho/2) no for (which in your case can be summarized to j < 2

  • Myth thank you for your time, Bacco! I understand! I wish you much success

  • Glad it worked, qq thing leave comment here. If you can, mark any of the answers as accepted to close the issue as resolved. Just click on the green V on the side of the score. The acceptance can only be in one of them, but remember that soon you will be able to vote positive in all the posts that help you, even in questions from third parties (and can positively more than one answer in this case, only the acceptance that is in one).

Show 1 more comment

2

Try it like this:

i = 0;
for (j = 3; j >= i; j--){
    aux = x[j]; 
    x[j] = x[i];
    x[i] = aux;
    i++;    
}

Browser other questions tagged

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