Consider the following definition of a procedure, max, which takesa list, L, of
ID: 3611075 • Letter: C
Question
Consider the following definition of a procedure, max, which takesa list, L, of numbers as an argument. the notation L(1) refers tothe first entry of the list L.max(L)
{
If L is empty return(error)
If L has length equal to 1 thanreturn (L(1))
devide L into non-empty listsL1 and L2:
r1=max(L1);
r2=max(L2);
if r1>r2 then return(r1) elsereturn(r2);
Consider the pseudocode divide L into non empty lists L1 and L2.Suggest a way in which this pseudocode instruction might beimplemented?
Explanation / Answer
divide L into non empty lists L1 and L2. This can be implemented such that L is dividedinto half and the first Half is L1 and the second half is L2 Let the length of L be l floor(n) gives the largest integer smaller than or equal ton... eg: floor(5/2) =2 [since 2 is the largest integer smallerthan 2.5] floor(6/2) =3 [since 3 is thelargest integer smaller or equal to 6/2 =3] Now get the floor of l, Then L1 has elements L[1] .....L[ floor(l/2)] L2 will have elements L[floor(l/2) +1] ........L[l]
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.