Ben
Ben

Reputation: 63

Changing Index Value in Array

I'm enrolled in AP Computer Science Principles. Our class curriculum has been in javascript, but our teacher wants us to get used to pseudocode since the AP exam is in that style. I've been looking over the problems in the textbook, and there is one that I don't quite understand.

enter image description here

My answer is close to C, but the last item in the array is different. In the last repeat, list[6] is assigned to the temp value, which is 44. Why, then, is the final array value 33 in C? Did I do the problem incorrectly? Can someone guide me through the procedures and explain my mistake??

Upvotes: 2

Views: 276

Answers (2)

CertainPerformance
CertainPerformance

Reputation: 370639

Assuming that the arrays in the odd code in the question are one-indexed rather than zero-indexed, the answer is C. Translating the code to Javascript:

const list = [33, 11, 66, 22, 44, 55];
const n = list.length - 1;
let k = 0;
for (let i = 0; i < n; i++) {
  const temp = list[k];
  list[k] = list[k + 1];
  list[k + 1] = temp;
  k = k + 1;
}
console.log(list);

Or, to translate that to English: for each index i in the array, starting at index 0, switch the positions of the ith element and the i + 1th element. So the first element gets switched with the second element, then the second element (the element that was just switched) gets switched with the third, and so on. In the end, you end up with the first element having been moved from the beginning of the array to the end. This will be clearer if you log the array during every iteration:

const list = [33, 11, 66, 22, 44, 55];
const n = list.length - 1;
let k = 0;
for (let i = 0; i < n; i++) {
  const temp = list[k];
  list[k] = list[k + 1];
  list[k + 1] = temp;
  k = k + 1;
  console.log(list);
}

The problem with the text on the right of your picture is that it's taking each list# as it was in the original array, not considering that things may have changed in the meantime, due to swaps.

Note that, in Javascript, which the code in the question is apparrently suppost to represent somehow, you can use destructuring to switch the positions of those elements at once, without an intermediate temp variable, if you wanted:

const list = [33, 11, 66, 22, 44, 55];
const n = list.length - 1;
let k = 0;
for (let i = 0; i < n; i++) {
  [list[k], list[k + 1]] = [list[k + 1], list[k]];
  k = k + 1;
  console.log(list);
}

Upvotes: 0

Burgan
Burgan

Reputation: 900

In your tracing on the right of the image, in step two, you have temp = 11. However, this is not the case. Temp should be 33, as you have just set list[2] = 33. The same mistake is carried through out the other steps.

Think of it like this. In each step, you set list[k+1] = 33. Then you increment like so, k = k + 1. So when you set temp = list[k] it will always be 33.

1.
temp = 33
list1 = 11
list2 = 33

2.
temp = 33
list2 = 66
list3 = 33

3.
temp = 33
list3 = 22
list4 = 33

4.
temp = 33
list4 = 44
list5 = 33

5.
temp = 33
list5 = 55
list6 = 33

Upvotes: 2

Related Questions