(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 ;}}
(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 ;}}
(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,char∗a r g v[ ] ){FILE∗f 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∗/char∗c 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 ;}
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,char∗a r g v[ ] ){FILE∗f 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 eprogramchar∗c 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 ") ;}
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[STRSIZE∗NFIELDS];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;FILE∗i 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, STRSIZE∗NFIELDS ,f p) ;/∗p r i n tt h eh e a d e r∗/