As I am at the very beginning of my adventures in coding, the following may seem simple to the more advanced coder. Nonetheless, it is where I am in my journey. The task as a whole is starting to make sense, but as I'm working my through the quizzes, I'm still missing several questions. At any rate, I am really enjoying this adventure and will be continually reflecting upon my learning.
---------------------------------------------------------------------------------
Assume that there is a function len that returns the length of a given array. Now consider the following definition for the function combine:
int[] combine( int[] list1 , int[] list2 )
int[] result ← new int[ len ( list1 ) + len ( list2 ) ]
for (int i ← 0; i < len ( result ) ; i ← i + 1)
// loop body
end for
return result
end combine
Which of the following loop bodies will result in the combine returning a new list with the contents of list1 preceding the contents of list2?
if ( i < len ( list1 ) ) result[i] ← list1[i] else result[i] ← list2[i - len( list1 )] end if
Explanation
When copying the contents of list2, each element must be copied from list2 starting at its first index. However, to achieve that first index we must "remove" the length of list1 from the current index where we are storing the copied values in order for the array index to artificially "start over" at index 0 in list2.
The sample question from above was copied/pasted from https://courses.edx.org
No comments:
Post a Comment