{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

final_practice - Practice exam   Problem1:MP5...

Info icon This preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Practice exam   Problem1:MP5 ­related Problem2:LC ­3 Problem3:Debugging Problem4:Linkeddatastructures Problem5:Multi ­dimensionalarrays   Pleasenote Youareallowedtobringfourhandwritten8.5x11"sheetsofnotes.Printednoteswill beconfiscated! Ontherealexam,wewillprovideyouwiththeactualcodestoworkonaswellasdetails aboutcompiling,executing,andtesting. Forthepracticeexam,weprovideyouwithjustahandoutandsomelimitedexamples ofhowtousethefunctionsthatyouarerequiredtoimplement.Itwillbeagood exerciseforyoutowritetherestofthecode.      1   ­ Forthisproblem,youwillbeimplementingthedestroysearchtreefunctionwhichtearsdown anAIsearchtreedatastructure.Eachnodeonthetreehasadistanceassociatedwithit,andan arrayoffourpointerstochildnodes,eachelementcorrespondingtothefourcardinal directions.ANULLpointermeansnochildnodeinthatdirection. Implementation Requirements Youarerequiredtoimplementthefollowingrecursivefunctionthatdestroysthetree:  voiddestroy_search_tree(tree_node*node)   Treenodestructureisdefinedasfollows:  typedefstructtree_node { intdistance  /*!Pointerstothefourchildnodes:N,S,E,W*/ structtree_node*branches[4]  }tree_node   Wehighlyrecommendrunningyourcodewithvalgrindtoverifythatthetreeisde ­allocated correctly. Example /*createsometree*/ tree_node*root=calloc(1,sizeof(tree_node))  for(i=0 i<4 i++) root >branches[i]=calloc(1,sizeof(tree_node))  for(i=0 i<4 i++) root >branches[1] >branches[i]=calloc(1,sizeof(tree_node))   /*destroyit*/ destroy_search_tree(root)    2   ­ Thefollowingcodecomputesthedepthofabinarytree. ConvertitfromCtoLC ­3assembly:  typedefstructs_node { intvalue  structs_node*left  structs_node*right  }node   intmax_depth(node*node) { intldepth,rdepth   if(node==NULL) { return0  } else { ldepth=max_depth(node >left)  rdepth=max_depth(node >right)   if(ldepth>rdepth) { returnldepth+1  } else { returnrdepth+1  } } }  Rememberthatstructuresmerelyconsistofsimpledatatypeswhereoneelementalways comesafteranotherinmemory.Thatistosaythatifrootpointstoanaddressofx6000,the datavalueiscontainedwithinx6000,theleftpointeraddressiscontainedwithinx6001, andtherightpointer'saddressiscontainedwithinx6002.  Assumethiscodeiscaller ­saveforR0 ­R4,whichmeansthefunctiondoesnotneedtosave ­ restorethem.  YouaregivenaskeletonLC ­3assemblycode(seenextpage).Ontheexam,somesectionswill bealreadyimplementedandsomesectionswillbeclearlymarkedforyourimplementation. Typically,suchsectionsshouldrequirenomorethan4 ­6linesofLC ­3assemblycode(ifitis muchlonger,youareprobablydoingsomethingwrong).Youmustdoonlywhatisaskedofyou ineachsectionofcodetoreceivecredit.Yourprogramwillbeautomaticallygradedbysetting thePCaddresstotheappropriatelabelandbreakingat thenextlabeltoobservethechanges totheLC ­3statemachine.Thus,DONOTalterlabelsorcommentsalreadyinthecode!  3   .origx3000 MAX_DEPTH  Buildtheactivationrecord  PART_A  IMPLEMENTME!  if(node==NULL)  {  return0   }    Note:If"return0"istobeexecuted,setthereturn  valueto"0"andthenjump/branchtoDONEwheretherest  ofthestackistorndown.Otherwisejump/branchtoPART_B  PART_B  IMPLEMENTME!  ldepth=max_depth(node >left)   rdepth=max_depth(node >right)     Note:Youwillneedtosetuptheparametersforthe  recursivecallandthenJSRtoMAX_DEPTH.Finally,  readthereturnvaluesandstoreinlocalvariablesas  neededtoimplementCcode.  PART_C  IMPLEMENTME!  if(ldepth>rdepth)  {  returnldepth+1   }    Note:Ifthisistobeexecuted,setthereturnvalue  andjump/branchtoDONE.Otherwisejump/branchtoPART_D.  PART_D  IMPLEMENTME!  else  {  returnrdepth+1   }    Note:Setreturnvalueandjump/branchtoDONE.  DONE  Teardowntheactivationrecordandreturn RET .end  4  Youaregiventhefollowingcodeforsortingelementsofalinkedlist.Thecodecontainsasingle bugwhichyouareaskedtofindandfix.Inordertodoso,you needtosetupatest environmentthatcallssort_listfunctiononsomelinkedlist.Anymodificationsofthe providedcodenotrelatedtofixingthebugarenotallowed.Goodluck!  typedefstructnode_t{ intval  structnode_t*next  }node   intcompare(void*xa,void*xb){ node*a=xa  node*b=xb  returna >val b >val  }  intcount_nodes(node*head){ if(!head)return0  return1+count_nodes(head >next)  }  voidsort_list(node**head){ intn=count_nodes(*head)  node**all=malloc(sizeof(node*)*n)  intflag=1  node*tmp=*head  inti   for(i=0 i<n i++){ all[i]=tmp  tmp=tmp >next  }  while(flag){ flag=0  for(i=1 i<n i++){ if(compare(all+i 1,all+i)<0){ tmp=all[i 1]  all[i 1]=all[i]  all[i]=tmp  flag=1  } } }  *head=all[0]  for(i=1 i<n i++) all[i 1] >next=all[i]  all[i 1] >next=NULL   free(all)  }  5  Inthisassignment,youareaskedtoimplementiterativefunctionsforinsertingandremoving Nthelementintoalinkedlist.  Givenalinkedlist,head,apointertoanewnode,newNode,andanumberN,implementa functionthatinsertsthenewnodeafterNthnodeintheexistinglinkedlist.Ifthelistisempty, the Nnodes, thenewnodeshouldbeaddedtothetailofthelinkedlist.   Givenalinkedlist,headandanumberN,implementafunctionthatremovesNthnodefrom thelinkedlist.Thenodeshouldberemovedfromthelinkedlist,butnotde  ­allocatedfrom memory.Thefunctionshouldreturnapointertotheremovednode.Ifthelistisempty,the functionshouldreturnNULL.Ifthelinkedlisthasfewerthan Nnodes,thefunctionshould returnNULL. Implementation Requirements Youarerequiredtoimplementthefollowingtwofunctions:  voidinsert(node**head,node*newNode,intN)  node*remove(node**head,intN)   Linkedlistnodestructureisdefinedasfollows:  typedefstructnode_structnode  structnode_struct { intvalue  node*next  }    Example Insertion:Inserting{7}intoposition2of{1,2,3,3,5}shouldresultin{1,2,7,3,3,5}.  Removal:Removingnode1of{1,2,3,3}shouldresultin{1,3,3},and{2}shouldbereturned.  6   ­ Inthisassignment,youwillimplementafunctionforcomputing2Dimage convolution.The resultofanimageconvolutionisanewimageofthesamesizeastheoriginalimagewithpixel valuescomputedasthesumoftheproductbetweenthepixelsfromtheoriginalimageandthe convolutionkernel. Algorithm GivenanimageArepresentedasanN×MarrayandaconvolutionkernelB represented as 3×3 array,theresultoftheconvolutionofimageAbytheconvolutionkernelBisanN×MimageC whosepixelsarecomputedas   where i  1istherowindexand j M 1isthecolumnindex. Implementation Requirements Youarerequiredtowritethefollowingfunctionthatimplementstheabovealgorithm:   voidim_convolution(int*A,int*B,int*C,intN,intM)   Thetwoparameters,AandB,arepointerstoone ­dimensionalarrayscontainingthetwo ­ dimensionalimageandconvolutionkernelarrays,respectively.TheparameterCisapointerto thearrayforstoringthefinalimage.NotethattheimageAhasdimensionsN×Mpixels,the convolutionkernelBhasfixeddimensions3×3pixels,andtheimageChasdimensionsN×M pixels.Youcanassumethatallthreearraysareproperlyallocatedandtheinputarraysare filledinwithdatastoredinrow ­majororder.Youarerequiredtofillintheoutputarray,C, withtheconvolvedimagealsostoredintherow ­majororder.  Youarealsorequiredtoimplementahelperfunctionthat computesaconvolutionatlocation (x,y):  intpoint_cnv(int*A,int*B,intN,intM,intx,inty)   Youarerequiredtocallthisfunctionwithinim_convolutionfunctiontocomputethepixel valuesoftheresultingimageC.  Whencomputingtheconvolution,assumethatimageAiszero ­padded.Thatis,whenpoint fallsoutsidetheimagearray(wheni+s<0,ori+s , or j+t<0,orj+t ), assumeitsvalueisalwayssetto0. 7  Example intA={1,1,1,1,1,/*a5×5pixelsimage*/ 9,9,9,9,9, 9,9,9,9,9, 5,5,5,5,5, 2,2,2,2,2}  intB={ 1, 2, 1,/*a3×3convolutionkernel*/ 0,0,0, 1,2,1}  intC[25] /*a5×5image*/ im_convolution(A,B,C,5,5) /*C=AconvolvedwithB*/  Afterthiscodeisexecuted,imagearrayCshouldbesetto  {27,36,36,36,27 24,32,32,32,24,  12, 16, 16, 16, 12,  21, 28, 28, 28, 21,  15, 20, 20, 20, 15}  8  ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern