Upload
miguelmaah
View
219
Download
0
Embed Size (px)
Citation preview
8/10/2019 Pginas desdeSolMNChap.pdf
1/10
CHAPTER 2
2.1IF x < 10 THEN IF x < 5 THEN x = 5 ELSE
PRINT x END IFELSE DO IF x < 50 EXIT x = x - 5 END DOEND IF
2.2Step 1: StartStep 2: Initialize sumand countto zeroStep 3: Examine top card.
Step 4: If it says end of data proceed to step 9; oter!ise" proceed to next step.Step #: $dd %al&e from top card to sum.Step ': Increase count(y 1.Step ): *iscard top cardStep +: ,et&rn to Step 3.Step 9: Is te count-reater tan zero
If yes" proceed to step 1/.If no" proceed to step 11.
Step 1/: 0alc&late a%era-e sumcountStep 11: End
2.3start
sum = 0count = 0
INPUT
value
value =
end of data
value =
end of data
sum = sum + valuecount = count + 1
T
F
count > 0
average = sum/count
end
T
F
8/10/2019 Pginas desdeSolMNChap.pdf
2/10
2.4St&dents co&ld implement te s&(pro-ram in any n&m(er of lan-&a-es. e follo!in-ortran 9/ pro-ram is one example. It so&ld (e noted tat te a%aila(ility of complex%aria(les in ortran 9/" !o&ld allo! tis s&(ro&tine to (e made e%en more concise.5o!e%er" !e did not exploit tis feat&re" in order to ma6e te code more compati(le !it7is&al 8$SI0" $$8" etc.
PROGRAM RootfindIMPLICIT NONEINTEGER::ierREAL::a, b, c, r1, i1, r2, i2DATA a,b,c/1.,.,2./CALL Root!"a, b, c, ier, r1, i1, r2, i2#I$ "ier .E%. T'EN PRINT (, r1,i1,) i) PRINT (, r2,i2,) i)EL*E PRINT (, )No root!)END I$END
*+RO+TINE Root!"a, b, c, ier, r1, i1, r2, i2#IMPLICIT NONEINTEGER::ier
REAL::a, b, c, d, r1, i1, r2, i2r1-&.r2-&.i1-&.i2-&.I$ "a .E%. &.# T'EN I$ "b T'EN r1 - 0c/b EL*E ier - 1 END I$EL*E d - b((2 0 .(a(c I$ "d - T'EN r1 - "0b *%RT"d##/"2(a# r2 - "0b 0 *%RT"d##/"2(a# EL*E r1 - 0b/"2(a#
r2 - r1 i1 - *%RT"A*"d##/"2(a#
i2 - 0i1 END I$END I$END
e ans!ers for te 3 test cases are: a< /.43+" =4.#'; b< /.#; c< 1.2# > 2.33i; 1.2# 2.33i.
Se%eral feat&res of tis s&(ro&tine (ear mention:
e s&(ro&tine does not in%ol%e inp&t or o&tp&t. ,ater" information is passed in and o&t%ia te ar-&ments. is is often te preferred style" (eca&se te I? is left to tediscretion of te pro-rammer !itin te callin- pro-ram.
@ote tat an error code is passed IE, 1< for te case !ere no roots are possi(le.
8/10/2019 Pginas desdeSolMNChap.pdf
3/10
2.# e de%elopment of te al-oritm in-es on reco-nizin- tat te series approximation of tesine can (e represented concisely (y te s&mmation"
x
i
i
i
n 2 1
1 2 1
=
;
8/10/2019 Pginas desdeSolMNChap.pdf
4/10
2.'
start
INPUTx n
! > n
end
! = 1
true = s!n"x#a$$rox = 0
factor = 1
a$$rox a$$roxx
factor!
! %
= + %" 1# % 1& 1
errortrue a$$rox
true100=
C
'UTPUT
!a$$roxerror
! = ! + 1
F
T
factor=factor"&!%"&!%1#
Dse&docode:
SUBROUTINE Sincomp(n,xi = 1!"#$ = SIN(x%pp"ox = 0&%c!o" = 1DO IF i ' n EXIT
%pp"ox = %pp"ox (-1i-1x)i-1* &%c!o" $""o" = +(!"#$ - %pp"ox * !"#$ . 100
PRINT i, !"#$, %pp"ox, $""o" i = i 1
&%c!o" = &%c!o"()i-)()i-1END DOEND
8/10/2019 Pginas desdeSolMNChap.pdf
5/10
2.) e follo!in- ortran 9/ code !as de%eloped (ased on te pse&docode from Dro(. 2.':
PROGRAM *erie!IMPLICIT NONEINTEGER::nREAL::3n - 1
3 - 1.CALL *inco45"n,3#END
*+RO+TINE *inco45"n,3#IMPLICIT NONEINTEGER::n,i,facREAL::3,tr6,a55ro3,eri - 1tr6 - *IN"3#a55ro3 - &.fac - 1PRINT (, ) order tr6e a55ro3 error)DO I$ "i n# E7IT a55ro3 - a55ro3 "01# (( "i01# ( 3 (( "2(i 0 1# / fac
er - A*"tr6 0 a55ro3# / tr6# ( 1&& PRINT (, i, tr6, a55ro3, er i - i 1 fac - fac ( "2(i02# ( "2(i01#END DOEND
O+TP+T: order tr6e a55ro3 error 1 &.8898& 1.&&&&& 0&.9;;8 2 &.8898& &.89&&& ;.&1;; &.8898& 1.&&&9
8/10/2019 Pginas desdeSolMNChap.pdf
6/10
Interpretation: e a(sol&te percent relati%e error drops &ntil at n '" it act&ally yields aperfect res< p&re l&c6A
8/10/2019 Pginas desdeSolMNChap.pdf
7/10
8/10/2019 Pginas desdeSolMNChap.pdf
8/10
*6bro6tine 6bb?e$or"n, b#
I45?icit None
@!ort! an arra= in a!cendin@order 6!in tBe b6bb?e !ort
Inteer"#::4, i, nLoica?::!itcBRea?::a"n#,b"n#,d64
4 - n 0 1Do !itcB - .$a?!e. Do i - 1, 4 If "b"i# b"i 1## TBen d64 - b"i# b"i# - b"i 1# b"i 1# - d64 !itcB - .Tr6e. End If End Do
If "!itcB -- .$a?!e.# E3it 4 - 4 0 1End Do
End
O5tion E35?icit
*6b 6bb?e"n, b#
!ort! an arra= in a!cendinorder 6!in tBe b6bb?e !ort
Di4 4 A! Inteer, i A! InteerDi4 !itcB A! oo?eanDi4 d64 A! *in?e
4 - n 0 1Do !itcB - $a?!e $or i - 1 To 4 If b"i# b"i 1# TBen d64 - b"i# b"i# - b"i 1# b"i 1# - d64 !itcB - Tr6e End If Ne3t i
If !itcB - $a?!e TBen E3it Do 4 - 4 0 1Loo5
End *6b
2.14 5ere is a flo!cart for te al-oritm:
Function Vol(R, d
!i " 3.1415#3
d $ R
d $ 3 % R
V1 " !i % R&3 ' 3
V2 " !i % R&2 (d R
Vol " V1 + V2
Vol "
)*erto!
End Function
Vol " !i % d&3 ' 3
5ere is a pro-ram in 78$:
O5tion E35?icit
$6nction o?"R, d#
8/10/2019 Pginas desdeSolMNChap.pdf
9/10
Di4 1 A! *in?e, F2 A! *in?e, 5i A! *in?e
5i - ( Atn"1#
If d R TBen o? - 5i ( d / E?!eIf d - ( R TBen 1 - 5i ( R /
F2 - 5i ( R 2 ( "d 0 R# o? - 1 F2E?!e o? - )oFerto5)End If
End $6nction
e res<s are
( d )olume1 0*+ 0*0&,&-.1 0*, 0*/+1/
1 1 1*0.-1,1 &*& .*,1-10
1 + -*++0+,+1 +*1 overto$
2.1# 5ere is a flo!cart for te al-oritm:
Function Pol-r(., /
22yxr +=
. $ 0
/ 0/ 0
+
= x
y1tan
= x
y1tan
= x
y1tan = 0
2
=
/ $ 02
=
=
/ $ 0
1+/=Pol-r
1+/=Pol-r
End Pol-r
= +*1.1/+
T
T
T
T
T
F
F
F
F
$nd ere is a 78$ f&nction proced&re to implement it:
O5tion E35?icit
$6nction Po?ar"3, =#
8/10/2019 Pginas desdeSolMNChap.pdf
10/10
Di4 tB A! *in?e, r A! *in?eCon!t 5i A! *in?e - .118
r - *Hr"3 2 = 2#
If 3 & TBen If = & TBen
tB - Atn"= / 3# 5i E?!eIf = & TBen tB - Atn"= / 3# 0 5i E?!e tB - 5i End IfE?!e If = & TBen tB - 5i / 2 E?!eIf = & TBen tB - 05i / 2 E?!e tB - & End IfEnd If
Po?ar - tB ( 1