2
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.

2
  • Draw out the array at each step on paper. Commented Jan 7, 2022 at 7:30
  • 4
    This is the perfect time to learn how to use a debugger to step through your code statement by statement while monitoring variables and their values. Pay close attentoion to the array indexes you use (i and 9 - i). Commented Jan 7, 2022 at 7:35

2 Answers 2

7

Your loop:

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

Each iteration it swaps elements and does it perfectly.

On the first iteration, it swaps a[0] and a[9]. On the second a[1] and a[8] and so on.

But what about the 10th iteration?

Well, it swaps the values in a[9] and a[0]. But you already swapped those values, so it's just swapping them back the way they originally were.

1 2 3 4 5 6 7 8 9 10
10 2 3 4 5 6 7 8 9 1
10 9 3 4 5 6 7 8 2 1
10 9 8 4 5 6 7 3 2 1
10 9 8 7 5 6 4 3 2 1 
10 9 8 7 6 5 4 3 2 1 <-- This is what we want.
10 9 8 7 5 6 4 3 2 1 <-- Oops.
10 9 8 4 5 6 7 3 2 1 <-- Just getting worse.
10 9 3 4 5 6 7 8 2 1
10 2 3 4 5 6 7 8 9 1
1 2 3 4 5 6 7 8 9 10

If you stop after 5 (half the length of the array) iterations, you're only swapping each pair once and your code will behave the way you expect.


To illustrate the suggestion from @user4581301 in comments, see below. We get begin and end pointers to the first and last elements in the array, respectively. Our loop continues as long as the begin pointer is less than the end pointer. If end is greater than begin it means we've gone past halfway. If they're equal, it means our array had an odd number of elements, and we've hit the middle one. That middle element wouldn't need to be swapped, so either way we're done.

The update in the loop increments begin and decrements end, moving inward one place from each end of the array.

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    int *begin, *end;

    for (
      begin = &arr[0], end = &arr[9]; 
      begin < end; 
      ++begin, --end
    ) {
        int temp = *begin;
        *begin = *end;
        *end = temp;
    }

    for (size_t i = 0; i < 10; ++i) {
        printf("%d\n", arr[i]);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

When writing something like this I'll use a begin pointer and an end pointer (or begin and end indices) and at the end of each iteration of the loop I advance begin and decrease the end. When end <= begin, you've hit the middle and stop.
0

Because you are swapping every pair twice.

Iterate it to half of the array. If you are not able to spot the error kindly dry run it on paper, you will get the answer.

#include <stdio.h>

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

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

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

2 Comments

Correct, but a good answer generally has some meat, explanation and possibly a solution, on its bones.
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.