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....
– JcSaint
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).
– Matheus Minguini
I edited with the new code (which still continues to mirror);
– Matheus Minguini
Try to replace the line
x[j] = x[i];
forx[j] = aux;
– rdleal
right, you’re using the aux to receive the value but is not resetting the value of aux in the vector
– JcSaint
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?
– Matheus Minguini