October 21, 2024
Chicago 12, Melborne City, USA
C#

Why do array element values not change?


int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

for (int i = 0; i <= 9; i++) {
    int tmp = a[i];
    a[i] = a[9 - i];
    a[9 - i] = tmp;
}

for (int i = 0; i <= 9; i++) {
    printf("%d", a[i]);
}

The result of it is: 1234567890,
but why not 10987654321
since it switch values in the first for loop,
but why can not change it when the next for loop runs

I have tried to move "int tmp" outside of the loop, but of no use at all.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video