Lecture 11: Electrochemistry Introduction

Lecture 11: Electrochemistry Introduction

-

English
11 pages
Lire
Le téléchargement nécessite un accès à la bibliothèque YouScribe
Tout savoir sur nos offres

Description

  • cours magistral
Lecture 11: Electrochemistry Introduction • Reading: Zumdahl 4.10, 4.11, 11.1 • Outline – General Nomenclature – Balancing Redox Reactions (1/2 cell method) – Electrochemical Cells
  • oxidation gain
  • sb sb
  • -2 +5 +6 +2 oxidation reduction cus
  • reduction
  • oxidation
  • redox reaction
  • electrons

Sujets

Informations

Publié par
Nombre de lectures 25
Langue English
Signaler un problème
MassachusettsInstituteofTechnologyDepartmentofElectricalEngineeringandComputerScience
6.087:PracticalProgramminginC
IAP2010
ProblemSet3SolutionsControlflow.Functions.Variablescope.Staticandglobalvariables.I/O:printfandscanf.FileI/O.Characterarrays.Errorhandling.Labelsandgoto.
Out:Wednesday,January13,2010.
Problem3.1
Due:Friday,January15,2010.
Codeprofilingandregisters.Inthisproblem,wewillusesomebasiccodeprofilingtoexaminetheeffectsofexplicitlydeclaringvariablesasregisters.Considerthefibonaccisequencegeneratingfunctionfibonacciinprob1.c,whichisreproducedattheendofthisproblemset(andcanbedownloadedfromStellar).Themain()functionhandlesthecodeprofiling,callingfibonacci()manytimesandmeasuringtheaverageprocessortime.
(a)First,togetabaseline(withoutanyexplicitlydeclaredregisters),compileandrunprob1.c.Codeprofilingisoneoftherarecaseswhereusingadebuggerlikegdbisdiscouraged,becausethedebugger’soverheadcanimpacttheexecutiontime.Also,wewanttoturnoffcompileroptimization.Pleaseusethefollowingcommandstocompileandruntheprogram:dweller@dwellerpc:~$gcc-O0-Wallprob1.c-oprob1.odweller@dwellerpc:~$./prob1.oAvg.executiontime:0.000109msecexampleoutputdweller@dwellerpc:~$
Howlongdoesasingleiterationtaketoexecute(onaverage)?Answer:Onmy64-bitmachine(resultsmaydifferslightlyfor32-bitmachines),theoriginalfibonacci()functiontook0.000109mseconaverage.
(b)Now,modifythefibonacci()functionbymakingthevariablesa,b,andcregistervariables.Recompileandrunthecode.Howlongdoesasingleiterationtakenow,onaverage?Turninaprintoutofyourmodifiedcode(thefibonacci()functionitselfwouldsuffice).Answer:Here’sthemodifiedfibonacci()functionforpart(b):voidf i b o n a c c i( ){/h e r ea r et h ev a r i a b l e st os e ta sr e g i s t e r s/r e g i s t e runsignedi n ta =0 ;r e g i s t e runsignedi n tb =1 ;r e g i s t e runsignedi n tc ;i n tn ;
/don o te d i tb e l o wt h i sl i n e/r e s u l t sb u f f e r[ 0 ]=a ;r e s u l t sb u f f e r[ 1 ]=b ;f o r( n= 2 ;n<NMAX;n++){c=a+ b ;
1
r e s u l t sb u f f e r[ n ]=c;/s t o r ec o d ei nr e s u l t sb u f f e r/a;= b b =c ;}}
Onmy64-bitmachine(resultsmaydifferslightlyfor32-bitmachines),themodifiedfunctiontook0.000111mseconaverage.
(c)Modifythefibonacci()functiononemoretimebymakingthevariablenalsoaregistervariable.Recompileandrunthecodeoncemore.Howlongdoesasingleiterationtakewithallfourvariablesasregistervariables?Answer:Here’sthemodifiedfibonacci()functionforpart(c):voidf i b o n a c c i( ){/h e r ea r et h ev a r i a b l e st os e ta sr e g i s t e r s/r e g i s t e runsignedi n ta =0 ;r e g i s t e runsignedi n tb =1 ;r e g i s t e runsignedi n tc ;r e g i s t e ri n tn ;
/don o te d i tb e l o wt h i sl i n e/r e s u l t sb u f f e r[ 0 ]=a ;r e s u l t sb u f f e r[ 1 ]=b ;f o r( n= 2 ;n<NMAX;n++){c=a+ b ;r e s u l t sb u f f e r[ n ]=c;/s t o r ec o d ei nr e s u l t sb u f f e r/a= b ;b =c ;}}
Onmy64-bitmachine(resultsmaydifferslightlyfor32-bitmachines),thefurthermodifiedfibonacci()functiontook3.4e-05mseconaverage.
(d)Commentonyourobservedresults.Whatcanyouconcludeaboutusingregistersinyourcode?Answer:Theobservedresultssuggestthatstoringsomevariablesinaregistervs.inmemorymayormaynotimpactperformance.Inparticular,storinga,b,andcinregistersdonotappeartoimprovetheperformanceatall,whilestoringninaregisterimprovesperformancebyafactorof3.
Problem3.2
Wearewritingasimplesearchabledictionaryusingmodularprogramming.First,theprogramreadsafilecontainingwordsandtheirdefinitionsintoaneasilysearchabledatastructure.Then,theusercantypeaword,andtheprogramwillsearchthedictionary,andassumingthewordisfound,outputsthedefinition.Theprogramproceedsuntiltheuserchoosestoquit.
Wesplitthecodeintoseveralfiles:main.c,dict.c,anddict.h.Thecontentsofthesefilesaredescribedbrieflybelow.
2
main.c:#include<stdio.h>#include<stdlib.h>#include"dict.h"
intmain(){...}
dict.c:#include"dict.h"
/*datastructureforthedictionary*/char*thedictionary[1000];
voidloaddictionary(){ ...}
char*lookup(char[]){...}
dict.h:/*datastructureforthedictionary*/char*thedictionary[1000];
/*declarations*/voidloaddictionary();char*lookup(char[]);
Answerthefollowingquestionsbasedontheaboveprogramstructure.
(a)Inimplementingthisprogram,youwanttoaccesstheglobalvariablethedictionaryfrommain.c,aswellasfromdict.c.However,duetotheheaderfile’sinclusioninbothsourcedocuments,thevariablegetsdeclaredinbothplaces,creatinganambiguity.Howwouldyouresolvethisambiguity?Answer:Addingtheexternkeywordimmediatelybeforethedeclarationindict.hresolvestheambiguity,causingbothfilestoreferencethevariabledeclaredindict.c.
(b)Now,supposeyouwanttorestrictthedictionarydatastructuretobeaccessibleonlyfromfunctionsindict.c.Youremovethedeclarationfromdict.h.Isitstillpossibletodirectlyaccessormodifythevariablefrommain.c,evenwithoutthedeclarationindict.h?Ifso,howwouldyouensurethedatastructurevariableremainsprivate?Answer:Simplyremovingthedeclarationfromdict.hdoesnotmakethevariableprivatetodict.c.Onecouldsimplyaddanexterndeclarationtomain.coranyothersourcefileandstillbeabletoaccessormodifythedictionarydirectly.Inordertopreventdirectaccess,thedictionaryshouldbedeclaredwiththestatickeywordindict.c.
(c)Congratulations!You’redoneandreadytocompileyourcode.Writethecommandlinethatyoushouldusetocompilethiscode(usinggcc).Let’scallthedesiredoutputprogramdictionary.o.Answer:gcc-g-O0-Wallmain.cdict.c-odictionary.o.Notethattheorderofmain.canddict.cisnotimportant,aslongastheyarebothspecified.
Problem3.3
Boththeforloopandthedo-whileloopcanbetransformedintoasimplewhileloop.Foreachofthefollowingexamples,writeequivalentcodeusingawhileloopinstead.
(a)i n tf a c t o r i a l(i n tn ){ i n ti ,r e t= 1 ;f o r( i;= 2 i<=n ;i ++)r e t=i ;returnr e t;}
3
Answer:i n tf a c t o r i a l(i n tn ){i n ti = 1 ,r e t;= 1 while(++ i<=n )r e t=i ;returnr e t;}
(b)#incl ude<s t d l i b. h>
doubler a n dd o u b l e( ){ /g e n e r a t erandomnumberi n[ 0, 1 )/doubler e t= (double) r a n d( ) ;returnr e t/ (RANDMAX+ 1 ) ;}
i n ts a m p l eg e o m e t r i cr v (doublep ){doubleq ;i n tn =0 ;do{q=r a n dd o u b l e( ) ;n++;}while( q>=p ) ;returnn ;}
Note:Youonlyneedtomodifythesamplegeometricrv()function.Answer:i n ts a m p l eg e o m e t r i cr v (doublep ){doubleq ;i n tn=0 ,c o n d i t i o n=1 ;while( c o n d i t i o n){q=r a n dd o u b l e( ) ;n++;c o n d i t i o n=q>=p ;} returnn ;}
4
Problem3.4
’wc’isaunixutilitythatdisplaythecountofcharacters,wordsandlinespresentinafile.Ifnofileisspecifieditreadsfromthestandardinput.Ifmorethanonefilenameisspecifieditdisplaysthecountsforeachfilealongwiththefilename.Inthisproblem,wewillbeimplementingwc.Oneofthewaystobuildacomplexprogramistodevelopititeratively,solvingoneproblematatimeandtestingitthroroughly.Forthisproblem,startwiththefollowingshellandtheniterativelyaddthemissingcomponents.#incl ude<s t d i o. h>#incl ude<s t d l i b. h>i n tmain(i n ta r g c,chara r g v[ ] ){FILEf p=NULL ;i n tn f i l e s=−−a r g c ;/i g n o r et h enameo ft h eprogrami t s e l f/i n ta r g i d x=1;/i g n o r et h enameo ft h eprogrami t s e l f/charc u rrfile =" ";charc ;/c o u n to fwords,l i n e s,c h a r a c t e r s/unsignedlongnw=0 ,n l=0 , nc=0;
i f(n f i l e s==0){f p=s t d i n;/s t a n d a r di n p u t/nfiles ++;} e l s e/s e tt of i r s t/{c u r r f i l e =a r g v[a r g i d x ++];f p=f o p e n ( c u r r f i l e," r ") ;} while(n f i l e s>0)/f i l e sl e f t>0/{i f(f p==NULL){f p r i n t f ( s t d e r r," U n a b l etoopeninput \ n ") ;e x i t(1 ) ;} nc=nw=n l=0;while( (c=g e t c(f p) ) !=EOF){/TODO:FILLHEREp r o c e s st h ef i l eu s i n gg e t c ( f p )/} p r i n t f(" % ld% s \ n ", nc , c u r r f i l e) ;/n e x tf i l ei fe x i s t s/n f i l e s−−;i f(n f i l e s>0){c u r r f i l e =a r g v[a r g i d x ++];f p=f o p e n ( c u r r f i l e," r ") ;}}return0 ;}
Hint:Inordertocountwords,countthetransitionsfromnon-whitespacetowhitespacecharacters.
5
Answer:Here’sthecodewiththeshellfilledin/6 . 0 8 7IAPS p r i n g2 0 1 0Problems e t2/#incl ude<s t d i o. h>#incl ude<s t d l i b. h>
i n tmain(i n ta r g c,chara r g v[ ] ){FILEf p=NULL ;i n tn f i l e s=−−;a r g c /i g n o r et h enameo ft h eprogrami n ta r g i d x=1;/i g n o r et h enameo ft h eprogramcharc u rrfile =" ";charc ;/f o l l o w i n gu s e dt oc o u n twords/enums t a t e{INSIDE, OUTSIDE};enums t a t ec u r r s t a t e=INSIDE ;
/c o u n to fwords,l i n e s,c h a r a c t e r s/unsignedlongnw=0 ,n l=0 , nc=0;
i f(n f i l e s==0){f p=s t d i n;/s t a n d a r di n p u t/nfiles ++;} e l s e/s e tt of i r s t/{c u r r f i l e =a r g v[a r g i d x ++];f p=f o p e n ( c u r r f i l e," r ") ;} while(n f i l e s>0)/f i l e sl e f t>0/{i f(f p==NULL){f p r i n t f ( s t d e r r," U n a b l etoopeninput \ n ") ;e x i t(1 ) ;} /TODO:FILLHEREp r o c e s st h ef i l eu s i n gg e t c ( f p )/nc=nw=n l=0;while( (c=g e t c(f p) ) !=EOF){nc++;i f(c==’\ n ’){nl ++;} i f(i s s p a c e( c ) ){i f(c u r r s t a t e==INSIDE )nw++;currstate=OUTSIDE ;} e l s e{
6
i t s e l f/i t s e l f/
}
}
currstate=INSIDE;}} /u p d a t et o t a l s/
p r i n t f(" % ld% ld% ld% s \ n ", n l, nw , nc ,c u r r f i l e) ;/n e x tf i l ei fe x i s t s/n f i l e s−−;i f(n f i l e s>0){c u r r f i l e =a r g v[a r g i d x ++];f p=f o p e n ( c u r r f i l e," r ") ;}
7
Problem3.5
Inthisproblem,wewillbereadinginformatteddataandgeneratingareport.Oneofthecommonformatsforinterchangeofformatteddatais’tabdelimited’whereeachlinecorrespondstoasinglerecord.Theindividualfieldsoftherecordareseparatedbytabs.Forthisproblem,downloadthefilestateoutflow0708.txtfromStellar.Thiscontainstheemigrationofpeoplefromindividualstates.Thefirstrowofthefilecontainsthecolumnheadings.Thereareeightselfexplanatoryfields.YourtaskistoreadthefileusingfscanfandgenerateareportoutliningthemigrationofpeoplefromMassachusettstoalltheotherstates.Usethefield”AggrAGI”toreportthenumbers.Also,attheend,displayatotalandverifyitisconsistentwiththeoneshownbelow.Anexamplereportshouldlooklikethefollowing:
STATETOTAL----------------------------"FLORIDA"590800"NEWHAMPSHIRE"421986..........----------------------------Total4609483
Makesurethatthefieldsarealigned.Answer:Here’sthecodewiththeshellfilledin#incl ude<s t d i o. h>#incl ude<s t d l i b. h>#incl ude<s t r i n g. h>
#d e f i n eSTRSIZE100#d e f i n eNFIELDS9i n tmain(){chari n p u t f i l e[ ]=" s t a t e o u t f l o w 0 7 0 8 . txt";/d e f i n ea l lt h ef i e l d s/chars t a t ec o d eo r g[ STRSIZE ] ;charc o u n t r yc o d e[ STRSIZE ] ;o r g chars t a t ec o d ed e s t[ STRSIZE ] ;charc o u n t r yc o d e[ STRSIZE ] ;d e s t chars t a t e[ STRSIZE ] ;a b b r v chars t a t en a m e [ STRSIZE ] ;charline[STRSIZENFIELDS];i n tr e t u r nn u m=0;i n texmptnum =0;i n ta g g ra g i =0;longt o t a l=0;
/f i l er e l a t e d/i n tf i e l d sr e a d =0;FILEi n p u t f i l ef p=f o p e n ( ," r ") ;i f(f p==NULL){f p r i n t f ( s t d e r r," C a n n o topenfile \ n ") ;e x i t(1 ) ;} /s k i pf i r s tl i n e/f g e t s(l i n e, STRSIZENFIELDS ,f p) ;/p r i n tt h eh e a d e r/
8