118 CHAPTER7. CONTROLSTRUCTURES,PART 1
- Ababysittercharges$2.50anhouruntil9:00PMwhentheratedropsto$1.75anhour(thechildren
areinbed). Writea programthatacceptsa startingtimeandendingtimeinhoursandminutesand
calculatesthetotalbabysittingbill.Youmayassumethatthestartingandendingtimesareina single
24 hourperiod.Partialhoursshouldbeappropriatelyprorated.
- A personis eligibletobea USsenatorif they areat least 30 yearsoldandhave beena UScitizenfor
at least9 years.To bea USrepresentative thesenumbersare 25 and7,respectively. Writea program
thatacceptsa person’s ageandyearsofcitizenshipasinputandoutputstheireligibilityfortheSenate
andHouse.
- AformulaforcomputingEasterintheyears1982–2048,inclusive,isasfollows:leta year%19,
b year%4,c year%7,d
19 a 24 %30,e
2 b 4 c 6 d 5 %7.ThedateofEasteris March
22 d e(whichcouldbeinApril).Writea programthatinputsa year, verifiesthatit is intheproper
rangeandthenprintsoutthedateofEasterthatyear.
- TheformulaforEasterinthepreviousproblemworksforeveryyearintherange1900–2099except
for1954,1981, 2049 and2076.Forthese4 yearsit producesa datethatis oneweektoolate.Modify
theabove programtoworkfortheentirerange1900–2099.
- A yearis a leapyearif it is divisibleby4, unlessit is a centuryyearthatis notdivisibleby400.(1800
and 1900 arenotleapyearswhile 1600 and 2000 are.) Writea programthatcalculateswhethera year
is a leapyear.
- Writea programthatacceptsa dateintheformmonth/day/yearandoutputswhetherornotthedateis
valid.Forexample5/24/1962is valid,but9/31/2000is not.(Septemberhasonly 30 days.)
- Thedaysoftheyearareoftennumberedfrom1 to through 365 (or366).Thisnumbercanbecomputed
inthreestepsusingintarithmetic:
(a)dayNum 31
month 1 day
(b)if themonthis afterFebruarysubtract
4 month 23
10
(c)if it’s a leapyearandafterFebruary29,add 1
Writea programthatacceptsa dateasmonth/day/year, verifiesthatit isa validdate(seeprevious
problem)andthencalculatesthecorrespondingdaynumber.
- Take a favoriteprogrammingproblemfroma previouschapterandadddecisionsand/orexception
handlingasrequiredtomake it trulyrobust(willnotcrashonany inputs).
- ArcheryScorer. Writea programthatdrawsanarcherytarget(seeexercisesfromChapter5)and
allowstheusertoclick5 timestorepresentarrowsshotat thetarget.Using5-bandscoring,a bulls-eye
(yellow)is worth9 pointsandeachsuccessive ringis worth2 fewerpointsdownto1 forwhite.The
programshouldoutputa scoreforeachclickandkeeptrackofa runningsumfortheentireseries.