CÂU HỎI : Cho biết kết quả thực thi chương trình sau:

  • View
    48

  • Download
    3

Embed Size (px)

DESCRIPTION

CU HI : Cho bit kt qu thc thi chng trnh sau:. void f(int, int&, int); void main() { int a=100, b=200, c=300; cout

Transcript

  • CU HI: Cho bit kt qu thc thi chng trnh sau:void f(int, int&, int);void main(){int a=100, b=200, c=300; cout
  • CHNG 5CHNG TRNH CON(TIP THEO)

    VI. PHM VI - THI GIAN SNG CA BIN (SCOPE & LIFE TIME)VII. HM QUI

  • VI. PHM VI - THI GIAN SNG CA BIN (SCOPE & LIFE TIME)nh ngha phm vi bin: Phm vi ca bin l phn chng trnh trong vic tham kho n bin l hp l. Gm bin cc b v bin tan cc.N Thi gian sng ca bin: Khi thc thi chng trnh, bin c to ra v hy i. Thi gian sng l thi gian tn ti ca bin t lc c to ra n lc hy i. Thi gian sng ph thuc phm vi v lp lu tr.

  • VI. PHM VI - THI GIAN SNG CA BIN (SCOPE & LIFE TIME)1. Bin tan cc: Khai bo bn ngoi cc hm.T ng khi to gi tr 0 (khng cho php vit lnh khai bo khi to tr ).C thi gian sng cng vi thi gian chy chng trnh. Nhc li: int n; //khai bo binint a=5; //khai bo bin c khi to tr.//khng cho php /v tan cc

  • VI. PHM VI - THI GIAN SNG CA BIN (SCOPE & LIFE TIME)2. Bin cc b:Khai bo bn trong hm, trong khi lnh.Khng c t ng khi to gi tr.C phm vi t v tr khai bo n cui hm hoc cui khi lnh. Thi gian sng t lc hm c thc thi hoc khi lnh c thc thi n lc kt thc hm hoc chy xong khi lnh. Xt v d sau:

  • int n1;void sub_fun();void main(){int n2=2;int n3;cout
  • VI. PHM VI - THI GIAN SNG CA BIN (SCOPE & LIFE TIME)Lu 1: Trong cng phm vi, mi bin ch c khai bo mt ln.Phm vi cc b che ph phm vi ton cc.Trong phm vi cc b, mun s dng bin tan cc cng tn ta dng tan t phm vi (::)V d:

  • #include int n1;void sub_fun();void main(){int n1=10;cout
  • VI. PHM VI - THI GIAN SNG CA BIN (SCOPE & LIFE TIME)Lu 2: Cc hm phi c khai bo ton cc (Bn trong hm khng c khai bo hm) Cc hm cng tn, khc thng s c gi l overload.V d:

  • #include #include void GiaiPhuongTrinh(float, float);void GiaiPhuongTrinh(float,float,float);void main(){float m=2,n=3,p=-4;cout
  • void GiaiPhuongTrinh(float a, float b){if (a!=0) cout
  • void GiaiPhuongTrinh(float a, float b, float c){if (a==0) { GiaiPhuongTrinh(a,b); return;}float d=b*b -4*a*c;if (d>0){ cout
  • VI. PHM VI - THI GIAN SNG CA BIN (SCOPE & LIFE TIME)3. Lp lu tr bin:C 4 lp lu tr l: auto, static, extern, register.a. Lp lu tr cho bin cc b: auto, static hoc register.Ch nh lp khi khai bo mt bin cc bV d: static int x;register float y;Mc nh l auto, khng c khi ng tr.V d: int n; //n c mt tr bt k khi toBin thuc lp register c c tnh ging auto, nhng c lu trong thanh ghi (Vng nh CPU).

  • VI. PHM VI - THI GIAN SNG CA BIN (SCOPE & LIFE TIME) Bin cc b auto v register c thi gian sng cng vi hm nn tr mt i sau mi ln gi hm.Bin cc b static c thi gian sng cng vi chng trnh. c khi ng tr t ng nu ta khng khi ng tr cho n. V ch khi ng tr 1 ln duy nht khi c to. Nn gi li c gi tr ca ln gi hm trc .V d

  • #include #include void sub();void main(){int i;for (i=0; i
  • VI. PHM VI - THI GIAN SNG CA BIN (SCOPE & LIFE TIME)b. Lp lu tr cho bin tan cc: extern hoc static. V d: extern int n; static float b;Lp lu tr ca bin tan cc ch thay i phm vi s dng bin, khng i thi gian sng:extern thng bo (declaration) mt bin c khai bo (definition) tp tin khc.static ngn cm khng cho bin s dng tp tin khc.

  • VII. HM QUI1. nh ngha: Hm qui l hm c cha (hoc n cha) li gi hm li chnh n. V d: Hm in ra n du *void InDauSao(int n){cout
  • VII. HM QUI2. Phn lai: qui tuyn tnh: Trong hm ch c 1 li gi hm qui. qui phi tuyn: C hn 1 li gi hm qui. qui nh phn l trng hp c bit ca qui phi tuyn. qui tng h: Khng qui trc tip, khng gi li chnh mnh.V d: A gi B, B gi C, C gi A

  • VII. HM QUI3. S dng hm qui khi no? Gii bi tan c tnh qui gip chng trnh ngn gn.L dng tan c th qui v mt bi tan cng dng nhng vi s lng php tan hay cng vic gim i.V d:

  • VII. HM QUI3. S dng hm qui khi no? V d: n! = n * (n-1)!USC(x,y)= USC(x-y,y) nu x>yS ch s ca x= 1 + s ch s ca x/10fibonacci(n)=fibonacci(n-1)+fibonacci(n-2)

  • VII. HM QUI4. Nguyn tc vit hm qui: Hm qui cn tha 2 k:Gii quyt c trong trng hp ring ng vi gi tr c bit ca tham s (tr/h suy bin).Trng hp tng qut, bi tan c nh ngha qui vi gi tr tham s thay i.V d: Tnh n!Tng qut: n!= n*(n-1)!Suy bin: Bng 1 khi n=0||n=1

  • VII. HM QUINh vy mt hm qui s c dng

    ... hmF (...){if ( thng_s == tr_c_bit )return kt_qu;....hmF (...);....}

  • 5. Qu trnh thc thi hm qui:V d 1: m s ch s ca mt s nguyn n>0

    int DigitCount(int n){if (n

  • 5. Qu trnh thc thi hm qui:DigitCount(517)123

  • 5. Qu trnh thc thi hm qui:V d 2: Tm c s chung ln nht ca 2 s nguyn dng.

    int USCLN(int u, int v){if (v==0) return u;elsereturn USCLN(v, u%v);}

    Gi hm USCLN(9,12) ta c qu trnh thc thi sau:

  • 5. Qu trnh thc thi hm qui:USCLN(9,6)3333

  • 5. Qu trnh thc thi hm qui:Kt lun:Mi ln gi qui tp cc bin cc b, tham tr c to ra c lp vi tp cc bin to ra cho ln gi hm trc.Mi ln that ra khi hm ca 1 ln gi th tp bin cc b tng ng b xa.Ln gi hm sau cng s that ra u tin.Hm qui thc thi cn nhiu khng gian nh hn hm khng qui.

  • 6. Cc v d khcS= 1 + 2 + 3 + .... + nS= x + x3+ x5 + ... + x2n+1Tm USCLN ca 2 s bng thut tan trTnh s Fibonacci th nBi tan thp h ni