c语言编程全部练习.docx
《c语言编程全部练习.docx》由会员分享,可在线阅读,更多相关《c语言编程全部练习.docx(8页珍藏版)》请在冰豆网上搜索。
![c语言编程全部练习.docx](https://file1.bdocx.com/fileroot1/2023-2/4/2891c696-8f2a-48da-a416-bed1998014d2/2891c696-8f2a-48da-a416-bed1998014d21.gif)
c语言编程全部练习
命名文件和名相同压缩包文件无所谓3414_E01.zip
在下周二上课前交
Exercise1:
ProgrammingEnvironmentandBasicInput/Output
1.Writeaprogramthatprints“Thisismyfirstprogram!
”onthescreen.
(a)Savethisprogramontoyourowndiskwiththenameofe2-1a;
(b)RunthisprogramwithoutopeningTurboC;
(c)Modifythisprogramtoprint“Thisismysecondprogram!
”,thensaveitase2-1b.Pleasedonotoverwritethefirstprogram.
2.Writeaprogramthatprintsthenumber1to4onthesameline.Writetheprogramusingthefollowingmethods:
(a)Usingfour“printf”statements.
(b)Usingone“printf”statementwithnoconversionspecifier(i.e.no‘%’).
(c)Usingone“printf”statementwithfourconversionspecifiers
3.(a)Writeaprogramthatcalculatesanddisplaysthenumberofminutesin15days.
(b)Writeaprogramthatcalculatesanddisplayshowmanyhours180minutesequalto.
(c)(Optional)Howabout174minutes?
Exercise2:
DataTypesandArithmeticOperations
1.Youpurchasealaptopcomputerfor$889.Thesalestaxrateis6percent.WriteandexecuteaCprogramthatcalculatesanddisplaysthetotalpurchaseprice(netprice+salestax).
2.Writeaprogramthatreadsintheradiusofacircleandprintsthecircle’sdiameter,circumferenceandarea.Usethevalue3.14159for“”.
3.Writeaprogramthatreadsintwonumbers:
anaccountbalanceandanannualinterestrateexpressedasapercentage.Yourprogramshouldthendisplaythenewbalanceafterayear.Therearenodepositsorwithdraws–justtheinterestpayment.Yourprogramshouldbeabletoreproducethefollowingsamplerun:
Interestcalculationprogram.
Startingbalance?
6000
Annualinterestratepercentage?
4.25
Balanceafteroneyear:
6255.00
Exercise3:
Selectionstructure
1.WriteaCprogramthatacceptsastudent’snumericalgrade,convertsthenumericalgradetoPassed(gradeisbetween60-100),Failed(gradeisbetween0-59),orError(gradeislessthan0orgreaterthan100).
2.Writeaprogramthataskstheusertoenteranintegernumber,thentellstheuserwhetheritisanoddorevennumber.
3.Writeaprogramthatreadsinthreeintegersandthendeterminesandprintsthelargestinthegroup.
Exercise4:
‘switch’statementandsimple“while”repetitionstatement
1.Writeaprogramthatreadsthreeintegersanabbreviateddate(forexample:
261294)andthatwillprintthedateinfull;forexample:
26thDecember1994.Thedayshouldbefollowedbyanappropriatesuffix,‘st’,‘nd’,‘rd’or‘th’.Useatleastoneswitchstatement.
2.WriteaCprogramthatusesawhilelooptocalculateandprintthesumoftheevenintegersfrom2to30.
3.Alargechemicalcompanypaysitssalesstaffonacommissionbasis.Theyreceive£200perweekplus9%oftheirgrosssalesforthatweek.Forexample,someonewhosells£5000ofchemicalsinoneweekwillearn£200plus9%of£5000,atotalof£650.DevelopaCprogramthatwillinputeachsalesperson’ssalesforthepreviousweek,andprintouttheirsalary.Processoneperson’sfiguresatatime.
Entersalesinpounds(-1toend):
5000.00
Salaryis:
650.00
Entersalesinpounds(-1toend):
00.00
Salaryis:
200.00
Entersalesinpounds(-1toend):
1088.89
Salaryis:
298.00
Entersalesinpounds(-1toend):
-1
Optional:
两重的while
4.Amailordercompanysellsfivedifferentproductswhoseretailpricesareshowninthefollowingtable:
ProductNumberRetailPrice(inpounds)
12.98
24.50
39.98
44.49
56.87
WriteaCprogramthatreadsinaseriesofpairsofnumbersasfollows:
(1).Productnumber
(2).Quantitysoldforoneday
Yourprogramshoulduseaswitchstatementtohelpdeterminetheretailpriceforeachproduct,andshoulduseasentinel-controlledlooptocalculatethetotalretailvalueofallproductssoldinagivenweek(7days).
Exercise5:
‘for’and‘do…while”repetitionstatements
1.Writeaprogramwhichusesado/whilelooptoprintoutthefirst10powersof2otherthan0(ie.itprintsoutthevaluesof21,22,...,210).Useaforlooptodothesame.
2.Theconstantcanbecalculatedbytheinfiniteseries:
=4-4/3+4/5-4/7+4/9-4/11+....
WriteaCprogramthatusesado/whilelooptocalculateusingtheseries.Theprogramshouldasktheuserhowmanytermsintheseriesshouldbeused.Thusiftheuserenters‘3’,thentheprogramshouldcalculateasbeing4-4/3+4/5.
Nestedrepetition
3.Writeaprogramthatprintsthefollowingdiamondshape.Youmayuseprintfstatementsthatprinteitherasingleasterisk(*)orasingleblank.Maximizeyouruseofrepetition(withnestedforstatements)andminimizethenumberofprintfstatements.
*
***
*****
*******
*********
*******
*****
***
*
4.Writeaprogramtoprintatableasfollows:
1*1=1
2*1=22*2=4
3*1=33*2=63*3=9
….
9*1=99*2=189*3=279*4=369*5=459*6=549*7=639*8=729*9=81
Exercise6:
SimpleFunctions
1.WriteaCprogramthatreadsseveralnumbersandusesthefunctionround_to_nearesttoroundeachofthesenumberstothenearestinteger.Theprogramshouldprintboththeoriginalnumberandtheroundednumber.
2.Writeaprogramthatreadsthreepairsofnumbersandaddsthelargerofthefirstpair,thelargerofthesecondpairandthelargerofthethirdpair.Useafunctiontoreturnthelargerofeachpair.
3.Acarparkchargesa£2.00minimumfeetoparkforupto3hours,andanadditional£0.50foreachhourorparthourinexcessofthreehours.Themaximumchargeforanygiven24-hourperiodis£10.00.Assumethatnocarparksformorethan24hoursatatime.
WriteaCprogramthatwillcalculateandprinttheparkingchargesforeachof3customerswhoparkedtheircarinthecarparkyesterday.Theprogramshouldacceptasinputthenumberofhoursthateachcustomerhasparked,andoutputtheresultsinaneattabularform,alongwiththetotalreceiptsfromthethreecustomers:
CarHoursCharge
11.52.00
24.02.50
324.010.00
TOTAL29.514.50
Theprogramshouldusethefunctioncalculate_chargestodeterminethechargeforeachcustomer.
Exercise7:
MoreFunctions
1.Writeaprogramthatusessentinel-controlledrepetitiontotakeanintegerasinput,andpassesittoafunctionevenwhichusesthemodulusoperatortodetermineiftheintegeriseven.Thefunctionevenshouldreturn1iftheintegeriseven,and0ifitisnot.
Theprogramshouldtakethevaluereturnedbythefunctionevenanduseittoprintoutamessageannouncingwhetherornottheintegerwaseven.
2.WriteaCprogramthatusesthefunctionintegerPower1(base,exponent)toreturnthevalueof:
baseexponent
sothat,forexample,integerPower1(3,4)givesthevalue3*3*3*3.Assumethatexponentisapositive,non-zerointeger,andbaseisaninteger.Thefunctionshoulduseaforloop,andmakenocallstoanymathlibraryfunctions.
3.WriteaCprogramthatusestherecursivefunctionintegerPower2(base,exponent)toreturnthevalueof:
baseexponent
sothat,forexample,integerPower2(3,4)givesthevalue3*3*3*3.Assumethatexponentisapositive,non-zerointeger,andbaseisaninteger.Thefunctionshouldmakenocallstoanymathlibraryfunctions.
(Hint:
therecursivestepwillusetherelationship:
baseexponent=base.baseexponent-1
andthebasecasewillbewhenexponentis1since:
base1=base.)
Exercise8:
Arrays
1.Writeaprogramthatreadstennumberssuppliedbytheuserintoasinglesubscriptedarray,andthenprintsouttheaverageofthem.
2.Writeaprogramthatreadstennumberssuppliedbytheuserintoa2by5array,andthenprintsoutthemaximumandminimumvaluesheldin:
(a)eachrow(2rows)
(b)thewholearray
3.Useasingle-subscriptedarraytosolvethefollowingproblem.Readin20numbers,eachofwhichisbetween10and100,inclusive.Aseachnumberisread,printitonlyifitisnotaduplicateofanumberalreadyread.Prepareforthe“worstcase”inwhichall20numbersaredifferent.Usethesmallestpossiblearraytosolvethisproblem.
Exercise9:
MoreArrays
1.Writeaprogramthatenters5namesoftownsandtheirrespectivedistance(aninteger)fromLondoninmiles.Theprogramwillprintofthenamesofthetownsthatarelessthan100milesfromLondon.Usearraysandcharacterstringstoimplementyourprogram.
2.Writeaprogramthatpromptstheusertotypeinfourcharacterstrings,placestheseinanarrayofstrings,andthenprintsout:
(e.g.IamPeterPan)
(i)Thefourstringsinreverseorder.(e.g.PanPeteramI)
(ii)Thefourstringsintheoriginalorder,butwitheachstringbackwards.(e.g.ImaretePnaP)
(iii)Thefourstringsinreverseorderwitheachstringbackwards.(e.g.naPretePmaI)
Exercise10:
Pointers
1.Writeaprogramthatreads5integersintoanarray,andthenusesfourdifferentmethodsofaccessingthemembersofanarraytoprintthemoutinreverseorder.
2.Writeaprogramthatreads8floatsintoanarrayandthenprintsoutthesecond,fourth,sixthandeighthmembersofthearray,andthesumofthefirst,third,fifthandseventh,usingpointerstoaccessthemembersofthearray.
3.WriteaprogramthatuseaSINGLEFUNCTION(用一个函数)tofindandreturnsimultaneouslyboththelowestandhighestvaluesinanarrayoftypeint.Supposethesizeofthearrayis6.
Exercise11:
Files
1.Writeaprogramthatentersthenameandtheannualsalaryof5employeesfromthekeyboard,calculatesthemonthlysalaryofeachemployeeassumingthattaxisdeductedataflatrateof20%.Theprogramprintsthename,theannualsalaryandthemonthlypayment(net)ofeachemployeeinafile.Theprogramaddsanemployee’snumbertoeachemployeebeforeprin