Professional Documents
Culture Documents
Tutorial4
SIE1003CProgramming
Tutorial4:CFunctions
1. For the following program, state the scope (either function scope, file scope, block scope or
functionprototypescope)ofeachofthefollowingelements.
(a) Thevariableainmain.
__________
(b) Thefunctionsquare.
__________
(c) Thevariablebinsquare.
__________
(d) Thefunctionmain.
__________
(e) Thefunctionprototypeforsquare.
__________
(f) The identifier b in the function prototype for
square.
__________
2. Findtheerror(s)ineachofthefollowingprogramsegmentsandexplainhowtheerrorcanbe
corrected.
(a)
(b)
(c)
SIE1003CProgramming
Tutorial4
(d)
(e)
(f)
(g)
(h)
3. Write(usingpenandpaper)aprogramthatinputstworadiuses,r1andr2,oftypedoubleand
passesthemtoafunctionareaOfEllipsethatcalculatestheareaofanellipse(*r1*r2)and
returnstheareaoftheellipseoftypedouble.Printtheareaoftheellipseinonedecimalplace.
Defineas3.14159.
SIE1003CProgramming
Tutorial4
4. Writeafunctionthattakesthedistancetravelledbyacarastwointegerarguments(forkilometres
andmeters)andreturnsthetotaldistancetravelledinmeters.Usethisfunctiontocalculatethe
distancetravelled(inmeters)bythreecars.Displaythetotaldistanceforeachcarandforallthree
cars.
5. Writeaprogramthatpromptstheusertoentertwopoints(x1,y1)and(x2,y2),anddisplays(intwo
decimalplaces)thedistancebetweenthetwopointsthatiscomputedbyafunctiondistance.All
numbersandreturnvaluesshouldbeoftypedouble.Displaytheresultintwodecimalplaces.
6. A baggage counter charges a $0.50 minimum fee to deposit bag for up to three hours and an
additional$0.25perhourforeachhourorpartthereofoverthreehours.Writeaprogramthat
calculatesandprints(inatabularformat)thebaggagecounterchargesforeachoffivecustomers
and the total of all five charges. The program should use the function calculateCharges to
determinethechargeforeachcustomer.Theoutputsshouldappearinthefollowingformat:
Customers
1
TOTAL
Hours
1.00
1.50
3.00
3.40
5.60
14.50
Charge($)
0.50
0.50
0.50
0.75
1.25
3.50
7. Write a recursive function power (base, exponent) that when invoked returns baseexponent. The
recursive step would use the relationship baseexponent = base * baseexponent-1 and the terminating
conditionoccurswhenexponentisequalto1becausebase1=base.Forexample,power(3,4)=
3*3*3*3.Assumethatexponentisanintegergreaterthanorequalto1.Bothbaseandexponent
areunsignedlongintegers,whereasthereturnpowervalueisanunsignedlonglonginteger.
8. WriteafunctionguessGametoplaythegameofguessthealphabet.Thefunctionfirstrandomly
selectsaletterintherangeAtoZ,andthenpromptstheplayertoguesstheletter.Theplayer
types a first guess, and the function responds with one of the following: Too low! Please try
again,Toohigh!PleasetryagainorGreat!Yourguessisright.Thefunctionshouldloopuntil
the player gets the letter right, and should prompt the player to playing the game again. The
programonlyterminateswhentheplayerdoesnotwanttoplaythegameagain.
SIE1003CProgramming
Tutorial4
ASCIICharacterSetThedigitsattheleftofthetablearetheleftdigitsofthedecimalequivalent(0
127)ofthecharactercode,andthedigitsatthetopofthetablearetherightdigitsofthecharacter
code.Forexample,thecharactercodeforFis70,andthecharactercodefor&is38.Reference
fromC:HowtoProgram(PaulDeitelandHarveyDeitel,2013).