j j j
SOLUTION MANUAL
j
,CHAPTERj2
2.1
IFj xj <j 10j THENjIF
j xj <j 5j THEN
xj =j 5jELS
E
PRINTj xj
ENDj IF
ELSE
DO
IFj xj <j 50j EXIT
xj =j xj -
j 5jENDj DO
ENDj IF
2.2
Stepj1:jStart
Stepj2:jInitializejsumjandjcountjtojzerojStepj
3:jExaminejtopjcard.
Stepj4:jIfjitjsaysj“endjofjdata”jproceedjtojstepj9;jotherwise,jproceedjtojnextjstep.jStepj5:j
Addjvaluejfromjtopjcardjtojsum.
Stepj6:jIncreasejcountjbyj1.jS
tepj7:jDiscardjtopjcardjStepj8
:jReturnjtojStepj3.
Stepj9:jIsjthejcountjgreaterjthanjzero?
Ifjyes,jproceedjtojstepj10.jIfj
no,jproceedjtojstepj11.
Stepj10:jCalculatejaveragej=jsum/count
Stepj11:jEnd
2.3
start
sumj =j0
countj=j0
j j T
countj>j 0
INPUT
value
F
averagej=jsum/count
valuej=j “e T
ndjofjdata”
F end
sumj=jsumj+jvaluej
countj=jcountj+j1
,2.4
Studentsjcouldjimplementjthejsubprogramjinjanyjnumberjofjlanguages.jThejfollowingj Fortranj90
jprogramjisjonejexample.jItjshouldjbejnotedjthatjthejavailabilityjofjcomplexjvariablesjinjFortranj90
,jwouldjallowjthisjsubroutinejtojbejmadejevenjmorejconcise.j However,jwejdidjnotjexploitjthisjfeat
ure,jinjorderjtojmakejthejcodejmorejcompatiblejwithjVisualjBASIC,jMATLAB,jetc.
PROGRAMj Rootfindj
IMPLICITj NONEj INT
EGER::ier
REAL::a,j b,j c,j r1,j i1,j r2,j i2
DATAj a,b,c/1.,5.,2./
CALLj Roots(a,j b,j c,j ier,j r1,j i1,j r2,j i2)j
IFj (ierj .EQ.j 0)j THEN
PRINTj *,j r1,i1,"j i"
PRINTj *,j r2,i2,"j i"j E
LSE
PRINTj *,j "Noj roots"j E
NDj IF
END
SUBROUTINEj Roots(a,j b,j c,j ier,j r1,j i1,j r2,j i2)j IMPLICI
Tj NONE
INTEGER::ier
REAL::a,jb,jc,jd,jr1,ji1,jr2,ji2j r1=0
.
r2=0.
j i1=0
.j i2=
0.
IFj (aj .EQ.j 0.)j THENj
IFj (bj <>j 0)j THEN
r1j =j -
c/bj ELSE
ierj =j 1j EN
Dj IF
ELSE
dj =j b**2j -
j 4.*a*cj IFj (dj >=j 0
)j THEN
r1j =j (-bj +j SQRT(d))/(2*a)
r2j =j (-bj -
j SQRT(d))/(2*a)j ELSE
r1j =j -
b/(2*a)j r2j =j r
1
i1j =j SQRT(ABS(d))/(2*a)
i2j =j -
i1j ENDj IF
ENDj IFj
END
Thej answersj forj thej 3j testj casesj are:j (a)j 0.438,j -4.56;j (b)j 0.5;j (c)j 1.25j +j 2.33i;j 1.25j
2.33i.
Severaljfeaturesjofjthisjsubroutinejbearjmention:
Thejsubroutinejdoesjnotjinvolvejinputjorjoutput.jRather,jinformationjisjpassedjinjandjoutjviajt
hejarguments.jThisjisjoftenjthejpreferredjstyle,jbecausejthejI/Ojisjleftjtojthej discretionjofjthejpr
ogrammerjwithinjthejcallingjprogram.
Notejthatjanjerrorjcodejisjpassedj(IERj=j1)jforjthejcasejwherejnojrootsjarejpossible.
, 2.5jThejdevelopmentjofjthejalgorithmjhingesjonjrecognizingjthatjthejseriesjapproximationjofjthejsinejc
anjbejrepresentedjconciselyjbyjthejsummation,
j1
n
x2i
(2i 1)!
j
j
i1j j j
wherej ij =j thej orderj ofj thej approximation.j Thej followingj algorithmj implementsj thisjsummat
ion:
Stepj1:jStart
Stepj2:jInputjvaluejtojbejevaluatedjxjandjmaximumjorderjnjStepj
3:jSetjorderj(i)jequaljtojone
Stepj4:jSetjaccumulatorjforjapproximationj(approx)jtojzeroj Stepj5
:jSetjaccumulatorjforjfactorialjproductj(fact)jequaljtojonejStepj6:jC
alculatejtruejvaluejofjsin(x)
Stepj7:jIfjorderjisjgreaterjthanjnjthenjproceedjtojstepj13jOth
erwise,jproceedjtojnextjstep
Stepj8:jCalculatejthejapproximationjwithjthejformula
x2i-
approxj japproxj j(1) i-1
1j fact
Stepj9:jDeterminejthejerror or
truejapproxj t
%errorj 100%j
rue
Stepj10:jIncrementjthejorderjbyjone
Stepj11:jDeterminejthejfactorialjforjthejnextjiteration
factorj j factorj j(2jjij j2)jj(2jjij j1)
Stepj12:jReturnjtojstepj7
Stepj13:jEnd