Professional Documents
Culture Documents
Tutorial5
SIE1003CProgramming
Tutorial5:CArrays
1. Findtheerror(s)ineachofthefollowingprogramsegmentsandexplainhowtheerrorcanbe
corrected.
(a)
(b)
(c)
(d)
(e)
(f)
(g)
2. Writestatementstoaccomplisheachofthefollowing.
(a) Inputavalueintotheelementthatispresentinthe5throwand6thcolumnofthedouble
subscriptedintegerarraymatrix.
(b) Initialiseeachofthe4elementsofthedoublesubscriptedintegerarraydoubleArraytothe
productofitssubscripts.Assumetheintegervariablesaandbarecontrolvariables,andthe
symbolicconstantLENGTHhasavalueof2.
(c) MultipletheelementsoftheintegerarrayintArrayof50elements.
(d) Determinethesumanddifferencevaluescontainedin50elementintegerarraym.
SIE1003CProgramming
Tutorial5
3. Whatdoeseachofthefollowingprogramsdo?
(a)
(b)
SIE1003CProgramming
Tutorial5
a) $0120
b) $121240
c) $241360
d) $361480
e) $481600
f) $601720
g) $721840
h) $841960
i) $961andabove
UsefunctionprintCommissiontoprintthenumberofemployeesineachrange.
5. Writeaprogramthatreads10lettersfromAtoZ.Amongthe10letters,storethenonduplicated
onesinanarrayandthenprintthosenonduplicatedletters.
6. Writeaprogramtosimulatetherollingoftwodice.Theprogramshoulduserandtwicetorollthe
firstdieandseconddie,respectively.Thesumofthetwovaluesshouldthenbecalculated.Since
eachdiecanshowanintegervaluefrom1to6,thenthesumofthetwovalueswillvaryfrom2to
12.Theprogramshouldrollthetwodice36000times.Useasinglesubscriptedarraytotallythe
numberoftimeseachpossiblesumappears.Printtheresultsinatabularformat.Findanddisplay
themodeanditscorrespondingnumberofoccurrences.
7. Fortycustomerswereaskedtoratetheservicequalityinashoppingmallonascaleof1to10.
Place the 40 responses in an unsigned integer array and determine the median of the survey
resultsfromfunctionmedian.UsefunctionbubbleSorttosortthearrayofresponses.Eventually,
plottheresultsusingahistogramofasterisks.
Responses={1,2,6,4,8,5,9,7,8,10,1,6,3,8,6,10,3,8,2,7,
6,5,7,6,8,6,7,5,6,6,5,6,7,5,6,4,8,6,8,10}
SIE1003CProgramming
Tutorial5
8. Acompanyhasfoursalesperson(0to3)whosellfivedifferentproducts(P0toP4).Onceaday,
eachsalespersonpassesinaslipforeachdifferenttypeofproductsold.Eachslipcontainsthe
salespersonnumberandproductnumber.Assumethattheinformationfromalltheslipsforlast
monthisavailable,writeaprogramthatreadsallthisinformationandsummarisethetotalsales
bysalespersonandbyproduct.Alltotalsshouldbestoredinthedoublesubscriptedarraysales.
Afterprocessingalltheinformationforlastmonth,printtheresultsintabularformatwitheach
of the rows representing a particular salesperson and each of the columns representing a
particularproduct.Crosstotaleachrowtogetthetotalsalesbyeachsalespersonforlastmonth;
crosstotaleachcolumntogetthetotalsalesofeachproductforlastmonth.Thetabularprintout
shouldalsoincludethesecrosstotalstotherightofthetotalledrowsandtothebottomofthe
totalledcolumns,asshownbelow.
P0
P1
P2
P4
P5
0
1.00
2.00
3.00
4.00
5.00
15.00
1
1.00
2.00
3.00
4.00
5.00
15.00
2
1.00
2.00
3.00
4.00
5.00
15.00
3
1.00
2.00
3.00
4.00
5.00
15.00
4.00
8.00 12.00
16.00
20.00