Below is the question you did in class on Tuesday, along with a full answer.
Things you need to take away from this are:
- You would obviously not be expected to colour-code your answer. I have only done this to make it easier for you to see what is going on.
- The first question asks you to show the array contents after each iteration of the loop. This is all you really need to do for this question. You don’t need to show N, K or K < N – 1. However, showing those variables might help you avoid mistakes.
- Note that I have generally provided more than the minimum amount of information for each question. This is a “belt and braces” approach. It is good exam technique.
- Read the question! Some of you didn’t show the contents of the array!
- Note the standard technique for reversing an array in-place.
- Be careful with the number of loop iterations.
- For part c, It is not good enough that the loop condition should be K < N – 2, because that will only work for an array of this length! You must generalise. In general then, you need to go halfway into the array to make sure you have reversed it, and so the condition should be K <= N/2.