《C语言编程练习》PPT课件.ppt_第1页
《C语言编程练习》PPT课件.ppt_第2页
《C语言编程练习》PPT课件.ppt_第3页
《C语言编程练习》PPT课件.ppt_第4页
《C语言编程练习》PPT课件.ppt_第5页
已阅读5页,还剩100页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

12/5/2019,ProgrammingExercises,NorthChinaElectricPowerUniversity,12/5/2019,Chapter4ManagingInputandOutputOperations4.2,4.2Writeaprogramtoreadthevaluesofxandyandprinttheresultsofthefollowingexpressionsinoneline:(a)(b)(c),12/5/2019,Chapter4ManagingInputandOutputOperations4.2,main()floatx,y;printf(Pleaseinputthevalueofxandy:n);printf(x=);scanf(%f,12/5/2019,Chapter4ManagingInputandOutputOperations4.3,4.3Writeaprogramtoreadthefollowingnumbers,roundthemofftothenearestintegersandprintouttheresultsinintegerform:35.750.21-23.73-46.45,12/5/2019,Chapter4ManagingInputandOutputOperations4.3,main()floatx;printf(Pleaseinputthenumber:);scanf(%f,12/5/2019,Chapter4ManagingInputandOutputOperations4.5,4.5Writeaninteractiveprogramtodemonstratetheprocessofmultiplication.Theprogramshouldasktheusertoentertwotwo-digitintegersandprinttheproductofintegersasshownbelow.4537745is315345is135Addthem1665,12/5/2019,Chapter4ManagingInputandOutputOperations4.5,main()intx,y,m,n;printf(Pleaseinput2two-digitintegers:);scanf(%d%d,12/5/2019,Chapter5DecisionMakingandBranching5.1,5.1WriteaprogramtodeterminewhetheragivennumberisoddofevenandprintthemessageNUMBERISEVENorNUMBERISODD(a)withoutusingelseoption,and(b)withelseoption.,12/5/2019,Chapter5DecisionMakingandBranching5.1,main()/*(a)*/intn;printf(Pleaseinputaninteger:);scanf(%d,12/5/2019,Chapter5DecisionMakingandBranching5.1,main()/*(b)*/intn;printf(Pleaseinputaninteger:);scanf(%d,12/5/2019,Chapter5DecisionMakingandBranching5.3,5.3Asetoftwolinerequationswithtwounknownx1andx2isgivenbelow:ax1+bx2=mcx1+dx2=nThesethasauniquesolutionprovidedthedenominatorad-cbisnotequaltozero.Writeaprogramthatwillreadthevaluesofconstantsa,b,c,d,mandnandcomputethevaluesofx1andx2.Anappropriatemessageshouldbeprintedifad-cb=0.,12/5/2019,Chapter5DecisionMakingandBranching5.3,main()floata,b,c,d,m,n;printf(Aboutasetoftwolinerequations:n);printf(tax1+bx2=mntcx1+dx2=nn);printf(Pleaseinputthevalueofa,b,c,d,mandn:n);printf(a=);scanf(%f,12/5/2019,Chapter5DecisionMakingandBranching5.5,5.5Admissiontoaprofessionalcourseissubjecttothefollowingconditions:(a)MarksinMathematics=60(b)MarksinPhysics=50(c)MarksinChemistry=40(d)Totalinallthreesubjects=200orTotalinMathematicsandPhysics=150Giventhemarksinthethreesubjects,writeaprogramtoprocesstheapplicationstolisttheeligiblecandidates.,12/5/2019,Chapter5DecisionMakingandBranching5.5,main()floatmath,phy,chem;printf(Pleaseinputscoresofthe3subject:);printf(Mathematics:);scanf(%f,12/5/2019,Chapter5DecisionMakingandBranching5.8,5.8Aclothshowroomhasannouncedthefollowingseasonaldiscountsonpurchaseofitems:Writeaprogramusingswitchandifstatementstocomputethenetamounttobepaidbyacustomer.,12/5/2019,Chapter5DecisionMakingandBranching5.8,main()floatprice,discount;charcategory;printf(Category(m/h):);scanf(%c,12/5/2019,Chapter5DecisionMakingandBranching5.9,5.9Writeaprogramthatwillreadthevalueofxandevaluatethefollowingusing(a)nestedifstatements(b)elseifstatements,and(c)conditionaloperator?:,12/5/2019,Chapter5DecisionMakingandBranching5.9,main()/*(a)*/floatx;printf(Pleaseinputthevalueofx:);scanf(%f,12/5/2019,Chapter5DecisionMakingandBranching5.9,main()/*(b)*/floatx;printf(Pleaseinputthevalueofx:);scanf(%f,12/5/2019,Chapter5DecisionMakingandBranching5.9,main()/*(c)*/floatx;printf(Pleaseinputthevalueofx:);scanf(%f,12/5/2019,Chapter5DecisionMakingandBranching5.10,5.10WriteaprogramtocomputetherealrootsofaquadraticequationTherootsaregivenbytheequationsTheprogramshouldrequestforthevaluesoftheconstantsa,bandcandprintthevaluesofx1,andx2.Usethefollowingrules:(a)Nosolution,ifbothaandbarezero(b)Thereisonlyoneroot,ifa=0(x=-c/b)(c)Therearenorealroots,ifb2-4acisnegative(d)Otherwise,therearetworealrootsTestyourprogramwithappropriatedatasothatalllogicalpathsareworkingasperyourdesign.Incorporateappropriateoutputmessages.,12/5/2019,Chapter5DecisionMakingandBranching5.10,#includemain()floata,b,c,f;printf(Pleaseinputthevalueofa,bandcinthequadraticequation:nax*x+bx+c=0n);printf(a=);scanf(%f,12/5/2019,Chapter6DecisionMakingandLooping6.1,6.1Givenanumber,writeaprogramusingwhilelooptoreversethedigitsofthenumber.Forexample,thenumber12345shouldbewrittenas54321.(Hint:Usemodulusoperatortoextractthelastdigitandtheintegerdivisionby10togetthen-1digitnumberfromthendigitnumber.),12/5/2019,Chapter6DecisionMakingandLooping6.1,main()longn;printf(Inputapositiveinteger:);scanf(%ld,/*numberisdecreasedby10times*/,12/5/2019,Chapter6DecisionMakingandLooping6.2,6.2Thefactorialofanintegermistheproductofconsecutiveintegersfrom1tom.Thatis,factorialm=m!=m(m-1)1.Writeaprogramthatcomputesandprintsatableoffactorialsforanygivenm.,12/5/2019,Chapter6DecisionMakingandLooping6.2,main()intm;longn=1;printf(Pleaseinputanumber:);scanf(%d,12/5/2019,Chapter6DecisionMakingandLooping6.3,6.3Writeaprogramtocomputethesumofthedigitsofagivenintegernumber.,12/5/2019,Chapter6DecisionMakingandLooping6.3,main()longn;intsum=0;printf(Inputapositiveinteger:);scanf(%ld,12/5/2019,Chapter6DecisionMakingandLooping6.4,6.4Thenumbersinthesequence1123581321arecalledFibonaccinumbers.Writeaprogramusingado.whilelooptocalculateandprintthefirstmFibonaccinumbers.(Hint:Afterthefirsttwonumbersintheseries,eachnumberisthesumofthetwoprecedingnumbers.),12/5/2019,Chapter6DecisionMakingandLooping6.4,main()longf1=1,f2=1,f=1;inti=2,m;printf(Howmanynumbersdoyouwanttooutput?);scanf(%d,12/5/2019,Chapter6DecisionMakingandLooping6.5,6.5RewritetheprogramoftheExample6.1usingtheforstatement.,12/5/2019,Chapter6DecisionMakingandLooping6.5,main()intcount,n;floatx,y;printf(Enterthevaluesofxandn:);scanf(%f%d,12/5/2019,Chapter6DecisionMakingandLooping6.6,6.6WriteaprogramtoevaluatethefollowinginvestmentequationV=P(1+r)nandprintthetableswhichwouldgivethevalueofVforvariouscombinationofthefollowingvaluesofP,randn.P:1000,2000,3000,10,000r:0.10,0.11,0.12,0.20n:1,2,3,8(Ichangethevalueofnforformat.)(Hint:PistheprincipalamountandVisthevalueofmoneyattheendofnyears.ThisequationcanberecursivelywrittenasV=P(1+r)P=VThatis,thevalueofmoneyattheendoffirstyearbecomestheprincipalamountforthenextyearandsoon.),12/5/2019,Chapter6DecisionMakingandLooping6.6,main()intn;floatp,pp,r,v;charch;for(p=1000;p=10000;p=p+1000)printf(nPrincipalamount:%.0fnrate,p);for(n=1;n=8;n+)if(n=1)printf(%dyear,n);elseprintf(%dyears,n);for(r=0.10;r0.21;r=r+0.01)pp=p;printf(n%.2f:,r);for(n=1;n=8;n+)v=pp*(1+r);pp=v;printf(%9.2f,v);printf(nPressenterkeytocontinue.);scanf(%c,12/5/2019,Chapter6DecisionMakingandLooping6.7,6.7Writeprogramstoprintthefollowingoutputsusingforloops.(a)1(b)*22*333*4444*55555*,12/5/2019,Chapter6DecisionMakingandLooping6.7,main()/*(a)*/inti,j;for(i=1;i=5;i+)for(j=1;j=1;i-)for(j=1;j=5-i;j+)printf();for(j=1;j=i;j+)printf(*);printf(n);,12/5/2019,Chapter6DecisionMakingandLooping6.8,6.8Writeaprogramtoreadtheageof100personsandcountthenumberofpersonsintheagegroup50to60.Useforandcontinuestatements.,12/5/2019,Chapter6DecisionMakingandLooping6.8,main()intage,count=0,i;for(i=1;i60)continue;count+;printf(Thenumberofpersonsintheagegroup50to60is%d.n,count);,12/5/2019,Chapter6DecisionMakingandLooping6.10,6.9Writeaprogramtoprintatableofvaluesofthefunctiony=exp(-x)forxvaryingfrom0.0to10.0instepsof0.10.Thetableshouldappearasfollows:TableforY=EXP(-X)_x0.10.20.30.90.01.02.03.09.0_,12/5/2019,Chapter6DecisionMakingandLooping6.10,#includemain()doublex=0;inti,j;printf(nY=EXP(-X)n);for(i=1;i=80;i+)printf(_);printf(nxt);for(i=1;i=9;i+)printf(t0.%d,i);for(i=0;i=9;i+)printf(nn%d.0,i);for(j=0;j=9;j+)x=i+0.1*j;printf(t%.2le,exp(-x);for(i=1;i=80;i+)printf(_);,12/5/2019,Chapter6DecisionMakingandLooping6.11,6.11Writeaprogramthatwillreadapositiveintegeranddetermineandprintitsbinaryequivalent.(Hint:Thebitsofthebinaryrepresentationofanintegercanbegeneratedbyrepeatedlydividingthenumberandthesuccessivequotientsby2andsavingtheremainder,whichiseither0or1,aftereachdivision.),12/5/2019,Chapter6DecisionMakingandLooping6.11,main()longnumber,n;intm=0,i,j;printf(Pleaseinputapositivenumber:);scanf(%ld,12/5/2019,Chapter6DecisionMakingandLooping6.12,6.12WriteaprogramusingforandifstatementtodisplaythecapitalletterSinagridof15rowsand18columnsshownbelow.,*,12/5/2019,Chapter6DecisionMakingandLooping6.12,main()inti,j;for(i=1;i=4,12/5/2019,Chapter6DecisionMakingandLooping6.13,6.13WriteaprogramtocomputethevalueofEulersnumbere,thatisusedasthebaseofnaturallogarithms.Usethefollowingformula.e=1+1/1!+1/2!+1/3!+1/n!Useasuitableloopconstruct.Theloopmustterminatewhenthedifferencebetweentwosuccessivevaluesofeislessthan0.00001.,12/5/2019,Chapter6DecisionMakingandLooping6.13,#defineERROR0.00001main()floate=1,item;inti,m=1;longn;printf(e=1);dofor(n=1,i=1;i=ERROR);printf(=%fn,e);,e=1+1/1!+1/2!+1/3!+1/4!+1/5!+1/6!+1/7!+1/8!+1/9!=2.718282,12/5/2019,Chapter6DecisionMakingandLooping6.15,6.15Thepresentvalue(popularlyknownasbookvalue)ofanitemisgivenbytherelationship.P=c(1-d)nwherec=originalcostd=rateofdepreciation(peryear)n=numberofyearsP=presentvalueafternyearsIfPisconsideredthescrapvalueattheendofusefullifeoftheitem,writeaprogramtocomputetheusefullifeinyearsgiventheoriginalcost,depreciationrate,andthescrapvalue.Theprogramshouldrequesttheusertoinputthedatainteractively.,12/5/2019,Chapter6DecisionMakingandLooping6.15,main()floatc,d,p;intn,i;printf(Theoriginalcostoftheitemis:);scanf(%f,12/5/2019,Chapter7Arrays7.3,7.3Anelectioniscontestedby5candidates.Thecandidatesarenumbered1to5andthevotingisdonebymarkingthecandidatenumberontheballotpaper.Writeaprogramtoreadtheballotsandcountthevotescastforeachcandidateusinganarrayvariablecount.Incase,anumberreadisoutsidetherange1to5,theballotshouldbeconsideredasaspoiltballotandtheprogramshouldalsocountthenumberofspoiltballots.,12/5/2019,Chapter7Arrays7.3,main()intcount,candidate6=0,spoilt=0;printf(Pleaseinputthevotes(15)andpress0toendthevoting.n);scanf(%d,12/5/2019,Chapter7Arrays7.4,7.4ThefollowingsetofnumbersispopularlyknownasPascalstriangle.Ifwedenoterowsbyiandcolumnsbyj,thenanyelement(excepttheboundaryelements)inthetriangleisgivenbypi,j=pi-1,j-1+pi-1,jWriteaprogramtocalculatetheelementsofthePascaltrianglefor10rowsandprinttheresults.,12/5/2019,Chapter7Arrays7.4,#defineN10main()intpNN=0,i,j;for(i=0;iN;i+)for(j=0;j=i;j+)if(j=0|i=j)pij=1;elsepij=pi-1j-1+pi-1j;for(i=0;iN;i+)for(j=0;j=i;j+)printf(%5d,pij);printf(n);,12/5/2019,Chapter7Arrays7.5,7.5Theannualexaminationresultsof100studentsaretabulatedasfollows:Writeaprogramtoreadthedataanddeterminethefollowing:(a)Totalmarksobtainedbyeachstudent.(b)ThehighestmarksineachsubjectandtheRollNo.ofthestudentwhosecuredit.(c)Thestudentwhoobtainedthehighesttotalmarks.,12/5/2019,Chapter7Arrays7.5,#defineN100main()floatsN4=0,mN=0,max;inti,j,k;printf(Pleaseinputtherollnumberandmarksof%dstudents.n,N);for(i=0;imax)max=sij;k=i;printf(ThehighestmarkofSubject%dis:%.2f.TheRollNo.is:%04.0fn,j,max,sk0);for(i=0,max=m0,k=0;imax)max=mi;k=i;printf(Thestudent%04.0fobtainedthehighesttotalmarks.n,sk0);,12/5/2019,Chapter7Arrays7.6,7.6Givenaretwoone-dimensionalarraysAandBwhicharesortedinascendingorder.WriteaprogramtomergethemintoasinglesortedarrayCthatcontainseveryitemfromarraysAandB,inascendingorder.,12/5/2019,Chapter7Arrays7.6,#defineM5#defineN6main()intaM,bN,cM+N,i,j,k;printf(Input%dnumbersofarrayainascendingorder:n,M);for(i=0;i=M)ck=bj;j+;continue;elseck=ai;i+;continue;printf(Themergednumbersare:);for(k=0;kM+N;k+)printf(%6d,ck);,12/5/2019,Chapter7Arrays7.7,7.7Twomatricesthathavethesamenumberofrowsandcolumnscanbemultipliedtoproduceathirdmatrix.Considerthefollowingtwomatrices.TheproductofAandBisathirdmatrixCofsizenxnwhereeachelementofCisgivenbythefollowingequation.WriteaprogramthatwillreadthevaluesofelementsofAandBandproducetheproductmatrixC.,12/5/2019,Chapter7Arrays7.7,#defineN10main()intaNN,bNN,cNN,i,j,k;printf(PleaseinputmatrixAof%dby%d:n,N,N);for(i=0;iN;i+)for(j=0;jN;j+)scanf(%d,12/5/2019,Chapter7Arrays7.8,7.8Writeaprogramthatfillsafive-by-fivematrixasfollows.Upperlefttrianglewith+1sLowerrighttrianglewith-1sRighttoleftdiagonalwithzerosDisplaythecontentsofthematrixusingnotmorethantwoprintfstatements.,12/5/2019,Chapter7Arrays7.8,#defineN5main()intaNN=0,i,j;for(i=0;iN-1)aij=-1;for(i=0;iN;i+)for(j=0;jN;j+)printf(%3d,aij);printf(n);,12/5/2019,Chapter7Arrays7.9,7.9Selectionsortisbasedonthefollowingidea:Selectingthelargestarrayelementandswappingitwiththelastarrayelementleavesanunsortedlistwhosesizeis1lessthanthesizeoftheoriginallist.Ifwerepeatthisstepagainontheunsortedlistwewillhaveanorderedlistofsize2andanunorderedlistsizen-2.Whenwerepeatthisuntilthesizeoftheunsortedlistbecomesone,theresultwillbeasortedlist.Writeaprogramtoimplementthisalgorithm.,12/5/2019,Chapter7Arrays7.9,#defineN10main()intaN,i,j,t,k;printf(Pleaseinput%dnumbers:n,N);for(i=0;iN;i+)scanf(%d,12/5/2019,Chapter7Arrays7.10,7.10Developaprogramtoimplementthebinarysearchalgorithm.Thistechniquecomparesthesearchkeyvaluewiththevalueoftheelementthatismidwayinasortedlist.Then:(a)Iftheymatch,thesearchisover.(b)Ifthesearchkeyvalueislessthanthemiddlevalue,thenthefirsthalfofthelistcontainsthekeyvalue.(c)Ifthesearchkeyvalueisgreaterthanthemiddlevalue,thenthesecondhalfcontainsthekeyvalue.Repeatthisdivide-and-conquerstrategyuntilwehaveamatch.Ifthelistisreducedtoonenon-matchingelements,thenthelistdoesnotcontainthekeyvalue.Usethesortedlistcreatedinexercise7.9oruseanyothersortedlist.,12/5/2019,Chapter7Arrays7.10,#defineN10main()intaN,i,j,t,k,n,flag=0;printf(Pleaseinput%dnumbers:n,N);for(i=0;i1)m*=i-;ff=pow(-1,n+1)*pow(x,2*n-1)/m;if(fabs(ff)=ERROR)ff+=f(x,n+1);returnff;main()floatx;printf(Pleaseintputthevalueofx:);scanf(%f,12/5/2019,Chapter9User-DefinedFunctions9.5,9.5TheFibonaccinumbersaredefinedrecursivelyasfollows:F1=1F2=1Fn=Fn-1+Fn-2,n2WriteafunctionthatwillgenerateandprintthefirstnFibonaccinumbers.Testthefunctionforn=5,10,and15.,12/5/2019,Chapter9User-DefinedFunctions9.5,voidf(intm)longf1=1,f2=1,f=1;inti=2;printf(%12ld,f);while(i=m)printf(%12ld,f);f=f1+f2;f1=f2;f2=f;if(i%4=0)printf(n);i+;printf(n);main()intm;printf(Howmanynumbersdoyouwanttooutput?);scanf(%d,12/5/2019,Chapter9User-DefinedFunctions9.6,9.6Writeafunctionthatwillroundafloating-pointnumbertoanindicateddecimalplace.Forexamplethenumber17.457wouldyieldthevalue17.46whenitisroundedofftotwodecimalplaces.,12/5/2019,Chapter9User-DefinedFunctions9.6,floatround(floatf)intn;n=(int)(f*1000);if(n%105)f=(n/10)/100.0;elsef=(n/10+1)/100.0;return(f);main()floatf;printf(Whichnumberdoyouwanttoround?);scanf(%f,12/5/2019,Chapter9User-DefinedFunctions9.7,9.7Writeafunctionprimethatreturns1ifitsargumentisaprimenumberandreturnszerootherwise.,12/5/2019,Chapter9User-DefinedFunctions9.7,#includeintprime(intm)inti,k;k=sqrt(m);for(i=2;i=a,12/5/2019,Chapter9User-DefinedFunctions9.9,9.9Developatop-downmodularprogramtoimplementacalculator.Theprogramshouldrequesttheusertoinputtwonumbersanddisplayoneofthefollowingasperthedesireoftheuser:(a)Sumofthenumbers(b)Differenceofthenumbers(c)Productofthenumbers(d)DivisionofthenumbersProvideseparatefunctionsforperformingvarioustaskssuchasreading,calculatinganddisplaying.Calculatingmoduleshouldcallsecondlevelmodulestoperformtheindividualmathematicaloperations.Themainfunctionshouldhaveonlyfunctioncalls.,12/5/2019,Chapter9User-DefinedFunctions9.9,floataddition(floatx,floaty)return(x+y);floatsubtraction(floatx,floaty)return(x-y);floatmultiplication(floatx,floaty)return(x*y);floatdivision(floatx,floaty)return(x/y);voidcalculate()floatx,y;charop;printf(Whatdoyouwanttocalculate?(+,-,*,/)n);scanf(%f%c,12/5/2019,Chapter9User-DefinedFunctions9.10,9.10Developamodularinteractiveprogramusingfunctionsthatreadsthevaluesofthreesidesofatriangleanddisplayseitheritsareaoritsperimeteraspertherequestoftheuser.Giventhethreesidesa,bandc.,12/5/2019,Chapter9User-DefinedFunctions9.10,#includefloata,b,c;voidsides()printf(Pleaseinputthe3sidesofatriangle:);scanf(%f%f%f,12/5/2019,Chapter9User-DefinedFunctions9.11,9.11Writeafunctionthatcanbecalledtofindthelargestelemento

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论