2.1
IFvxv<v10vTHENvIF
vxv<v5vTHEN
xv=v5vE
LSE
PRINTvxv
ENDvIF
ELSE
DO
IFvxv<v50vEXIT
xv=vxv-
v5v
ENDvDO
ENDvIF
2.2
Stepv1:vStart
Stepv2:vInitializevsumvandvcountvtovzerovStep
v3:vExaminevtopvcard.
Stepv4:vIfvitvsaysv“endvofvdata”vproceedvtovstepv9;votherwise,vproceedvtovnextvstep.vStepv
5:vAddvvaluevfromvtopvcardvtovsum.
Stepv6:vIncreasevcountvbyv1.v
Stepv7:vDiscardvtopvcardvSte
pv8:vReturnvtovStepv3.
Stepv9:vIsvthevcountvgreatervthanvzero?
Ifvyes,vproceedvtovstepv10.v
Ifvno,vproceedvtovstepv11.
Stepv10:vCalculatevaveragev=vsum/count
Stepv11:vEnd
2.3
start
sumv=v0
countv=v0
vvvv T
countv>v0
INPUT
value
F
averagev=vsum/count
valuev =v“e v T
ndvofvdata”
F end
sumv=vsumv+vvalu
evcountv=vcountv+v
1
,2.4
Studentsvcouldvimplementvthevsubprogramvinvanyvnumbervofvlanguages.vThev followingvFortra
nv90vprogramvisvonevexample.vItvshouldvbevnotedvthatvthevavailabilityvofvcomplexvvariablesvinvF
ortranv90,vwouldvallowvthisvsubroutinevtovbevmadevevenv morev concise.vHowever,vwevdidvnotve
xploitvthisvfeature,vinvordervtovmakevthevcodevmorevcompatiblevwithvVisualvBASIC,vMATLAB,
vetc.
PROGRAMvRootfindv
IMPLICITvNONEvIN
TEGER::ier
REAL::a,vb,vc,vr1,vi1,vr2,vi2
DATAva,b,c/1.,5.,2./
CALLvRoots(a,vb,vc,vier,vr1,vi1,vr2,vi2)vIFv
(ierv.EQ.v0)vTHEN
PRINTv*,vr1,i1,"vi"
PRINTv*,vr2,i2,"vi"vE
LSE
PRINTv*,v"Novroots"vE
NDvIF
END
SUBROUTINEvRoots(a,vb,vc,vier,vr1,vi1,vr2,vi2)vIMP
LICITvNONE
INTEGER::ier
REAL::a,vb,vc,vd,vr1,vi1,vr2,vi2vr1
=0.
r2=0.v
i1=0.v
i2=0.
IFv(av.EQ.v0.)vTHENvI
Fv(bv<>v0)vTHEN
r1v=v-
c/bvELSE
ierv=v1v
ENDvIF
ELSE
dv=vb**2v-
v4.*a*cv IFv(dv>=v0)v
THEN
r1v=v(-bv+vSQRT(d))/(2*a)
r2v=v(-bv-
vSQRT(d))/(2*a)v ELSE
r1v=v-
b/(2*a)vr2v=vr
1
i1v=vSQRT(ABS(d))/(2*a)
i2v=v-
i1vENDvIF
ENDvIFv
END
Thev answersv forv thev 3v testv casesv are:v (a)v 0.438,v -4.56;v (b)v 0.5;v (c)v 1.25v +v 2.33i;v 1.25v
2.33i.
Severalvfeaturesvofvthisvsubroutinevbearvmention:
Thevsubroutinevdoesvnotvinvolvevinputvorvoutput.vRather,vinformationvisvpassedvinvandvoutvv
iavthevarguments.vThisvisvoftenvthevpreferredvstyle,vbecausevthevI/Ovisvleftvtov thevdiscretionv
ofvthevprogrammervwithinvthevcallingvprogram.
Notevthatvanverrorvcodevisvpassedv(IERv=v1)vforvthevcasevwherevnovrootsvarevpossible.
, 2.5vThev developmentvofv thevalgorithmvhingesv onv recognizingvthatv thevseriesv approximationv ofv thevsin
evcanvbevrepresentedvconciselyvbyvthevsummation,
n
xv2iv1
(2i 1)!
i1v
v
v v
wherev iv =v thev orderv ofv thev approximation.v Thev followingv algorithmv implementsv thisvsummatio
n:
Stepv1:vStart
Stepv2:vInputvvaluevtovbevevaluatedvxvandvmaximumvordervnv
Stepv3:vSetvorderv(i)vequalvtovone
Stepv4:vSetvaccumulatorvforvapproximationv(approx)vtovzerovSte
pv5:vSetvaccumulatorvforvfactorialvproductv(fact)vequalvtovonevSte
pv6:vCalculatevtruevvaluevofvsin(x)
Stepv7:vIfvordervisvgreatervthanvnvthenvproceedvtovstepv13vOtherwise,
vproceedvtovnextvstep
Stepv8:vCalculatevthevapproximationvwithvthevformula
x2i-
approxvvapproxvv(1) i-1
1vfact
Stepv9:vDeterminevtheverror or
truevvapproxv true
%errorv 100%v
Stepv10:vIncrementvthevordervbyvone
Stepv11:vDeterminevthevfactorialvforvthevnextviteration
factorv v factorv v(2vviv v2)vv(2vviv v1)
Stepv12:vReturnvtovstepv7
Stepv13:vEnd