lecture_16 - “’77??ng L ’9‘ Fame 71/014; fl, 4...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 12
Background image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: “’77??ng L ’9‘ Fame 71/014; fl, 4 [Lama § 19.!- 1‘1. 3 ’9 Rex/{W a OddvlkOZ slkudlwws I914 —> 2% [toad/$14) —> 1‘! [cod/Lb») (sac/{MM} me Jim I) Example: approximate value of rt 7r=4—i+£—i+...+(—1)”‘1 4 3 5 7 2n+1 “ Initialize n = 0.0 Get input # of terms, n Initialize iteration counter, i=0 . +.. Increment counter i = i + 1 Output result rt #include <stdio.h> main() { double t, pi = 0.0; int i; int n; printf("Enter number of terms to evaluate: "); scanf("%d", &n); for (i = 0; i < n; i++) { t = 4.0 / (2.0 *i + 1.0); if (i % 2 == 0) pi += t; else pi -= t; } printf("pi=%.10lf after %d iterations\n", pi, n); } s '5 True value of n pl_f0r.C 3.14159265358... S gcc -ansi pi.c -o pi S ls pi pi_for.c S ./ pi Enter number of terms to evaluate: 10 pi=3.0418396189 after 10 iterations S ./pi Enter number of terms to evaluate: 100 pi=3.1315929036 after 100 iterations S ./ pi Enter number of terms to evaluate: 1000 pi=3.1405926538 after 1000 iterations 5 Jr“ Enter number of terms to evaluate: 10000 pi=3.1414926536 after 10000 iterations Example: approximate value of It with given precision, A Im—mJSA Initialize no = 4.0, n_1 = 0.0 iteration counter, i = 1 Get input accuracy, A Output result "i #include <stdio.h> main() { int i = 1; double t, pi = 4.0; double delta, pi_dif, old_pi = 0.0; printf("Enter accuracy to evaluate to: "); scanf("%lf", &delta); pi_dif = pi — old_pi; while (pi_dif > delta) { old_pi = pi; t = 4.0 / (2.0 * i + 1.0); if (i % 2 == 0) pi += t; else pi -= t; pi_dif = pi - old_pi; pi_dif = (pi_dif > 0.0) ? pi_dif : -pi__dif; i++; } printf("pi=%.10lf after %d iterations\n", pi, i); } S '5 True value of rt pi_whi|e.c 3.14159265358... S gcc -ansi pi_whi|e.c -o pi $ls pi pi_whi|e.c S .lpi Enter accuracy to evaluate to: 0.1 pi=3.1891847823 after 21 iterations S .lpi Enter accuracy to evaluate to: 0.01 pi=3.1465677472 after 201 iterations S ./ pi Enter accuracy to evaluate to: 0.001 pi=3.1420924037 after 2001 iterations $ .lpi Enter accuracy to evaluate to: 0.0001 pi=3.1416426511 after 20001 iterations ~7 Qwé‘m (Ia Ca 4 IC. #9 o? fqaaI/bq ('9 C (’9 ecydm/eq If? a sqflkpww‘r'éae 1’01 443’; flSWVWéZj [detox/mgr. 4131 1's 4’ Segmva ov/ def/P flat-L z’mplomwr/Si game Wd/‘leyf/lh-ZCL ’quflllm Ha, W WoXY‘QW, 952(4me : WP‘Z —> day”? swgwou‘z’w "9 “835/ la Jr’s/Nag flu” ‘70};ch +0Y W194 swgrom‘tw 1'; “(kc/v10 Wu F 1': #1 %W [E av”. 9%64 Wham—Z gwgrpuolthé {’S “we! Jo 924/ 5mg Wye/{L Luz-[63 (PH/WI W meg 2!. -> 1% eavl/re wwwm W [’5 wrf/Jwa ('4' MM 02 was did/WM Ewgmml/WS carp 34%0/ WA“: Melee! 4a M091”?! 0142 or M/J/twr 7143K: dzklalag TLL/g ewfieg/ (gel 114ml, 4? We Mme (Mmé '\ wakFfly My” “1" W “8&- SJ‘Vawv/um v V . J? m (kt,wa —=> Ea:me 0% at W flaw/6 [91.4mm xi; 6 W41“ L ) i 1’ S-Hum‘une 04 0%}; Se,£ap<1}owo/9 ', warm ['9 m‘JmJ $e4ermfmfz‘ exé/J’ wl\(}'€wu{' Know/mg C10 { ) (Kw ('MFWAVLICDM Warm ( y, W; BULLKTW” V 5 }\,/l,u~[e "‘7 Mg/wg fuupla'oug ((4; Q. Peoyu/flafi: [v F (D ——> a fqup-J'Jow Eralo‘ljfie; or W 4"“; [Euler/M (in; a); \ /’ 1‘ a V‘ 1’: 03 00L JIEKM‘L $3493; at F M’umk - vaéwe —7 {mmflég ! 0M 0 WM dawgze wS {WW 2! ' Kama“? méwz [Mi (vmi‘o/J (f.ng lag/goW/cl ~> o. {luumh‘au lamb relum' bw Value; 1’0: (flu/E W #3 rdwm 453% {‘5 "WM." ~> a futoth nub W24 Weymke m»ij 1’14 rflu‘s W #5 51%qu are calmed as *t/w‘cl“ 9 6-8. Voz'c/ almSarecm [Va/J]; ) / l9 {$un 9601' final/M’s: dwaMM [/0 —-> Jammy. M» 0L— :‘wJoWhé’w. ~> MW Wu Male yJ M MMLz/W - ~7 z‘uclucle/s a {lam/ad jay/«WM 4%; ~ a (ISL 0% Wank/flog clef/[Me Ma! Ma ordtw I’M wluw 44% M16 gym Jo m (4867)“. ML Fatima” [z’mln)__é- mic Wmdycco“-" {6‘44 2” haw/#30 519113 03” (We Q4 7L0“ U: 1' 64:14; 1‘++ \ ‘Q‘ - mwfl’ = new“ a) éuWWm é fie-horn mead-Ll W ‘5 w mWoV'A {\hdx‘wm Mel/{W 0* N laud M ‘3 éoée @9964 “154‘ ,3 @ > utL‘\WV$’:agra%n_ ‘11190 and; Jaw gag ,L, 4w :4th fimdmgé @awarwm w 4": M 1* “Mr/64$ 451ml (:9. L > a“; Paloma (m4 “3' (H W94 / aha/4m w“ { _ [bu/a Uni (flown/g”) quach/n‘ \ l‘ui (MSW) Frz‘quflrfufml a Wager: ">‘ Sam/u} (mwMMWy—j WW wow WW , ' PVWAJJ’ ("TAP J'M‘Lon! a V, (11'; \H: mantle}: W]; l1'AMWl/QVL‘IE M2 av WVZ t3 ertgm‘fleol 7L)»th W @MW #0 W VZL/hpfit/b’ln alkyd 61ng 6’1an Wit/w; 9 fine eel/e1, E 9M ‘. MWWHL 3W marrow/x M710 K WM WMWS dz Qfiuwh‘m Afr arm/MA: W WW 1),: (Mé Jam); p. duWWr/ W: [ng/ I farm cmdmi’ P/ /&wg1€ (£314?) pél‘mlgWW: #include <stdio.h> /* function prototype */ double computePi(double delta); main() { double delta; double pi; printf("Enter accuracy to evaluate to: "); scanf("%lf", &delta); pi = computePi(delta); /* function call */ printf("pi=%.10lf\n", pi); } double computePi(double delta) { int i = 1; double t, pi = 4.0; double pi_dif, old_pi = 0.0; pi_dif = pi - old_pi; while (pi_dif > delta) { old_pi = pi; t = 4.0 / (2.0 * i + 1.0); if (i % 2 == 0) pi += t; else pi -= t; pi_dif = pi - old_pi; pi_dif = (pi_dif > 0.0) ? pi_dif : -pi_dif; i++; } return pi; } HIS/WQJZ 4;#Jrfa.li> W awed Maw (maze mm);- dowé comm Pr (dew 4444); WQ am: mac/ms) { ($0ng pz‘ = (:0ka Pi (0. WWW!» {’6‘}wa pz' 46 ram} 96 med/9,4; M12 WW F» [M12 Jew} fi [kw/W416 Pl'x/ ...
View Full Document

Page1 / 13

lecture_16 - “’77??ng L ’9‘ Fame 71/014; fl, 4...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online