版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
Chapter2AnswerstoSelectedExercises2.[was#2](a)Theprogramcontainsonedirective(#include)andfourstatements(threecallsofprintfandonereturn).Parkinson'sLaw:Workexpandssoastofillthetimeavailableforitscompletion.3.[was#4]#include<>intmain(void){intheight=8,length=12,width=10,volume;volume=height*length*width;printf("Dimensions:%dx%dx%d\n",length,width,height);printf("Volume(cubicinches):%d\n",volume);printf("Dimensionalweight(pounds):%d\n",(volume+165)/166);return0;}4.[was#6]Here'sonepossibleprogram:#include<>intmain(void){inti,j,k;floatx,y,z;printf("Valueofi:%d\n",i);printf("Valueofj:%d\n",j);printf("Valueofk:%d\n",k);printf("Valueofx:%g\n",x);printf("Valueofy:%g\n",y);printf("Valueofz:%g\n",z);return0;}WhencompiledusingGCCandthenexecuted,thisprogramproducedthefollowingoutput:Valueofi:5618848Valueofj:0Valueofk:6844404Valueofx:Valueofy:Valueofz:Thevaluesprinteddependonmanyfactors,sothechancethatyou'llgetexactlythesenumbersissmall.5.[was#10](a)isnotlegalbecause100_bottlesbeginswithadigit.AnswerstoSelectedProgrammingProjects4.[was#8;modified]#include<>intmain(void){floatoriginal_amount,amount_with_tax;printf("Enteranamount:");scanf("%f",&original_amount);amount_with_tax=original_amount*;printf("Withtaxadded:$%.2f\n",amount_with_tax);return0;}Theamount_with_taxvariableisunnecessary.Ifweremoveit,theprogramisslightlyshorter:#include<>intmain(void){floatoriginal_amount;printf("Enteranamount:");scanf("%f",&original_amount);printf("Withtaxadded:$%.2f\n",original_amount*;return0;}Chapter3AnswerstoSelectedExercises2.[was#2](a)printf("%",x);(b)printf("%",x);(c)printf("%",x);(d)printf("%",x);5.[was#8]Thevaluesofx,i,andywillbe,45,and.6,respectively.AnswerstoSelectedProgrammingProjects1.[was#4;modified]#include<>intmain(void){intmonth,day,year;printf("Enteradate(mm/dd/yyyy):");scanf("%d/%d/%d",&month,&day,&year);printf("Youenteredthedate%d%.2d%.2d\n",year,month,day);return0;}3.[was#6;modified]#include<>intmain(void){intprefix,group,publisher,item,check_digit;printf("EnterISBN:");scanf("%d-%d-%d-%d-%d",&prefix,&group,&publisher,&item,&check_digit);printf("GS1prefix:%d\n",prefix);printf("Groupidentifier:%d\n",group);printf("Publishercode:%d\n",publisher);printf("Itemnumber:%d\n",item);printf("Checkdigit:%d\n",check_digit);/*Thefiveprintfcallscanbecombinedasfollows:number:%d\nCheckdigit:%d\n",prefix,group,publisher,item,check_digit);return0;}Chapter4AnswerstoSelectedExercisesofijcouldbeeither–1or–2,dependingontheimplementation.OntheotherInC99,ontheotherhand,thevalueof(-i)/jmustbeequaltothevalue9.[was#6](b)321thexpressionsisiaftertheincrementhasbeenperformed.AnswerstoSelectedProgrammingProjects2.[was#4]#include<>intmain(void){printf("Enterathree-digitnumber:");scanf("%d",&n);printf("Thereversalis:%d%d%d\n",n%10,(n/10)%10,n/100);return0;}Chapter5AnswerstoSelectedExercises2.[was#2]4.[was#4](i>j)-(i<j)since5isnotequalto–9.10.[was#16]Theoutputisonetwosincetherearenobreakstatementsafterthecases.AnswerstoSelectedProgrammingProjects2.[was#6]#include<>intmain(void){inthours,minutes;printf("Entera24-hourtime:");scanf("%d:%d",&hours,&minutes);printf("Equivalent12-hourtime:");if(hours==0)printf("12:%.2dAM\n",minutes);elseif(hours<12)printf("%d:%.2dAM\n",hours,minutes);elseif(hours==12)printf("%d:%.2dPM\n",hours,minutes);printf("%d:%.2dPM\n",hours-12,minutes);return0;}4.[was#8;modified]#include<>intmain(void){intspeed;printf("Enterawindspeedinknots:");scanf("%d",&speed);if(speed<1)printf("Calm\n");elseif(speed<=3)printf("Lightair\n");elseif(speed<=27)printf("Breeze\n");elseif(speed<=47)printf("Gale\n");elseif(speed<=63)printf("Storm\n");printf("Hurricane\n");return0;}6.[was#10]#include<>intmain(void){intcheck_digit,d,i1,i2,i3,i4,i5,j1,j2,j3,j4,j5,first_sum,second_sum,total;printf("Enterthefirst(single)digit:");scanf("%1d",&d);printf("Enterfirstgroupoffivedigits:");scanf("%1d%1d%1d%1d%1d",&i1,&i2,&i3,&i4,&i5);printf("Entersecondgroupoffivedigits:");scanf("%1d%1d%1d%1d%1d",&j1,&j2,&j3,&j4,&j5);printf("Enterthelast(single)digit:");scanf("%1d",&check_digit);first_sum=d+i2+i4+j1+j3+j5;second_sum=i1+i3+i5+j2+j4;total=3*first_sum+second_sum;if(check_digit==9-((total-1)%10))printf("VALID\n");printf("NOTVALID\n");return0;}10.[was#14]#include<>intmain(void){intgrade;printf("Enternumericalgrade:");scanf("%d",&grade);if(grade<0||grade>100){printf("Illegalgrade\n");return0;}switch(grade/10){case10:case9:printf("Lettergrade:A\n");break;case8:printf("Lettergrade:B\n");break;case7:printf("Lettergrade:C\n");break;case6:printf("Lettergrade:D\n");break;case5:case4:case3:case2:case1:case0:printf("Lettergrade:F\n");break;}return0;}Chapter6AnswerstoSelectedExercisesementedbeforetheloopbodyisexecuted.10.[was#12]Considerthefollowingwhileloop:while(…){…continue;…}Theequivalentcodeusinggotowouldhavethefollowingappearance:while(…){…gotoloop_end;…loop_end:;/*nullstatement*/}12.[was#14]for(d=2;d*d<=n;d++)if(n%d==0)break;Theifstatementthatfollowstheloopwillneedtobemodifiedaswell:if(d*d<=n)printf("%disdivisibleby%d\n",n,d);printf("%disprime\n",n);weremoveit,thestatementisnowcorrect:if(n%2==0)printf("niseven\n");AnswerstoSelectedProgrammingProjects2.[was#2]#include<>intmain(void){intm,n,remainder;printf("Entertwointegers:");scanf("%d%d",&m,&n);while(n!=0){remainder=m%n;m=n;n=remainder;}printf("Greatestcommondivisor:%d\n",m);return0;}4.[was#4]#include<>intmain(void){floatcommission,value;printf("Entervalueoftrade:");scanf("%f",&value);while(value!={if(value<commission=+.017f*value;elseif(value<commission=+.0066f*value;elseif(value<commission=+.0034f*value;elseif(value<commission=+.0022f*value;elseif(value<commission=+.0011f*value;commission=+.0009f*value;if(commission<commission=;printf("Commission:$%.2f\n\n",commission);printf("Entervalueoftrade:");scanf("%f",&value);}return0;}6.[was#6]#include<>intmain(void){inti,n;printf("Enterlimitonmaximumsquare:");scanf("%d",&n);for(i=2;i*i<=n;i+=2)printf("%d\n",i*i);return0;}8.[was#8]#include<>intmain(void){inti,n,start_day;printf("Enternumberofdaysinmonth:");scanf("%d",&n);printf("Enterstartingdayoftheweek(1=Sun,7=Sat):");scanf("%d",&start_day);/*printanyleading"blankdates"*/for(i=1;i<start_day;i++)printf("");/*nowprintthecalendar*/for(i=1;i<=n;i++){printf("%3d",i);if((start_day+i-1)%7==0)printf("\n");}return0;}Chapter7AnswerstoSelectedExercises3.[was#4](b)isnotlegal.asitsfirstargument.10.[was#14]unsignedint,becausethe(int)castappliesonlytoj,notj*k.resultisconvertedtodoubleandstoredind.thelargestvalueoftypeint.AnswerstoSelectedProgrammingProjectsreoccurringat46341.2.[was#8]#include<>intmain(void){inti,n;charch;printf("Thisprogramprintsatableofsquares.\n");printf("Enternumberofentriesintable:");scanf("%d",&n);ch=getchar();/*disposeofnew-linecharacterfollowingnumberofentries*//*couldsimplybegetchar();*/for(i=1;i<=n;i++){printf("%10d%10d\n",i,i*i);if(i%24==0){printf("PressEntertocontinue...");ch=getchar();/*orsimplygetchar();*/}}return0;}5.[was#10]#include<>#include<>intmain(void){intsum=0;charch;printf("Enteraword:");while((ch=getchar())!='\n')switch(toupper(ch)){case'D':case'G':sum+=2;break;case'B':case'C':case'M':case'P':sum+=3;break;case'F':case'H':case'V':case'W':case'Y':sum+=4;break;case'K':sum+=5;break;case'J':case'X':sum+=8;break;case'Q':case'Z':sum+=10;break;default:sum++;break;}printf("Scrabblevalue:%d\n",sum);return0;}6.[was#12]#include<>intmain(void){printf("Sizeofint:%d\n",(int)sizeof(int));printf("Sizeofshort:%d\n",(int)sizeof(short));printf("Sizeoflong:%d\n",(int)sizeof(long));printf("Sizeoffloat:%d\n",(int)sizeof(float));printf("Sizeofdouble:%d\n",(int)sizeof(double));printf("Sizeoflongdouble:%d\n",(int)sizeof(longdouble));return0;}Sincethetypeofasizeofexpressionmayvaryfromoneimplementationtoanother,it'snecessaryinC89tocastsizeofexpressionstoaknowntypesafetocastthemtoint.(Ingeneral,however,it'sbesttocastsizeofthecastbyusingthe%zuconversionspecification.Chapter8AnswerstoSelectedExercises1.[was#4]Theproblemwithsizeof(a)/sizeof(t)isthatitcan'teasilyesintheunderlyingcharacterset,whichistrueofASCIIandotherpopularcharactersets.7.[was#10]constintsegments[10][7]={{1,1,1,1,1,1},{1,1,0,1,1,0,1},{1,1,1,1,0,0,1},{0,1,1,0,0,1,1},{1,0,1,1,0,1,1},{1,0,1,1,1,1,1},{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};AnswerstoSelectedProgrammingProjects2.[was#2]#include<>intmain(void){intdigit_count[10]={0};intdigit;ngnprintf("Enteranumber:");scanf("%ld",&n);while(n>0){digit=n%10;digit_count[digit]++;n/=10;}printf("Digit:");for(digit=0;digit<=9;digit++)printf("%3d",digit);printf("\nOccurrences:");for(digit=0;digit<=9;digit++)printf("%3d",digit_count[digit]);printf("\n");return0;}5.[was#6]#include<>#defineNUM_RATES((int)(sizeof(value)/sizeof(value[0])))#defineINITIAL_BALANCEintmain(void){inti,low_rate,month,num_years,year;doublevalue[5];printf("Enterinterestrate:");scanf("%d",&low_rate);printf("Enternumberofyears:");scanf("%d",&num_years);printf("\nYears");for(i=0;i<NUM_RATES;i++){printf("%6d%%",low_rate+i);value[i]=INITIAL_BALANCE;}printf("\n");for(year=1;year<=num_years;year++){printf("%3d",year);for(i=0;i<NUM_RATES;i++){for(month=1;month<=12;month++)value[i]+=((double)(low_rate+i)/12)/*value[i];printf("%",value[i]);}printf("\n");}return0;}8.[was#12]#include<>#defineNUM_QUIZZES5#defineNUM_STUDENTS5intmain(void){intgrades[NUM_STUDENTS][NUM_QUIZZES];inthigh,low,quiz,student,total;for(student=0;student<NUM_STUDENTS;student++){printf("Entergradesforstudent%d:",student+1);for(quiz=0;quiz<NUM_QUIZZES;quiz++)scanf("%d",&grades[student][quiz]);}printf("\nStudentTotalAverage\n");for(student=0;student<NUM_STUDENTS;student++){printf("%4d",student+1);total=0;for(quiz=0;quiz<NUM_QUIZZES;quiz++)total+=grades[student][quiz];printf("%3d%3d\n",total,total/NUM_QUIZZES);}printf("\nQuizAverageHighLow\n");for(quiz=0;quiz<NUM_QUIZZES;quiz++){printf("%3d",quiz+1);total=0;high=0;low=100;for(student=0;student<NUM_STUDENTS;student++){total+=grades[student][quiz];if(grades[student][quiz]>high)high=grades[student][quiz];if(grades[student][quiz]<low)low=grades[student][quiz];}printf("%3d%3d%3d\n",total/NUM_STUDENTS,high,low);}return0;}Chapter9AnswerstoSelectedExercises2.[was#2]intcheck(intx,inty,intn){return(x>=0&&x<=n-1&&y>=0&&y<=n-1);}4.[was#4]intday_of_year(intmonth,intday,intyear){intnum_days[]={31,28,31,30,31,30,31,31,30,31,30,31};intday_count=0,i;for(i=1;i<month;i++)day_count+=num_days[i-1];/*adjustforleapyears,assumingtheyaredivisibleby4*/if(year%4==0&&month>2)day_count++;returnday_count+day;}correct.Centuriesarespecialcases:ifayearisamultipleof100,thenitmustalsobeamultipleof400inordertobealeapyear.Thecorrecttestisyear%4==0&&(year%100!=0||year%400==0)6.[was#6;modified]intdigit(intn,intk){inti;for(i=1;i<k;i++)n/=10;returnn%10;}anintvalueinC89;inC99,omittingthereturntypeisillegal.10.[was#10]intlargest(inta[],intn){inti,max=a[0];for(i=1;i<n;i++)if(a[i]>max)max=a[i];returnmax;}intaverage(inta[],intn){inti,avg=0;for(i=0;i<n;i++)avg+=a[i];returnavg/n;}intnum_positive(inta[],intn){inti,count=0;for(i=0;i<n;i++)if(a[i]>0)count++;returncount;}15.[was#12;modified]doublemedian(doublex,doubley,doublez){doubleresult;if(x<=y)if(y<=z)result=y;elseif(x<=z)result=z;elseresult=x;else{if(z<=y)result=y;elseif(x<=z)result=x;elseresult=z;}returnresult;}17.[was#14]intfact(intn){pb(n);inti,result=1;for(i=2;i<=n;i++)result*=i;returnresult;}19.[was#16]Thefollowingprogramteststhepbfunction:#include<>voidpb(intn);intmain(void){printf("Enteranumber:");scanf("%d",&n);printf("Outputofpb:");printf("\n");return0;}voidpb(intn){if(n!=0){pb(n/2);putchar('0'+n%2);}}pbprintsthebinaryrepresentationoftheargumentn,assumingthatnisgreaterthan0.(Wealsoassumethatdigitshaveconsecutivecodesintheunderlyingcharacterset.)Forexample:Enteranumber:53Outputofpb:110101Atraceofpb'sexecutionwouldlooklikethis:pb(53)findsthat53isnotequalto0,soitcallspb(26),whichfindsthat26isnotequalto0,soitcallspb(13),whichfindsthat13isnotequalto0,soitcallspb(6),whichfindsthat6isnotequalto0,soitcallspb(3),whichfindsthat3isnotequalto0,soitcallspb(1),whichfindsthat1isnotequalto0,soitcallspb(0),whichfindsthat0isequalto0,soitreturns,causingpb(1)toprint1andreturn,causingpb(3)toprint1andreturn,causingpb(6)toprint0andreturn,causingpb(13)toprint1andreturn,causingpb(26)toprint0andreturn,causingpb(53)toprint1andreturn.Chapter10AnswerstoSelectedExercises1.[was#2](a)a,b,andcarevisible.(b)a,anddarevisible.(c)a,d,andearevisible.(d)aandfarevisible.AnswerstoSelectedProgrammingProjects3.[was#4]#include<>/*C99only*/#include<>#include<>#defineNUM_CARDS5#defineRANK0#defineSUIT1/*externalvariables*/inthand[NUM_CARDS][2];/*01________ranksuitboolstraight,flush,four,three;intpairs;/*canbe0,1,or2*//*prototypes*/voidread_cards(void);voidanalyze_hand(void);voidprint_result(void);/***********************************************************main:Callsread_cards,analyze_hand,andprint_result**repeatedly.***********************************************************/intmain(void){for(;;){read_cards();analyze_hand();print_result();}}/***********************************************************read_cards:Readsthecardsintotheexternalvariable***hand;checksforbadcardsandduplicatecards.************************************************************/voidread_cards(void){charch,rank_ch,suit_ch;inti,rank,suit;boolbad_card,duplicate_card;intcards_read=0;while(cards_read<NUM_CARDS){bad_card=false;printf("Enteracard:");rank_ch=getchar();switch(rank_ch){case'0':case'2':case'3':exit(EXIT_SUCCESS);rank=0;break;rank=1;break;case'4':rank=2;break;case'5':rank=3;break;case'6':rank=4;break;case'7':rank=5;break;case'8':rank=6;break;case'9':rank=7;break;case't':case'T':rank=8;break;case'j':case'J':rank=9;break;case'q':case'Q':rank=10;break;case'k':case'K':rank=11;break;case'a':case'A':rank=12;break;default:bad_card=true;}suit_ch=getchar();switch(suit_ch){case'c':case'C':suit=0;break;case'd':case'D':suit=1;break;case'h':case'H':suit=2;break;case's':case'S':suit=3;break;default:bad_card=true;}while((ch=getchar())!='\n')if(ch!='')bad_card=true;if(bad_card){printf("Badcard;ignored.\n");continue;}duplicate_card=false;for(i=0;i<cards_read;i++)if(hand[i][RANK]==rank&&hand[i][SUIT]==suit){printf("Duplicatecard;ignored.\n");duplicate_card=true;break;}if(!duplicate_card){hand[cards_read][RANK]=rank;hand[cards_read][SUIT]=suit;cards_read++;}}}/***********************************************************analyze_hand:Determineswhetherthehandcontainsa**straight,aflush,four-of-a-kind,**and/orthree-of-a-kind;determinesthe***numberofpairs;storestheresultsinto*theexternalvariablesstraight,flush,**four,three,andpairs.***********************************************************/voidanalyze_hand(void){intrank,suit,card,pass,run;straight=true;flush=true;four=false;three=false;pairs=0;/*sortcardsbyrank*/for(pass=1;pass<NUM_CARDS;pass++)for(card=0;card<NUM_CARDS-pass;card++){rank=hand[card][RANK];suit=hand[card][SUIT];if(hand[card+1][RANK]<rank){hand[card][RANK]=hand[card+1][RANK];hand[card][SUIT]=hand[card+1][SUIT];hand[card+1][RANK]=rank;hand[card+1][SUIT]=suit;}}/*checkforflush*/suit=hand[0][SUIT];for(card=1;card<NUM_CARDS;card++)if(hand[card][SUIT]!=suit)flush=false;/*checkforstraight*/for(card=0;card<NUM_CARDS-1;card++)if(hand[card][RANK]+1!=hand[card+1][RANK])straight=false;/*checkfor4-of-a-kind,3-of-a-kind,andpairsbylookingfor"runs"ofcardswithidenticalranks*/card=0;while(card<NUM_CARDS){rank=hand[card][RANK];run=0;do{run++;card++;}while(card<NUM_CARDS&&hand[card][RANK]==rank);switch(run){case2:pairs++;break;case3:three=true;break;case4:four=true;break;}}}/***********************************************************print_result:Printstheclassificationofthehand,**basedonthevaluesoftheexternal**variablesstraight,flush,four,three,**andpairs.***********************************************************/voidprint_result(void){if(straight&&flush)printf("Straightflush");elseif(four)printf("Fourofakind");_elseif(three&&pairs==1)elseif(flush)elseif(straight)elseif(three)elseif(pairs==2)elseif(pairs==1)printf("Fullhouse");printf("Flush");printf("Straight");printf("Threeofakind");printf("Twopairs");printf("Pair");printf("Highcard");printf("\n\n");}5.[was#6]#include<>/*C99only*/#include<>#include<>#defineNUMRANKS13#defineNUM_SUITS4#defineNUM_CARDS5/*externalvariables*/intnum_in_rank[NUM_RANKS];intnum_in_suit[NUM_SUITS];boolstraight,flush,four,three;intpairs;/*canbe0,1,or2*//*prototypes*/voidread_cards(void);voidanalyze_hand(void);voidprint_result(void);/***********************************************************main:Callsread_cards,analyze_hand,andprint_result**repeatedly.***********************************************************/intmain(void){for(;;){read_cards();analyze_hand();print_result();}}/***********************************************************read_cards:Readsthecardsintotheexternal**variablesnum_in_rankandnum_in_suit;**checksforbadcardsandduplicatecards.***********************************************************/voidread_cards(void){boolcard_exists[NUM_RANKS][NUM_SUITS];charch,rank_ch,suit_ch;intrank,suit;boolbad_card;intcards_read=0;for(rank=0;rank<NUM_RANKS;rank++){num_in_rank[rank]=0;for(suit=0;suit<NUM_SUITS;suit++)card_exists[rank][suit]=false;}for(suit=0;suit<NUM_SUITS;suit++)num_in_suit[suit]=0;while(cards_read<NUM_CARDS){bad_card=false;printf("Enteracard:");rank_ch=getchar();switch(rank_ch){case'0':exit(EXIT_SUCCESS);case'2':rank=0;break;case'3':rank=1;break;case'4':rank=2;break;case'5':rank=3;break;case'6':rank=4;break;case'7':rank=5;break;case'8':rank=6;break;case'9':rank=7;break;case't':case'T':rank=8;break;case'j':case'J':rank=9;break;case'q':case'Q':rank=10;break;case'k':case'K':rank=11;break;case'a':case'A':rank=12;break;default:bad_card=true;}suit_ch=getchar();switch(suit_ch){case'c':case'C':suit=0;break;case'd':case'D':suit=1;break;case'h':case'H':suit=2;break;case's':case'S':suit=3;break;default:bad_card=true;}while((ch=getchar())!='\n')if(ch!='')bad_card=true;if(bad_card)printf("Badcard;ignored.\n");elseif(card_exists[rank][suit])printf("Duplicatecard;ignored.\n");else{num_in_rank[rank]++;num_in_suit[suit]++;card_exists[rank][suit]=true;cards_read++;}}}/***********************************************************analyze_hand:Determineswhetherthehandcontainsa***straight,aflush,four-of-a-kind,and/orthree-of-a-kind;determinesthe***numberofpairs;storestheresultsinto**theexternalvariablesstraight,flush,**four,three,andpairs.***********************************************************/voidanalyze_hand(void){intnum_consec=0;intrank,suit;straight=false;flush=false;four=false;three=false;pairs=0;/*checkforflush*/for(suit=0;suit<NUM_SUITS;suit++)if(num_in_suit[suit]==NUM_CARDS)flush=true;/*checkforstraight*/rank=0;while(num_in_rank[rank]==0)rank++;for(;rank<NUM_RANKS&&num_in_rank[rank]>0;rank++)num_consec++;if(num_consec==NUM_CARDS){straight=true;return;}/*checkforace-lowstraight*/if(num_consec==NUM_CARDS-1&&num_in_rank[0]>0&&num_in_rank[NUM_RANKS-1]>0){straight=true;return;}/*checkfor4-of-a-kind,3-of-a-kind,andpairs*/for(rank=0;rank<NUM_RANKS;rank++){if(num_in_rank[rank]==4)four=true;if(num_in_rank[rank]==3)three=true;if(num_in_rank[rank]==2)pairs++;}}/***********************************************************print_result:Printstheclassificationofthehand,**basedonthevaluesoftheexternal**variablesstraight,flush,four,three,**andpairs.***********************************************************/voidprint_result(void){if(straight&&flush)printf("Straightflush");elseif(four)elseif(three&&pairs==1)elseif(flush)elseif(straight)elseif(three)elseif(pairs==2)elseif(pairs==1)printf("Fourofakind");printf("Fullhouse");printf("Flush");printf("Straight");printf("Threeofakind");printf("Twopairs");printf("Pair");printf("Highcard");printf("\n\n");}Chapter11AnswerstoSelectedExercisestoanintegerandiisaninteger.(b)isillegalbecause*pisanintegerand&iisapointertoaninteger.(c)isillegalbecause&pisapointertoaninteger.4.[was#4;modified]voidswap(int*p,int*q){inttemp;temp=*p;*p=*q;*q=temp;}6.[was#6]voidfind_two_largest(inta[],intn,int*largest,int*second_largest){inti;if(a[0]>a[1]){*largest=a[0];*second_largest=a[1];}else{*largest=a[1];*second_largest=a[0];}for(i=2;i<n;i++)if(a[i]>*largest){*second_largest=*largest;*largest=a[i];}elseif(a[i]>*second_largest)*second_largest=a[i];}Chapter12AnswerstoSelectedExercisesalegalstatementthathasthedesiredeffect:middle=low+(high-low)/2;ybeaddedtolow.4.[was#6]int*top_ptr;voidmake_empty(void){top_ptr=&contents[0];}boolis_empty(void){returntop_ptr==&contents[0];}boolis_full(void){returntop_ptr==&contents[STACK_SIZE];}6.[was#10;modified]intsum_array(constinta[],intn){int*p,sum;sum=0;for(p=a;p<a+n;p++)sum+=*p;returnsum;}13.[was#12;modified]#defineN10doubleident[N][N],*p;intnum_zeros=N;for(p=&ident[0][0];p<=&ident[N-1][N-1];p++)if(num_zeros==N){*p=;num_zeros=0;}else{*p=;num_zeros++;}15.[was#14]int*p;for(p=temperatures[i];p<temperatures[i]+24;p++)printf("%d",*p);AnswerstoSelectedProgrammingProjects1.[was#4]#include<>#defineMSG_LEN80/*maximumlengthofmessage*/intmain(void){charmsg[MSG_LEN];inti;printf("Enteramessage:");for(i=0;i<MSG_LEN;i++){msg[i]=getchar();if(msg[i]=='\n')break;}printf("Reversalis:");putchar(msg[i]);putchar('\n');return0;}#include<>#defineMSG_LEN80/*maximumlengthofmessage*/intmain(void){charmsg[MSG_LEN],*p;printf("Enteramessage:");for(p=&msg[0];p<&msg[MSG_LEN];p++){*p=getchar();if(*p=='\n')break;}printf("Reversalis:");putchar(*p);putchar('\n');return0;}3.[was#8]#include<>#defineMSG_LEN80/*maximumlengthofmessage*/intmain(void){charmsg[MSG_LEN],*p;printf("Enteramessage:");for(p=msg;p<msg+MSG_LEN;p++){*p=getchar();if(*p=='\n')break;}printf("Reversalis:");putchar(*p);putchar('\n');return0;}Chapter13AnswerstoSelectedExercises2.[was#2](a)Illegal;pisnotacharacter.(b)Legal;outputisa.(c)Legal;outputisabc.(d)Illegal;*pisnotapointer.4.[was#4]intread_line(charstr[],intn){intch,i=0;while((ch=getchar())!='\n')if(i==0&&isspace(ch));/*ignore*/elseif(i<n)str[i++]=ch;str[i]='\0';returni;}intread_line(charstr[],intn){intch,i=0;while(!isspace(ch=getchar()))if(i<n)str[i++]=ch;str[i]='\0';returni;}intread_line(charstr[],intn){intch,i=0;do{ch=getchar();if(i<n)str[i++]=ch;}while(ch!='\n');str[i]='\0';returni;}intread_line(charstr[],intn){intch,i;for(i=0;i<n;i++){ch=getchar();if(ch=='\n')break;str[i]=ch;}str[i]='\0';returni;}6.[was#6]voidcensor(chars[]){inti;for(i=0;s[i]!='\0';i++)if(s[i]=='f'&&s[i+1]=='o'&&s[i+2]=='o')s[i]=s[i+1]=s[i+2]='x';}testingcharactersthatfollowthenullcharacter.8.[was#10]tired-or-wired10.[was#12]Thevalueofqisundefined,sothecallofstrcpyattempts2inChapter17discusseshowtowritethisfunctioncorrectly.15.[was#8]characterinsthatisnotalsoint.16.[was#16]intcount_spaces(constchar*s){intcount=0;while(*s)if(*s++=='')count++;returncount;}AnswerstoSelectedProgrammingProjects1.[was#14]#include<>#include<>#defineWORD_LEN20voidread_line(charstr[],intn);intmain(void){charsmallest_word[WORD_LEN+1],largest_word[WORD_LEN+1],current_word[WORD_LEN+1];printf("Enterword:");read_line(current_word,WORD_LEN);strcpy(smallest_word,strcpy(largest_word,current_word));while(strlen(current_word)!=4){printf("Enterword:");read_line(current_word,WORD_LEN);if(strcmp(current_word,smallest_word)<0)strcpy(smallest_word,current_word);if(strcmp(current_word,largest_word)>0)strcpy(largest_word,curre
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024鋼筋班組承包合同范文
- 二零二五年度辦公樓租賃期滿續(xù)約合同3篇
- 2024版服裝店聘用合同范本
- 二零二五年度房地產(chǎn)營(yíng)銷代理合同2篇
- 2024正規(guī)企業(yè)生產(chǎn)線設(shè)備整體轉(zhuǎn)讓合同模板3篇
- 2024汽車融資租租賃合同
- 二零二五年度XX北京房屋買賣合同稅費(fèi)減免協(xié)商范本2篇
- 二零二五年度數(shù)據(jù)分析工程師服務(wù)合同模板3篇
- 二零二五年度房產(chǎn)贈(zèng)與與裝修工程配套合同3篇
- 二零二五年度周轉(zhuǎn)材料租賃與物流配送服務(wù)合同3篇
- 胸腔積液-課件
- 公司設(shè)備轉(zhuǎn)讓合同協(xié)議書
- 2023年全國(guó)統(tǒng)一建筑工程預(yù)算工程量計(jì)算規(guī)則完整版
- cn.7a一種醬香型大曲酒固態(tài)發(fā)酵的生態(tài)控制方法
- TLFSA 003-2020 危害分析與關(guān)鍵控制點(diǎn)(HACCP)體系調(diào)味面制品生產(chǎn)企業(yè)要求
- LY/T 2244.3-2014自然保護(hù)區(qū)保護(hù)成效評(píng)估技術(shù)導(dǎo)則第3部分:景觀保護(hù)
- GB/T 8491-2009高硅耐蝕鑄鐵件
- 供水安全與搶修
- DB31 595-2021 冷庫(kù)單位產(chǎn)品能源消耗指標(biāo)
- 第三章果蔬采后生理課件
- 【英語手寫體】26英文字母手寫體描紅書寫字帖
評(píng)論
0/150
提交評(píng)論