Misconception: Students have difficulty working with temporary variables in arrays.

Like(116 Likes)
  • For instance, the code below reverses the content of an array.
    • // this code reverses an array
      for (int i = 0; i < N/2; i++) {
          int temp = a[i];
          a[i] = a[N - i - 1];
          a[N - i - 1] = temp;
      }
  • Students sometimes think that the code creates a mirror array due to misconceptions about temporary variables in arrays. Below is some similar looking code that actually mirrors an array.

    • // this code creates a "mirror" array
      for (int i = 0; i < N/2; i++) {
          a[i] = a[N - i - 1];
          a[N - i - 1] = a[i];
      }
External Source

Interview with Dan Leyzberg.

Other Tips By