0% found this document useful (0 votes)
111 views

Tutorial 4 - C Functions

This document is a tutorial on C programming functions. It provides examples of function scope, errors in function code, and exercises to write functions that perform tasks like calculating the area of an ellipse, distance between points, baggage charges, and a guessing game. It also includes an ASCII character set table.

Uploaded by

lightsoul91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views

Tutorial 4 - C Functions

This document is a tutorial on C programming functions. It provides examples of function scope, errors in function code, and exercises to write functions that perform tasks like calculating the area of an ellipse, distance between points, baggage charges, and a guessing game. It also includes an ASCII character set table.

Uploaded by

lightsoul91
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

SIE1003CProgramming

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).

You might also like