ed
SOLUTIONgMANUAL
,CHAPTERg2
2.1
IFg xg <g 10g THEN
gIFg xg <g 5g TH
EN
xg =g 5gE
LSE
PRINTg x
gENDg IF
ELSE
DO
IFg xg <g 50g EXIT
xg =g xg -
g 5gENDg DO
ENDg IF
2.2
Stepg1:gStart
Stepg2:gInitializegsumgandgcountgtogzer
ogStepg3:gExaminegtopgcard.
Stepg4:gIfgitgsaysg“endgofgdata”gproceedgtogstepg9;gotherwise,gproceedgtognext
gstep.gStepg5:gAddgvaluegfromgtopgcardgtogsum.
Stepg6:gIncreasegcountgby
g1.gStepg7:gDiscardgtopgc
ardgStepg8:gReturngtogSte
pg3.
Stepg9:gIsgthegcountggreatergthangzero?
Ifgyes,gproceedgtogstepg1
0.gIfgno,gproceedgtogste
pg11.
Stepg10:gCalculategaverageg=gsum/count
Stepg11:gEnd
2.3
start
sumg=g0
countg=g
0
ggT
countg>g
INPUT 0
value
F
averageg=gsum/coun
t
valueg=g “ T
endgofgdata”
F end
sumg=gsumg+gval
ueg countg=gcoun
tg+g1
,2.4
Studentsgcouldgimplementgthegsubprogramginganygnumbergofglanguages.gThegfollowing
g Fortrang90gprogramgisgonegexample.gItgshouldgbegnotedgthatgthegavailabilitygofgcomp
lexgvariablesgingFortrang90,gwouldgallowgthisgsubroutinegtogbegmadegevengmoregconcis
e.g However,gwegdidgnotgexploitgthisgfeature,gingordergtogmakegthegcodegmoregcompat
iblegwithgVisualgBASIC,gMATLAB,getc.
PROGRAMg Rootfind
g IMPLICITg NONEg I
NTEGER::ier
REAL::a,g b,g c,g r1,g i1,g r2,g i2
DATAg a,b,c/1.,5.,2./
CALLg Roots(a,g b,g c,g ier,g r1,g i1,g r2,g
i2)g IFg (ierg .EQ.g 0)g THEN
PRINTg *,g r1,i1,"g i"
PRINTg *,g r2,i2,"g i"
g ELSE
PRINTg *,g "Nog roots"
g ENDg IF
END
SUBROUTINEg Roots(a,g b,g c,g ier,g r1,g i1,g r2,g i2)g IMP
LICITg NONE
INTEGER::ier
REAL::a,gb,gc,gd,gr1,gi1,gr2,gi2
g r1=0.
r2=0.
g i1=
0.g i
2=0.
IFg (ag .EQ.g 0.)g TH
ENg IFg (bg <>g 0)g
THEN
r1g =g -
c/bg ELSE
ierg =g 1g
ENDg IF
ELSE
dg =g b**2g -
g 4.*a*cg IFg (dg >
=g 0)g THEN
r1g =g (-bg +g SQRT(d))/(2*a)
r2g =g (-bg -
g SQRT(d))/(2*a)g ELSE
r1g =g -
b/(2*a)g r2g =g
r1
i1g =g SQRT(ABS(d))/(2*a)
i2g =g -
i1g ENDg IF
ENDg IF
g END
Theganswersgforgtheg3gtestgcasesgare:g(a)g0.438,g-
4.56;g(b)g0.5;g(c)g1.25g+g2.33i;g1.25g
2.33i.
Severalgfeaturesgofgthisgsubroutinegbeargmention:
Thegsubroutinegdoesgnotginvolveginputgorgoutput.gRather,ginformationgisgpassedging
andgoutgviagthegarguments.gThisgisgoftengthegpreferredgstyle,gbecausegthegI/Ogisgle
ftgtogtheg discretiongofgthegprogrammergwithingthegcallinggprogram.
Notegthatgangerrorgcodegisgpassedg(IERg=g1)gforgthegcasegwheregnogrootsgaregpossible.
, 2.5gThegdevelopmentgofgthegalgorithmghingesgongrecognizinggthatgthegseriesgapproximation
gofgthegsinegcangbegrepresentedgconciselygbygthegsummation,
n
xg2ig1
g
g (2igg1)!
i1
whereg ig =g theg orderg ofg theg approximation.g Theg followingg algorithmg implementsg
thisgsummation:
Stepg1:gStart
Stepg2:gInputgvaluegtogbegevaluatedgxgandgmaximumgor
dergngStepg3:gSetgorderg(i)gequalgtogone
Stepg4:gSetgaccumulatorgforgapproximationg(approx)gtogzer
og Stepg5:gSetgaccumulatorgforgfactorialgproductg(fact)gequ
algtogonegStepg6:gCalculategtruegvaluegofgsin(x)
Stepg7:gIfgordergisggreatergthangngthengproceedgtog
stepg13gOtherwise,gproceedgtognextgstep
Stepg8:gCalculategthegapproximationgwithgthegformula
x2i-
approxggapproxgg( i-1
1g fact
1)
or
Stepg9:gDeterminegthegerror
truegapproxg
%errorg 100
%g
true
Stepg10:gIncrementgthegordergbygone
Stepg11:gDeterminegthegfactorialgforgthegnextgiteration
factorg gfactorgg(2ggigg2)gg(2ggigg1)
Stepg12:gReturngtogstepg7
Stepg13:gEnd