15 and 16 (1 Viewer)

triviajeon

Member
Joined
Aug 5, 2019
Messages
84
Gender
Undisclosed
HSC
2021
Would someone mind explaining why the answers are C and B?

I got B and A instead???

Q15 and Q16.pngMCQ.png
 

lincolny

Member
Joined
Jul 8, 2021
Messages
32
Gender
Male
HSC
2021
Hey triviajeon,

Let's read the program together.

Initially, the program starts off setting the default values of W, Y to the first element in the array, and sets the X, Z to 1 (which is the index of the array). Notice that the variable i was declared and set to 2, which we can assume that this must be quite an optimised sort/search algorithm (because there's no point of comparing element at index 1 to itself as that's a waste of time, so we start off from the next element. This approach will however introduce the problem of index overflow (runtime error) if the list is of length 1 and there is no element at index 2, but let's just say the question is legit).

Then, the program loops with incrementing i at the end of each loop. Let's see what the loop is doing to our variables.

Lines 8 to 11 compares the List(i) to W, and if the former is greater, it will set the variable W to the value of List(i) and X to the current index, i. As W and X are not referenced again in the loop, we can safely say that, after looping through the entire array, lines 8 to 11 will set the biggest value List(nMax) to variable W and its index in the array to X.

Similarly, Lines 12-15 sets the smallest value List(nMin) to variable Y and its index to Z.

After the loop, the program swaps the value at X and Z, i.e. swaps the min/max value in the list. So, since the max value of the list is 6 (index=3) and min value is 1 (index=2), the value of X is 3 and Y is 2 giving option C.

For Question 16, the correct answer is B. What we are trying to do is to Swap the elements at index (a, b), not the index itself. Looking at A), it is swapping the values of the a and b, which are the indices (integers), not the element at List(a) and List(b). For option C, it will not work as it has the same issue as A and also it will set both a and b to the value of b. Option D does not work as it will set both the elements at indices a and b to the value at index b, i.e. effectively List(a) = List(b).

Hope it helps. Good luck with your SDD exam tomorrow (and good luck to myself too haha)
 

triviajeon

Member
Joined
Aug 5, 2019
Messages
84
Gender
Undisclosed
HSC
2021
Hey! Thanks so much for your response! :hammer:

After the loop, the program swaps the value at X and Z, i.e. swaps the min/max value in the list. So, since the max value of the list is 6 (index=3) and min value is 1 (index=2), the value of X is 3 and Y is 2 giving option C.
For Q15, I made a table and got it as index=2 for X and index=3 for Z instead? So when I swapped them, it gave me B. I still didn't quite get how you got it the other way originally? I can send you my working out, if you'd like.

I understand Q16! Thank you!

Hope it helps. Good luck with your SDD exam tomorrow (and good luck to myself too haha)
You too mate! :spin:
 

lincolny

Member
Joined
Jul 8, 2021
Messages
32
Gender
Male
HSC
2021
The big takeaway from the logic of the loop is that, X is the index of the element that has the maximum value and Z is the index of the element that has the minimum value.

After the loop, nothing touches the values of X and Z. The SwapElements subprogram only swaps the values in the list, at those indices, but it does not change the values of X and Z. It only reads from the values and know which elements should be swapped.

The question gives the original list which is not swapped yet. After the loop, we get X = 3 and Z = 2, and since those values are not changed after the loop (and essentially remain the same for the rest of the program), after the code has been executed, the values of X should still be 3 and Z should be 2.
 

triviajeon

Member
Joined
Aug 5, 2019
Messages
84
Gender
Undisclosed
HSC
2021
The question gives the original list which is not swapped yet. After the loop, we get X = 3 and Z = 2, and since those values are not changed after the loop (and essentially remain the same for the rest of the program), after the code has been executed, the values of X should still be 3 and Z should be 2.
Ohhh so you mean it is only swapping the values and not the index?
 

lincolny

Member
Joined
Jul 8, 2021
Messages
32
Gender
Male
HSC
2021
Yeah pretty much, line 18 says that "swaps the values of the elements at X and Z". X and Z are parameters passed onto the subprogram that only do one thing - tell the subprogram that, I'll need to swap the elements at this index X and index of Z. It does not change the values of X and Z - so you can probably think about them as integers such as 1 and 2?
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top