A2 instructions 1/4 CIS*2500 – Assignment 2 Question 1: Swapping Rows and Columns 1. Writeafunction,double_array(int row, int col),whichreturnsapointertoastructcalled Double_Array....

It's a very easy assignment. Hardly 1 hour. I just don't have the time.
I need all the functions, a .h file, a makefile and a read me.
It has to be plagiarism-free.
Please do not use any high-end programming code because this is a first-year undergraduate assignment.
The function mentioned in the instructions is as follows:
double rand_double(double a, double b)
{
double rtrn;
int c=0;
rtrn = ((double)rand()/(double)RAND_MAX) * (b-a)+a;
c++;
return rtrn;
}


A2 instructions 1/4 CIS*2500 – Assignment 2 Question 1: Swapping Rows and Columns 1. Writeafunction,double_array(int row, int col),whichreturnsapointertoastructcalled Double_Array.TheDouble_Arraystructholdsanuninitialized2dimensionaldoublearray,therowsize, andthecolumnsize(accessedusing.array,.rowsize,.colsizerespectively). Note: • Fromthispointon,allfunctionswillbereferredtobytheirparametersignature o i.e.thetypesoftheparameterswillbegiven,butnottheirvariablenames. • Thisgivesyoudesigncontroloverthelocalvariablenamesusedinthefunction. • However,insomecasesparameternamesareprovidedinthequestion. o Inthesecases,pleaseusethem. 2. Usingthefunctionrand_double()youusedinLabAssignment2,writeafunction randomize_array(struct Double_Array *, double, double) thattakesaDouble_Arraystruct,and twovaluesoftypedoubleandinitializeseachelementofthearrayinDouble_Arraytoarandomly generatedvaluebetweenthetwoinputvalues(inclusive). 3. Createafunctioncalledprint_array()thattakesaDouble_Array structpointerandprintsthearray witheachelementdisplaying1decimalplace,withthecolumnelementsliningupvertically. 4. Createafunctioncalledfree_array()whichtakesaDouble_Array structpointerandfreesthestruct aswellasthearraywithinit. 5. Createafunctioncalledswap_rows(struct Double_Array *, int, int) wherethetwointegerarerownumberstobeswapped.Makesureeachrownumberisvalidaccordingto thestructure.Ifvalidtherowsareswappedandthefunctionreturns1,otherwisetherowsarenot swappedandthefunctionreturns0. Note: Therearetwopossibleapproachestoswappingrows. Oneisfastertoexecute,andeventocode. Bonus½markifyouimplementthefasterapproach. PlaceyourexplanationasbothacommentinthecodeandinyourreadmefileasQ1a 6. Createafunctioncalledswap_columns(struct Double_Array *, int, int)wherethetwointegerare columnnumberstobeswapped.Makesureeachcolumnnumberisvalidaccordingtothestructure.If validthecolumnsareswappedandthefunctionreturns1,otherwisethecolumnsarenotswappedand thefunctionreturns0. Note: The“fast”approachfromswap_rowscannotbeappliedhere. Bonus½markifyouexplainwhy. PlaceyourexplanationasbothacommentinthecodeandinyourreadmefileasQ1b. 2/4 7. Createamain()calleda2_q1.c thatperformsthefollowing: • Printsthefollowingheader ------------------------------------- Question1 ------------------------------------- • CreatesaDouble_Arraystructthatholdsa6rowby9columnarray • Randomlyinitializesthearrayinthestruct(youmaychoosetheupperandlowerboundsaandb) • Printsoutthearrayfromthestruct • Randomlypicktworowstoswap,andswapsthem • Informtheuserwhichtworowswereswappedandthenprintoutthearray • Randomlypicktocolumnstoswap,andswapthem • Informtheuserwhichtwocolumnswereswappedandthenprintoutthearray • Print3blanklines Makesureyoufreeallmallocedmemorybeforeexitingtheprogram. 3/4 Question 2: Pointer, Shallow and Deep Copy 1. Writeafunctionshallow_copy(struct Double_Array *) thatreturnsacopyoftheDouble_Arraystructthatispassedin(hasadifferentpointervalue)butholds thesamecontent.Anychangetotheinteriorofeitherthenewortheoldstructshouldbereflectedinthe otherautomatically,eventhoughthepointerstothetwostructsaredifferent. 2. Writeafunctiondeep_copy(struct Double_Array *) thatreturnsacopyoftheDouble_Arraystructthatispassedin(hasadifferentpointervalue)butholds thesamecontent.Now,however,anychangetotheinteriorofonestructshouldnotbereflectedinthe other. 3. Writeafunctionprint_struct(struct Double_Array *, char *)thatprintsout • aheaderstringonthefirstline(thestringthatwaspassedinasthesecondparameter) • “structaddress=%p”onthesecondline • “row_size=%d,col_size=%d”onthethirdline • “arrayaddress=%p,withcontents:”onthefourthline • leavethefifthlineblank • printthearrayfromthesixthlineon(printedinthesamewayyoudidinprint_arrayfromQ1) • afterthearrayisprinted,leavetwoblanklines 4. Createamain()calleda2_q2.cthatperformsthefollowing: Q2a • Printthefollowingheader ------------------------------------- Question2a ------------------------------------- • Declareavariablecalleda1thatholdsaDouble_Arraystructthatcontainsa6x9array(asinQ1) note:a1shouldholdastructpointer,notastructitself • Initializea1torandomvaluesbetween0.0and10.0 • Printtheaddressofa1usingtheprintfcommand“theaddressofa1is%p” • Printthestructurepointedtobya1withtheheader“Thestructurepointedtobya1is:” • Declareavariablecalleda2thatcanholdthesamestructpointerasa1,andseta2toa1 • Printtheaddressofa2usingtheprintfcommand“theaddressofa1is%p” • Printthestructurepointedtobya2withtheheader“Thestructurepointedtobya2is:” • Createashallowcopyofa1andstoreitinavariablecalleda_shallow • Printtheaddressofa_shallowusingtheprintfcommand“theaddressofa_shallowis%p” • Printthestructurepointedtobya2withtheheader“Thestructurepointedtobya_shallowis:” • Createadeepcopyofa1andstoreitinavariablecalleda_deep • Printtheaddressofa_deepusingtheprintfcommand“theaddressofa_deepis%p” • Printthestructurepointedtobya2withtheheader“Thestructurepointedtobya_deepis:” • Print3blanklines 4/4 Q2b • Printthefollowingheader ------------------------------------- Question2b ------------------------------------- • Ina1,set[0][1]to100.0 note:theaboveinstruction,forthepurposeofthisassignment,isshorthandforthefollowing: settheelementatthe0throwand1stcolumnofthearrayinthestructheldbya1 tothevalue100.0 • Ina2,set[1][2]to200.0 • Ina_shallow,set[2][3]to300.0 • Ina_deep,set[3][4]to400.0 • Printa1,a2,a_shallowanda_deep • Print3blanklines Explaintheresultsobservedinthereadmefile.LabelyouranswerasQ2b Q2c • Printthefollowingheader ------------------------------------- Question2c ------------------------------------- • Declareavariablecalledb1thatholdsaDouble_Arraystructthatcontainsa6x9array(asinQ2a) • Initializeb1torandomvaluesbetween10.0and20.0 • Seta2-> array = b1 -> array • Printa1,a2,a_shallow,a_deep,andb1 • Ina1,set[0][1]to5000.0 • Ina2,set[1][2]to6000.0 • Ina_shallow,set[2][3]to700.0 • Ina_deep,set[3][4]to8000.0 • Inb1,set[4][5]to9000.0 • Printa1,a2,a_shallow,a_deep,andb1 Explaintheresultsobservedinthereadmefile.LabelyouranswerasQ2c Q2d • Freeallmalloc’dpointers.Makesureyoudonothaveamemoryleak. InthereadmefileunderQ2dexplainwhichpointersneedtobefreed, inwhatorder,andwhytheorderisimportant(bespecific).
Feb 07, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here