You are on page 1of 150

Li m u

Gio trnh Bi Tp K Thut Lp Trnh Trang 1



LI M U
.

Khi bt u lm quen vi ngn ng lp trnh C th l ngn ng C Sinh Vin thng
gp kh khn trong vic chuyn vn l thuyt sang ci t c th trn my. Sch
Gio Trnh Bi Tp K Thut Lp Trnh nhm cung cp cho cc Hc Sinh - Sinh
Vin Trng C Cng Ngh Thng Tin Tp. H Ch Minh h thng cc bi tp, nhng
k nng thc hnh c bn v nng cao v ngn ng lp trnh C. Cun sch ny c
xem nh ti liu hng dn tng bc cho Hc Sinh - Sinh Vin ca Trng trong vic
hc v p dng kin thc l thuyt trn lp mt cch thnh tho v su rng.

Gio trnh c chia thnh 10 chng theo tng ni dung kin thc, km theo Cc
thi mu v 1 ph lc hng dn vit chng trnh, chun on li v sa li. Mi
chng gm 2 phn:
+ Phn l thuyt: c tm tt ngn gn vi y v d minh ho km theo.
+ Phn bi tp: vi nhiu bi tp c chia lm hai mc c bn v luyn tp
nng cao, bi tp c nh du * l bi tp kh dnh cho sinh vin luyn tp thm.
+ Phn kt lun: Tm tt ni dung v cc thao tc m sinh vin cn nm hay
nhng lu ca chng .

Trong qu trnh bin son, chng ti c gng trch lc nhng kin thc rt c bn,
nhng li hay gp i vi ngi mi lp trnh. Bn cnh chng ti cng b sung
thm mt s bi tp nng cao rn luyn thm k nng lp trnh.

Tuy nhin, ch ch chnh ca gio trnh ny l phc v cho mt mn hc nn chc chn
khng th trnh khi nhng thiu st, v th, rt mong nhn c nhng gp qu bu
ca cc thy c, cc ng nghip v cc bn Hc Sinh Sinh Vin gio trnh ny
ngy cng hon thin hn.

Chn thnh cm n.
Lch trnh thc hnh
Gio trnh Bi Tp K Thut Lp Trnh Trang 2

LCH TRNH THC HNH



Tng thi gian: 90 tit.
STT NI DUNG S TIT
1 Chng 1: Lu thut ton 03
2 Chng 2: Cu trc iu khin 06
3 Chng 3: Hm con 12
4 Chng 4: Mng mt chiu 24
5 Chng 5: Chui k t 06
6 Chng 6: Mng hai chiu 12
7 Chng 7: Kiu d liu c cu trc 12
8 Chng 8: Tp tin 06
9 Chng 9: qui 06
10 Chng 10: Hng dn lp trnh bng phng php Project 03
Lu thut ton
Gio trnh Bi Tp K Thut Lp Trnh Trang 3
CHNG 1 LU THUT TON (FLOWCHART)

Cc k hiu biu din lu thut ton, cch biu din cc cu trc iu khin r
nhnh, cu trc lp v cc k thut lin quan n lu thut ton.
I. TM TT L THUYT
I.1. Khi nim
Lu thut ton l cng c dng biu din thut ton, vic m t nhp
(input), d liu xut (output) v lung x l thng qua cc k hiu hnh hc.
I.2. Phng php duyt
Duyt t trn xung.
Duyt t tri sang phi.
I.3. Cc k hiu
STT K HIU DIN GII
1

Bt u chng trnh
2

Kt thc chng trnh
3

Lung x l
4

iu khin la chn
5

Nhp
6

Xut
7

X l, tnh ton hoc gn
8

Tr v gi tr (return)
9

im ni lin kt tip theo (S dng khi lu
vt qu trang)
Lu thut ton
Gio trnh Bi Tp K Thut Lp Trnh Trang 4
I.4. Cc cu trc iu khin c bn
a. Cu trc tun t
Tun t thc thi tin trnh. Mi lnh c thc thi theo mt chui t trn
xung, xong lnh ny ri chuyn xung lnh k tip.

V d: Nhp vo 3 s nguyn a, b, c v xut ra mn hnh vi gi tr ca mi
s tng ln 1.
a = a + 1
b = b + 1
c = c + 1
a, b, c
BAT AU
KET THUC
a, b, c


Lu thut ton
Gio trnh Bi Tp K Thut Lp Trnh Trang 5
b. Cu trc la chn
im quyt nh cho php chn mt trong hai trng hp.
if
Ch xt trng hp ng.
Bieu thc
ieu kien
ung

V d: Nhp vo s nguyn n. Kim tra nu n > 0 tng n ln 1 n v.
Xut kt qu.
ung
n
BAT AU
KET THUC
n
n > 0
n = n+1


Lu thut ton
Gio trnh Bi Tp K Thut Lp Trnh Trang 6
ifelse
Xt trng hp ng v trng hp sai.
Bieu thc
ieu kien
ung Sai

V d: Nhp vo s nguyn n. Kim tra nu n chn xut ra mn hnh n
chn, ngc li xut n l.

c. Cu trc lp
Thc hin lin tc 1 lnh hay tp lnh vi s ln lp da vo iu kin.
Lp s kt thc khi iu kin c tha.
Lu thut ton
Gio trnh Bi Tp K Thut Lp Trnh Trang 7
for / while (Kim tra iu kin trc khi lp)
ieu
kien lap
ung
Sai

V d: Nhp vo s nguyn n. Xut ra mn hnh t 1 n n.
ung
n
BAT AU
KET THUC
i
i n
Sai
i=1
i = i + 1


Lu thut ton
Gio trnh Bi Tp K Thut Lp Trnh Trang 8
do while (Thc hin lp trc khi kim tra iu kin)

V d: Nhp vo s nguyn dng n. Nu nhp sai yu cu nhp li.

d. Cc v d
V d 1: Gii v bin lun phng trnh: ax+b=0.
Lu thut ton
Gio trnh Bi Tp K Thut Lp Trnh Trang 9
BAT AU
a, b, c
Vo
Nghiem
KET THUC
a=0
b 0
ung
ung
Vo So
Nghiem
Sai
Sai
Nghiem
x=-b/a

V d 2: Tnh tng: n S + + + + = L 3 2 1 , vi n>0

V d 3: Tnh tng:
2 2
1 2
...
6
5
4
3
2
1
) (
+
+
+ + + + =
n
n
n S , vi n>0
Lu thut ton
Gio trnh Bi Tp K Thut Lp Trnh Trang 10
BAT AU
n
i = 0
S = 0
t = 1
m = 2
i <= n
S = S + t/m
t = t + 2
m = m + 2
ung
i = i +1
KET THUC
S
Sai

V d 4: Tnh tng: n
n
n S
1
) 1 ( 4 3 2 1 ) (
+
+ + + = L , vi n>0
BAT AU
n
i = 1
S = 0
dau = 1
i <= n
S = S + dau*i
dau = -dau
ung
i = i +1
KET THUC
S
Sai

Lu thut ton
Gio trnh Bi Tp K Thut Lp Trnh Trang 11
II. BI TP
V lu thut ton sau
II.1. Bi tp c bn
1. Nhp vo hai s x, y. Xut ra mn hnh tng, hiu, tch, thng ca hai s
trn.
2. Nhp vo s nguyn n, kim tra xem n chn hay l v xut ra mn hnh.
3. Nhp vo ba cnh a, b, c ca tam gic. Xut ra mn hnh tam gic thuc
loi tam gic g? (Thng, cn, vung, u hay vung cn).
4. Nhp vo s nguyn n. Xut ra n mn hnh (Nu n chn th gp i gi tr).
5. Nhp vo s nguyn n. Nu n>5 th tng n ln 2 n v v tr v gi tr n,
ngc li tr v gi tr 0.
6. Tnh n!, vi 0 n
7. Tnh ) 1 2 ( 5 . 3 . 1 ) ( + = n n P K , vi 0 n
8. Tnh ) 1 2 ( 5 3 1 ) ( + + + + + = n n S L , vi 0 n
9. Tnh n n S
n 1
) 1 ( 4 3 2 1 ) (
+
+ + + = L , vi 0 > n
10. Tnh n n S K L 3 . 2 . 1 3 . 2 . 1 2 . 1 1 ) ( + + + + = , vi 0 > n
11. Tnh
2 2 2 2
3 2 1 ) ( n n S + + + + = L , vi 0 > n
12. Tnh
n
n S
1
3
1
2
1
1 ) ( + + + + = L , vi 0 > n
13. (*) Tnh
n
n S
+ + + +
+ +
+ +
+
+
+ =
L
L
3 2 1
1
3 2 1
1
2 1
1
1 ) ( , vi 0 > n
14. Tnh
y
x y x P = ) , ( .
15. Tnh ) 3 2 1 ( ) 3 2 1 ( ) 2 1 ( 1 ) ( n n S + + + + + + + + + + + = L L , vi 0 > n
16. Cho s nguyn n. Tnh tr tuyt i ca n.
17. Cho s nguyn dng n gm k ch s. Tm ch s c gi tr ln nht.
18. m s lng c s chn ca s nguyn dng n.
19. In ra ch s u tin ca s nguyn dng n gm k ch s.
20. Cho 2 s nguyn dng a, b. Tm USCLN ca a v b.
21. Cho 2 s nguyn dng a, b. Tm BSCNN ca a v b.
22. Cho s nguyn dng x. Kim tra xem x c phi l s nguyn t khng?
23. Cho s nguyn dng x. Kim tra x c phi l s chnh phng khng?
24. Cho s nguyn dng x. Kim tra xem x c phi l s hon thin khng?
Lu thut ton
Gio trnh Bi Tp K Thut Lp Trnh Trang 12
II.2. Bi tp luyn tp v nng cao
25. Tnh
n
n n S + + + + = L
3 2
3 2 1 ) ( , vi 0 n
26. Tnh
1 4
3
3
2
2
1
) (
+
+ + + + =
n
n
n S L
, vi
0 > n

27. Tnh
!
1
! 3
1
! 2
1
1 ) (
n
n S + + + + = L
, vi
0 > n

28. Tnh
!
3 2 1
! 3
3 2 1
! 2
2 1
1 ) (
n
n
n S
+ + + +
+ +
+ +
+
+
+ =
L
L
, vi
0 > n

29. Gii v bin lun phng trnh:
0
2
= + + c bx ax

30. Gii v bin lun phng trnh: 0
2 4
= + + c bx ax
31. (*) Tnh 1 ... ) 2 ( ) 1 ( ) ( + + + + = n n n n S , vi 0 > n
32. (**) Tnh n n S + + + + = ... 3 2 1 ) ( , vi 0 > n
III. KT LUN
Lu thut ton rt hu ch trong vic m t cch gii quyt ca mt bi ton.
Vic m t ny rt trc quan thng qua cc k hiu hnh hc, y l giai on u
tin trc khi bt tay vo lp trnh trn mt ngn ng lp trnh c th.
Khi xy dng lu thut ton, chng ta cn ch mt vi im sau:
+ Mt lu phi c im bt u v im kt thc (iu kin kt thc).
+ Phi c d liu vo, d liu ra sau khi x l tnh ton.
+ Ti mi v tr quyt nh la chn r nhnh phi ghi r iu kin ng hoc
sai th i theo nhnh no.
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 13
CHNG 2 CU TRC IU KHIN

Tm hiu v ci t cc cu trc r nhnh, la chn, lp v cc k hiu php ton trong
ngn ng C. M t cch hot ng v hng dn chy tng bc chng trnh.
I. TM TT L THUYT
I.1. Cc k hiu
STT
K
HIU
DIN GII V D
1 { }
Bt u v kt thc hm hay khi
lnh.
void main()
{

}
2 ;
Kt thc khai bo bin, mt lnh,
mt li gi hm, hay khai bo
nguyn mu hm.
int x;
void NhapMang(int a[], int &n);
3 //
Ch thch (ghi ch) cho mt dng.
Ch c tc dng i vi ngi c
chng trnh.
//Ham nay dung de nhap mang
void NhapMang(int a[], int &n);
4
/*

*/
Tng t nh k hiu //, nhng
cho trng hp nhiu dng.
/* Dau tien nhap vao n. Sau do
nhap cho tung phan tu */
void NhapMang(int a[], int &n);
I.2. Cc kiu d liu c bn trong C
STT KIU GHI CH
KCH
THC
NH
DNG
KIU LIN TC (S THC)
1 float 4 bytes %f
2
double

8 bytes %lf
3
long double

10 bytes %lf
KIU RI RC (S NGUYN)
K t 1 byte %c
1 char
S nguyn 1 byte %d
2
unsigned char S nguyn dng 1 byte
%d
3
int S nguyn 2 bytes
%d
4
unsigned int S nguyn dng 2 bytes
%u
5
long S nguyn 4 bytes
%ld
6
unsigned long S nguyn dng 4 bytes
%lu
7
char * Chui
%s

Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 14
I.3. Bng k hiu cc php ton
STT
PHP
TON
NGHA GHI CH
PHP TON S HC
1 + Cng

2 - Tr

3 * Nhn

4 / Chia ly phn nguyn

5 % Chia ly phn d

PHP TON QUAN H
1 > Ln hn

2 < Nh hn

3 >= Ln hn hoc bng

4 <= Nh hn hoc bng

5 = = Bng nhau

6 != Khc nhau

PHP TON LOGIC
1 ! NOT

2 && AND

3 || OR

TON T TNG GIM
1 ++ Tng 1
2 -- Gim 1
Nu ton t tng gim t trc th tng
gim trc ri tnh biu thc hoc ngc
li.
PHP TON THAO TC TRN BIT
1 & AND

2 | OR

3 ^ XOR

4 << Dch tri

5 >> Dch phi

6 ~ Ly phn b theo bit


Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 15
I.4. Cc hm c bn
STT
TN
HM
TH VIN DIN GII V D
1 printf #include<stdio.h> Xut ra mn hnh.
2 scanf #include<stdio.h>
Ly d liu t bn
phm.
3 gotoxy #include<conio.h>
Di chuyn du nhy
n ta (x, y) trn
mn hnh vn bn.
4 textcolor #include<conio.h>
t mu cho ch (c
gi tr t 0 n 15).
5 cprintf #include<stdio.h>
Xut ra mn hnh vi
mu ch nh lin
trc .
6 delay #include<dos.h>
Dng thc hin lnh
tip sau mt khong
thi gian.
7 kbhit #include<conio.h>
Kim tra xem c
nhn phm.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
int c = 1, n;

clrscr();
printf(Nhap n:);
scanf(%d, &n);
do{
textcolor(c);
gotoxy(20, 10);
cprintf(%d, n);
c++;
if (c>15)
c = 1;
delay(200);
} while(!kbhit());
}
I.5. Cu trc r nhnh
a. Cu trc if
if (biu thc iu kin)
{
<khi lnh> ;
}
Nu biu thc iu kin cho kt qu khc khng th thc hin khi lnh.
V d:
#include <conio.h>
#include <stdio.h>
void main ()
{
float number ;

printf ( Nhap mot so trong khoang tu 1 den 10 => ) ;
scanf ( %f, &number) ;
if (number >5)
printf ( So ban nhap lon hon 5. \n) ;
printf ( %f la so ban nhap. , number);
}
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 16
b. Cu trc if else
if (biu thc iu kin)
{
<khi lnh 1>;
}
else
{
<khi lnh 2>;
}
Nu biu thc iu kin cho kt qu khc khng th thc hin khi lnh 1,
ngc li th cho thc hin khi lnh th 2. Biu thc iu kin phi t trong
cp du ngoc trn.
V d: Gii v bin lun phng trnh: ax+b=0
#include <conio.h>
#include <stdio.h>
void main ()
{
float a, b;
printf ( \n Nhap vao a:);
scanf ( %f, &a);
printf ( Nhap vao b:);
scanf ( %f, &b) ;
if (a= = 0)
if (b= = 0)
printf ( \n PTVSN);
else
printf ( \n PTVN);
else
printf ( \n Nghiem x=%f, -b/a);
getch ();
}
I.6. Cu trc la chn switch
switch (biu thc)
{
case n1:
cc cu lnh ;
break ;
case n2:
cc cu lnh ;
break ;

case nk:
<cc cu lnh> ;
break ;
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 17
[default: cc cu lnh]
}
n
i
l cc hng s nguyn hoc k t.
Ph thuc vo gi tr ca biu thc vit sau switch, nu:
o Gi tr ny = n
i
th thc hin cu lnh sau case n
i
.
o Khi gi tr biu thc khng tha tt c cc n
i
th thc hin cu lnh sau
default nu c, hoc thot khi cu lnh switch.
o Khi chng trnh thc hin xong cu lnh ca case n
i
no th n s
thc hin lun cc lnh thuc case bn di n m khng xt li iu kin
(do cc n
i
c xem nh cc nhn) V vy, chng trnh thot khi
lnh switch sau khi thc hin xong mt trng hp, ta dng lnh break.
V d: To menu cp 1 cho php chn menu bng s nhp t bn phm.
#include<stdio.h>
#include<conio.h>
int ChonTD ()
{
int chon ;

printf ("Thuc Don") ;
printf ("\n1. Lau thai!") ;
printf ("\n2. Nuoc ngot!") ;
printf ("\n3. Ca loc hap bau!") ;
printf ("\n4. Chuot dong!") ;
printf ("\n Xin moi ban chon mon an!") ;
scanf ("%d",&chon) ;
return chon ;
}

void TDchon(int chon)
{
switch (chon)
{
case 1:
printf ("\nBan chon lau thai!") ;
break ;
case 2:
printf ("\nBan chon nuoc ngot!") ;
break ;
case 3:
printf ("\nBan chon ca loc hap bau!") ;
break ;
case 4:
printf ("\Ban chon chuot dong!") ;
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 18
break ;
default:
printf ("\nBan chon khong dung!") ;
}
}

void main()
{
clrscr() ;
int c ;
c=ChonTD() ;
TDchon(c) ;
getch() ;
}
I.7. Cu trc lp
a. for
for (<biu thc khi gn>; <biu thc iu kin>; <biu thc tng/gim>)
{
<khi lnh>;
}
Bt k biu thc no trong 3 biu thc ni trn u c th vng nhng phi
gi du chm phy (;).
Hot ng ca cu trc iu khin for:

Bc 1: Khi gn cho biu thc 1
Bc 2: Kim tra iu kin ca biu thc 2.
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 19
Nu biu thc 2 0 th cho thc hin cc lnh ca vng lp, thc hin
biu thc 3. Quay tr li bc 2.
Ngc li thot khi lp.

V d: In ra mn hnh bng m ASCII t k t s 33 n 255.
#include<conio.h>
#include<stdio.h>
void main()
{
for (int i=33;i<=255;i++)
printf("Ma ASCII cua %c: %d\t", i, i) ;
getch () ;
}
b. while
< Khi gn>
while ( <biu thc iu kin>)
{
lnh/ khi lnh;
< tng/gim ch s lp>;
}

Lu : Cch hot ng ca while ging for

V d: Tnh gi tr trung bnh cc ch s ca s nguyn n gm k ch s.
#include<stdio.h>
#include<conio.h>
void main()
{
long n, tong=0;
int sochuso=0;
float tb;

printf ("Nhap vao gia tri n gom k chu so") ;
scanf ("%ld",&n) ;
while(n>0)
{
tong=tong+n%10 ;
sochuso++ ;
n=n/10 ;
}

tb=1.0*tong/sochuso ;
printf ("Gia tri trung binh la: %f", tb) ;
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 20
getch () ;
}
c. do while
do
{
< khi lnh> ;
} while (biu thc iu kin) ;
Thc hin khi lnh cho n khi biu thc c gi tr bng 0.
V d: Nhp k t t bn phm hin th ln mn hnh m ASCII ca k t ,
thc hin n khi nhn phm ESC (M ASCII ca phm ESC l 27).
#include<stdio.h>
#include<conio.h>

void main()
{
int ma ;
do{
ma=getch ();
if (ma !=27)
printf ("Ma ASCII %c:%d\t", ma, ma);
}while (ma!=27) ;

getch () ;
}

Lp while kim tra iu kin trc khi thc hin lp, cn vng lp dowhile
thc hin lnh lp ri mi kim tra iu kin. Do vng lp do...while thc hin
lnh t nht mt ln.
I.8. break v continue
a. break
Dng kt thc vng lp trc tip cha n khi tha iu kin no .
V d: Cho php ngi dng nhp lin tc gi tr n cho n khi nhp m th
dng.
#include<stdio.h>
#include<conio.h>

void main()
{
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 21
while (1)
{
printf(\nNhap n: );
scanf(%d, &n);
if(n<0)
break;
}

getch () ;
}
b. continue
Dng b qua mt ln lp khi tha iu kin no .
V d: In ra mn hnh gi tr t 10 n 20 tr i s 13 v s 17.
#include<stdio.h>
#include<conio.h>

void main()
{
for(int i=10 ; i<=20; i++)
{
if(i==13||i==17)
continue;
printf(%d\t, i);
}
getch () ;
}
II. BI TP
II.1. Phng php chy tay tng bc tm kt qu chng trnh
+ Xc nh chng trnh c s dng nhng bin no.
+ Gi tr ban u ca mi bin.
+ Nhng bin no s b thay i trong qu trnh chy chng trnh th lp
thnh bng c dng sau:
Bc
(Hoc ln thc hin)
Bin 1 Bin 2 Bin n
Kt qu in ra
mn hnh
0 Gi tr 0 Gi tr 0 Gi tr 0
1 Gi tr 1 Gi tr 1 Gi tr 1
2 Gi tr 2 Gi tr 2 Gi tr 2
...


Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 22
Lu tng lnh v biu thc iu kin trong on chng trnh
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 23
V d: Cho bit kt qu ca on chng trnh sau:
void main()
{
int i, a = 4;
clrscr();
for(i = 0 ; i<a; i++)
printf(%d\n, i);
}
Chng trnh gm 2 bin i v a, ch c bin i c gi tr thay i trong qu trnh
chy chng trnh nn ta lp bng sau:
a c gi tr l 4
Bc thc hin
Gi tr
ca bin i
Kt qu in ra
mn hnh
0 0 0
1 1
0
1
2 2
0
1
2
3 3
0
1
2
3
4 4
Ti bc 4, gi tr ca i = 4 vi phm iu kin lp (i<a) nn vng
lp kt thc. Do kt qu in ra mn hnh:
0
1
2
3
II.2. Bi tp c bn
a. Cu trc if / if..else v switch
1. Cho bit kt qu ca on chng trnh sau:
int a=9, b=6;
a++;
a=a+b--;
a=a+(--b);
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 24
if(a%2==0)
printf("Gia tri cua a la chan);
printf(Tong cua a va b la: %d, a+b) ;
2. Cho bit kt qu ca on chng trnh sau:
int a=7, b=8;
a++;
a=a+(b--);
--b;
a--;
a=(--a)+(--b);
if(a%2!=0)
printf("\n a la so le");
else
printf("\n a la so chan");
printf("\na = %d",a);
3. Cho bit kt qu ca on chng trnh sau:
int x=5, y;
y=x++ + 5;
printf(x=%d, y=%d\n, x, y);
y*=6;
x=y%7;
printf(x=%d,y=%d,y/x=%d, x, y, y/x);

4. Nhp vo hai s nguyn a, b. In ra mn hnh gi tr ln nht.
5. Cho ba s a, b, c c vo t bn phm. Hy tm gi tr ln nht ca ba s
trn v in ra kt qu.
6. Cho ba s a, b, c c vo t bn phm. Hy in ra mn hnh theo th t tng
dn cc s. (Ch c dng thm hai bin ph).
7. Vit chng trnh nhp vo mt s nguyn n gm ba ch s. Xut ra mn
hnh ch s ln nht v tr no?
V d: n=291. Ch s ln nht nm hng chc (9).
8. Vit chng trnh nhp vo s nguyn n gm ba ch s. Xut ra mn hnh
theo th t tng dn ca cc ch s.
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 25
V d: n=291. Xut ra 129.
9. Nhp vo ngy, thng, nm. Kim tra xem ngy, thng, nm c hp l
hay khng? In kt qu ra mn hnh.
10. Nhp vo gi, pht, giy. Kim tra xem gi, pht, giy c hp l hay
khng? In kt qu ra mn hnh.
11. Vit chng trnh nhp vo ngy, thng, nm hp l. Cho bit nm ny c
phi l nm nhun hay khng? In kt qu ra mn hnh.
12. Vit chng trnh tnh din tch v chu vi cc hnh: tam gic, hnh vung,
hnh ch nht v hnh trn vi nhng thng tin cn c nhp t bn phm.
13. Vit chng trnh tnh tin cc TAXI. Bit rng:
-
KM u tin l 5000

.

-
200m tip theo l 1000

.

-
Nu ln hn 30km th mi km thm s l 3000

.

Hy nhp s km sau in ra s tin phi tr.

14. Nhp vo 3 s nguyn dng a, b, c. Kim tra xem 3 s c lp thnh tam
gic khng? Nu c hy cho bit tam gic thuc loi no? (Cn, vung,
u, ).
15. Vit chng trnh nhp vo s nguyn dng n. Kim tra xem n c phi l
s chnh phng hay khng? (s chnh phng l s khi ly cn bc 2 c kt
qu l nguyn).
b. Cu trc lp
16. Cho bit kt qu ca an chng trnh sau:
int a=18;
for(int i=1; i<=a; i++)
if(a%i= =0)
printf("\t %d", i);
17. Cho bit kt qu ca an chng trnh sau:
for(int i=0; i<5; i++)
{
for(int j=0; j<=i; j++)
printf(%d\t, j);
printf(\n);
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 26
}
18. Cho bit kt qu ca an chng trnh sau:
int i=10, s=0;
while(i>0)
{
if(i%2= =0)
s+=i;
else
if(i>5)
s+=2*i;
i--;
}
printf(s = %d,s);
19. Cho bit kt qu ca an chng trnh sau:
int a=18, i=1;
do{
if(a%i==0)
printf("\t %d",i);
i++;
} while(i<=a);
20. Cho bit kt qu ca an chng trnh sau:
int a=11, b=16, i=a;
while( i<b )
{
if(i%2==0)
{
printf("\t %d", i);
break;
}
i++;
}
21. Cho bit kt qu ca an chng trnh sau:
int a=10, s=0, i=0;
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 27
while( i<a )
{
i++;
if(i%2==0) continue;
else s=s+i;
}
printf("s=%d",s);
22. Cho bit kt qu ca an chng trnh sau:
int i=1,s=0;
while(1)
{
s=s+i++;
if(i%2)
i=i+2;
else
i=i+1;
if(i>20)
break;
}
printf("%d",s);
23. Vit chng trnh in ra mn hnh hnh ch nht c kch thc n m (m, n
nhp t bn phm).
V d: Nhp m=5, n=4
* * * * *
* * * * *
* * * * *
* * * * *
24. Vit chng trnh in ra mn hnh hnh ch nht rng kch thc n m (m, n
nhp t bn phm).
V d: Nhp m=5, n=4
* * * * *
* *
* *
* * * * *
25. Vit chng trnh in ra mn hnh tam gic vung cn c c cao h (h
nhp t bn phm).
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 28
V d: Nhp h=4
*
* *
* * *
* * * *
26. Vit chng trnh in ra mn hnh tam gic cn rng c cao h (h nhp t
bn phm).
V d: Nhp h=4
*
* *
* *
* * * *
27. Vit chng trnh in ra mn hnh tam gic cn c c cao h (h nhp t
bn phm).
V d: Nhp h=4
*
* * *
* * * * *
* * * * * * *
28. Vit chng trnh in ra mn hnh tam gic cn rng c cao h (h nhp t
bn phm).
V d: Nhp h=4
*
* *
* *
* * * * * * *
29. Vit chng trnh nhp s nguyn dng n. Lit k n s nguyn t u tin.
30. Vit chng trnh nhp vo hai s nguyn dng a v b. Tm c s chung
ln nht v bi s chung nh nht ca a v b.
31. Vit chng trnh nhp vo mt s nguyn n gm ti a 10 ch s (4 bytes).
In ra mn hnh gi tr nh phn ca s trn. (Hng dn: chia ly d cho 2 v
xut theo th t ngc li dng hm gotoxy, wherex, wherey).
32. Vit chng trnh m s c s ca s nguyn dng N.
V d: N=12
s c s ca 12 l 6
33. Mt s hon thin l mt s c tng cc c s ca n (khng k n) bng
chnh n. Hy lit k cc s hon thin nh hn 5000.
V d: s 6 l s han thin v tng cc c s l 1+2+3=6.
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 29
34. Nhp vo ngy, thng, nm. Cho bit l ngy th my trong nm.
35. In ra dy s Fibonaci
f
1
= f
0
=1 ;
f
n
= f
n-1
+ f
n-2
; (n>1)
II.3. Bi tp luyn tp v nng cao
36. Ci t tt c cc lu v chng 1.
37. Nhp vo ngy, thng, nm. Kim tra xem ngy, thng, nm c hp l
hay khng, nu hp l cho bit ngy sau l bao nhiu.
V d: Nhp 31/12/2003
Ngy sau 01/01/2004
38. Nhp vo ngy, thng, nm. Kim tra xem ngy, thng, nm c hp l
hay khng, nu hp l cho bit ngy trc l bao nhiu.
V d: Nhp 01/01/2003
Ngy trc 31/12/2002
39. (*) Nhp vo ngy, thng, nm ca nm 2003. Hy kim tra xem d liu c
hp l hay khng? Nu hp l hy cho bit l ngy th my trong tun.
(hai, ba, t, , CN).(Hng dn: ly ngy 01 thng 01 nm 2003 l ngy
th t lm mc).
40. Nhp vo gi, pht, giy. Kim tra xem gi, pht, giy c hp l hay
khng, nu hp l cho bit gi sau 1 giy l bao nhiu.
V d: Nhp 01:59:59
Gi sau 1 giy 02:00:00
41. Nhp vo gi, pht, giy. Kim tra xem gi, pht, giy c hp l hay
khng, nu hp l cho bit gi trc 1 giy l bao nhiu.
V d: Nhp 02:00:00
Gi trc 1 giy 01:59:59
42. Vit chng trnh in ra bng cu chng t 2 n 9.
43. (*) V hnh cnh qut sau:
Cu trc iu khin
Gio trnh Bi Tp K Thut Lp Trnh Trang 30

S dng cc hm cprintf(), textcolor(), delay(), kbhit(), thay i mu
to cm gic cho cnh qut xoay cho n khi nhn mt phm bt k.
III. KT LUN
+ Cu trc lp v r nhnh (la chn) l hai cu trc chnh hnh thnh nn
chng trnh. Da vo nhng cu trc iu khin ny ta c th xy dng
thnh nhng chng trnh phc tp hn. V vy phi nm r cch hot ng
ca nhng cu trc iu khin ny ci t ng yu cu bi ton.
+ Khi s dng phi lu iu kin thc hin hay kt thc ca mt thao tc
no .
+ Bn trong mt pht biu iu khin phi l mt lnh hay mt khi lnh
(khi lnh c t bn trong cp du ngoc {}).
+ Nhng bin khng ph thuc vo vng lp nn t bn ngoi vng lp.
+ Khi s dng cu trc iu khin lng nhau phi lu v tr m ngoc hay
ng ngoc cho hp l.
Hm con
Gio trnh Bi Tp K Thut Lp Trnh Trang 31
CHNG 3 HM CON

Trnh by cu trc ca mt chng trnh, cc bc xy dng ci t chng trnh theo
phng php th tc hm v mt s k thut lin quan.
I. TM TT L THUYT
I.1. Khi nim
Hm l mt on chng trnh c lp thc hin trn vn mt cng vic nht
nh sau tr v gi tr cho chng trnh gi n, hay ni cch khc hm l s
chia nh ca chng trnh.
I.2. V d
//Khai bo th vin hm
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<dos.h>
#include<process.h>

//Khai bo bin ton cc v nguyn mu hm
void ThayThe(char * S, char *St );
void Doc1Sector(int vt);
void Ghi1Sector(int vt);

//Hm chnh
void main()
{
unsigned char buf[512];
char S[20], St[20];
printf("Nhap chuoi can tim: ");
gets(S);
printf("Nhap chuoi thay the:");
gets(St) ;
printf("\nXin cho");
TimVaThayThe(S,St,buf);
printf("\n Thanh cong.");
getch();
}

//Ci t cc hm con
void ThayThe(char * S, char *St )
{
Hm con
Gio trnh Bi Tp K Thut Lp Trnh Trang 32
int l=strlen(St);
for(int i=0;i<l;i++)
S[i]=St[i];
}

void Doc1Sector(int vt, char buf[512])
{
if(absread(0,1,vt,buf))
{
printf("\n loi doc dia, nhan enter thoat");
getch();
exit(1);
}
}

void Ghi1Sector(int vt, char buf[512])
{
if(abswrite(0,1,vt,buf))
{
printf("\n loi ghi dia, nhan enter thoat");
getch();
exit(1);
}
}

void TimVaThayThe(char * S, char *St, unsigned char buf[])
{
for(int i=33;i<=500;i++)
{
Doc1Sector(i, buf);
char * p=strstr(buf, S);
if(p)
{
ThayThe(p, St);
Ghi1Sector(i, buf);
}
}
}
Hm con
Gio trnh Bi Tp K Thut Lp Trnh Trang 33
I.3. Cu trc mt chng trnh C

a. Khi khai bo
Bao gm cc khai bo v s dng th vin, khai bo hng s, khai bo hm
con (cc nguyn mu hm), khai bo cc bin ton cc v khai bo cc kiu
d liu t nh ngha.
b. Hm chnh (main())
Cha cc bin, cc lnh v cc li gi hm cn thit trong chng trnh.
c. Cc hm con
c s dng nhm mc ch:
Khi c mt cng vic ging nhau cn thc hin nhiu v tr.
Khi cn chia mt chng trnh ln phc tp thnh cc n th nh (hm
con) chng trnh c trong sng, d hiu trong vic x l, qun l
vic tnh ton v gii quyt vn .
d. Nguyn mu hm
<Kiu d liu ca hm> Tn hm ([ danh sch cc tham s]);
Nguyn mu hm thc cht l dng u ca hm thm du chm phy (;) vo
cui, tuy nhin tham s trong nguyn mu hm c th b phn tn.
Khi khai
bo
Hm
main()
Cc hm con
(nu c)
Hm con
Gio trnh Bi Tp K Thut Lp Trnh Trang 34
I.4. Cch xy dng mt hm con
a. Kiu d liu ca hm
Xc nh da vo kt qu ca bi ton (Output). Gm 2 loi :
void: Hm khng tr v gi tr. Nhng hm loi ny thng ri vo nhng
nhm chc nng: Nhp / xut d liu , thng k, sp xp, lit k.
void Tn_hm (danh sch cc tham s)
{
Khai bo cc bin cc b
Cc cu lnh / khi lnh hay li gi n hm khc.
}
Kiu d liu c bn (ri rc/ lin tc) hay kiu d liu c cu trc:
Kiu d liu ty theo mc ch ca hm cn tr v gi tr g thng qua vic
phn tch bi ton. Nhng hm loi ny thng c s dng trong cc
trng hp: m, kim tra, tm kim, tnh trung bnh, tng, tch,
<Kiu d liu> Tn_hm ([danh sch cc tham s])
{
<Kiu d liu> kq;
Khai bo cc bin cc b
Cc cu lnh / khi lnh hay li gi n hm khc.

return kq;
}

i vi nhng hm tr v nhiu loi gi tr cho tng trng hp c th
(chng hn nh kim tra: ng hay sai, so snh: bng , ln hn hay nh
hn, ) th cn ghi ch r gi tr tr v l g cho tng trng hp .
b. Tham s
Xc nh da vo d liu u vo ca bi ton (Input). Gm 2 loi :
Tham s khng l con tr (tham tr): Khng thay i hoc khng cn
ly gi tr mi ca tham s sau li gi hm. Tham s dng ny ch mang
ngha l d liu u vo.
Tham s con tr (tham bin): C s thay i gi tr ca tham s trong
qu trnh thc hin v cn ly li gi tr sau khi ra khi hm. ng dng
ca tham s loi ny c th l d liu u ra (kt qu) hoc cng c th
va l d liu u vo va l d liu u ra.
Hm con
Gio trnh Bi Tp K Thut Lp Trnh Trang 35
c. Tn hm
t tn theo quy c t tn trong C sao cho tn gi ng vi chc nng
hay mc ch thc hin ca hm v gi nh.
d. V d
V d 1: Vit chng trnh nhp s nguyn dng n v in ra mn hnh cc c
s ca n
Phn tch bi ton:
Input: n ( xc nh tham s)
- Kiu d liu: s nguyn dng (unsigned int).
- Gi tr n khng b thay i trong qu trnh tm c s Tham s ca
hm khng l con tr.
Output: In ra cc c s ca n ( xc nh kiu d liu hm)
- Khng tr v gi tr.
- Kiu d liu ca hm l void .
Xc nh tn hm: Hm ny dng in ra cc c s ca n nn c th t l
LietKeUocSo
Ta c nguyn mu hm:
void LietKeUocSo ( unsigned int n );

#include<conio.h>
#include<stdio.h>

//Khai bao nguyen mau ham
void LietKeUocSo ( unsigned int n );

void main()
{
unsigned int n;

printf(Nhap n = );
scanf(%u,&n);
printf("Cac uoc so cua n : " );
LietKeUocSo(n);
getch( );
}

void LietKeUocSo (unsigned int n)
{
Hm con
Gio trnh Bi Tp K Thut Lp Trnh Trang 36
for(int i=1; i<=n; i++)
printf(%u\t, i);
}

Lu cch gi hm: i vi hm c kiu d liu hm l void th khi
gi khng cn phi gn gi tr vo bin, ngc li phi gi nh trong v
d 2 (Phi khai bo tng ng kiu vi kiu d liu hm s gi v gn
gi tr tr v vo bin ).

V d 2: Vit chng trnh nhp s nguyn dng n v tnh tng
n S + + + + = L 3 2 1 , vi n>0
Phn tch bi ton:
Input: n ( xc nh tham s)
- Kiu d liu: s nguyn dng (unsigned int).
- Gi tr n khng b thay i trong qu trnh tnh tng Tham s ca
hm khng l con tr.
Output: Tng S ( xc nh kiu d liu hm)
- Tr v gi tr ca S.
- S l tng cc s nguyn dng nn S cng l s nguyn dng Kiu
tr v ca hm l unsigned int (hoc unsigned long cho trng hp gi
tr ca tng ln hn 2 bytes).
Xc nh tn hm: Hm ny dng tnh tng S nn c th t l TongS.
Ta c nguyn mu hm:
unsigned long TongS ( unsigned int n );

#include<conio.h>
#include<stdio.h>

//Khai bao nguyen mau ham
unsigned long TongS ( unsigned int n );

void main()
{
unsigned int n;
unsigned long kq;

printf(Nhap n = );
Hm con
Gio trnh Bi Tp K Thut Lp Trnh Trang 37
scanf(%u,&n);
kq = TongS ( n );
printf(Tong can tinh la: %lu , kq);
getch( );
}
unsigned long TongS (unsigned int n)
{
unsigned long S=0;
int i=1;
while(i<=n)
{
S+=i;
i++;
}
return S;
}
II. BI TP
II.1. Bi tp c bn
1. Ci t li tt c cc bi tp chng 2 theo phng php hm.
2. Vit chng trnh tnh din tch v chu vi ca hnh ch nht vi chiu di v
chiu rng c nhp t bn phm.
3. Vit chng trnh tnh din tch v chu vi hnh trn vi bn knh c nhp
t bn phm.
4. Nhp s nguyn dng n (n>0). Lit k tt c cc s nguyn t nh hn n.
5. Nhp s nguyn dng n (n>0). Lit k n s chnh phng u tin.
6. Nhp s nguyn dng n (n>0). m xem c bao nhiu s hon thin nh
hn n.
7. Nhp s nguyn dng n (0 <= n< 1000) v in ra cch c ca n.
V d: Nhp n = 105. In ra mn hnh: Mot tram le nam.
8. Vit chng trnh tnh tin thu my dch v Internet v in ra mn hnh kt
qu. Vi d liu nhp vo l gi bt u thu (GBD), gi kt thc thu
(GKT), s my thu (SoMay).
- iu kin cho d liu nhp: 6<=GBD<GKT<=21. Gi l s nguyn.
- n gi: 2500 cho mi gi my trc 17:30 v 3000 cho mi gi my
sau 17:30.
9. Vit chng trnh tnh tin lng ngy cho cng nhn, cho bit trc gi vo
ca, gi ra ca ca mi ngi.
Hm con
Gio trnh Bi Tp K Thut Lp Trnh Trang 38
Gi s rng:
- Tin tr cho mi gi trc 12 gi l 6000 v sau 12 gi l 7500.
- Gi vo ca sm nht l 6 gi sng v gi ra ca tr nht l 18 gi (Gi s
gi nhp vo nguyn).
10. Nhp vo 2 s nguyn p, q v tnh biu thc sau:
(-q/2+(p
3
/27+q
2
/4)
1/2
)
1/3
+ (-q/2 (p
3
/27+q
2
/4)
1/2
)
1/3

11. Nhp vo 3 s thc a, b, c v kim tra xem chng c thnh lp thnh 3 cnh
ca mt tam gic hay khng? Nu c hy tnh din tch, chiu di mi ng
cao ca tam gic v in kt qu ra mn hnh.
- Cng thc tnh din tch s = sqrt(p*(p-a)*(p-b)*(p-c) )
- Cng thc tnh cc ng cao: h
a
= 2s/a, h
b
=2s/b, h
c
=2s/c.
(Vi p l na chu vi ca tam gic).
12. Nhp vo 6 s thc a, b, c, d, e, f . Gii h phng trnh sau :

= +
= +
c by ax
f ey dx

13. Vit chng trnh nhp 2 s nguyn dng a, b. Tm USCLN v BSCNN ca
hai s nguyn .
14. Vit chng trnh tnh tng nghch o ca n giai tha.
15. Cho 2 s nguyn a, b. Vit hm hon v gi tr 2 s trn.
16. (*) Vit chng trnh nhp s nguyn dng n gm 5 ch s, kim tra xem
cc ch s n c phi l s i xng hay khng.
V d: i xng: 13531
Khng i xng: 13921
17. Vit chng trnh nhp s nguyn dng n gm k ch s ) 5 0 ( < k , m xem
n c bao nhiu ch s chn v bao nhiu ch s l.
18. Vit chng trnh nhp s nguyn dng n gm k ch s ) 5 0 ( < k , m xem
n c bao nhiu ch s l s nguyn t.
19. Vit chng trnh nhp s nguyn dng n gm k ch s ) 5 0 ( < k , tnh tng
cc c s dng ca n.
V d: Nhp n=6
Tng cc c s t 1 n n: 1+2+3+6=12.
20. Vit chng trnh nhp s nguyn dng n gm k ch s ) 5 0 ( < k , tm c
s l ln nht ca n.
Hm con
Gio trnh Bi Tp K Thut Lp Trnh Trang 39
V d: c s l ln nht ca 27 l 9.
21. Vit chng trnh nhp s nguyn dng n gm k ch s ) 5 0 ( < k , kim tra
xem cc ch s ca n c ton l hay ton chn khng.
22. (*) Vit chng trnh nhp s nguyn dng n gm k ch s ) 5 0 ( < k , sp
xp cc ch s ca n theo th t tng dn.
V d: Nhp n=1536
Kt qu sau khi sp xp: 1356.
II.2. Bi tp luyn tp v nng cao
23. Vit chng trnh nhp s nguyn dng n gm k ch s ) 5 0 ( < k , sau
nhp mt s nguyn x, tm v tr xut hin ca ch s c gi tr x trong n.
V d: Nhp n=1526, x=2
Kt qu: Chu so 2 o vi tri thu 3.
24. Vit chng trnh nhp s nguyn dng n gm k ch s ) 5 0 ( < k , kim tra
xem cc ch s ca n c c sp th t khng.
V d: Nhp n=1569 hoc n=8521
Kt qu: C th t.
25. Vit chng trnh nhp 2 s a, b sao cho: s ln nht trong 2 s phi l mt s
dng v chia ht cho 7. Nu nhp sai phi yu cu nhp li cho n khi
ng.
26. Vit chng trnh nhp s nguyn dng n gm k ch s
) 5 0 ( < k
, tnh gi
tr trung bnh cc ch s chn trong n.
27. (*) Vit chng trnh in ra mn hnh ngy/thng/nm ca ngy hin ti, cho
php s dng cc phm mi tn ln, xung tng hoc gim mt ngy.
28. (*) Vit chng trnh in ra mn hnh gi:pht:giy hin ti, cho php s dng
cc phm mi tn ln, xung tng hoc gim mt giy.
III. KT LUN
+ Trc khi xy dng mt hm ta phi xc nh mc ch ca hm l dng
lm g, trn c s , ta mi xc nh c cc thnh phn ca hm v xy dng
nguyn mu hm.
+ Mi hm phi thc hin mt chc nng c lp v tch bit vi cc hm khc
(khng c lng nhau).
Hm con
Gio trnh Bi Tp K Thut Lp Trnh Trang 40
+ i vi hm c gi tr tr v phi lu kiu d liu phi tng ng kiu d liu
c gi tr tr v v kiu d liu ca bin c gn khi gi hm. Trng hp hm
tr v t hai loi gi tr tr ln th phi c dng ch thch cho trng hp
tng ng khi gi hm bit c kt qu (chng hn nh tm kim, kim tra,
so snh, gi tr tr v c 2 trng hp: C hoc khng c phn t cn tm,
tha iu kin kim tra hay khng? Do vy ta phi quy c gi tr cho tng
trng hp).
+ Nn t tn hm sao cho gi nh c chc nng, t tn theo quy tc nht
nh trnh vic gi sai tn hm do ln ln gia k t hoa v thng, c du
gch ni gia cc t trong hm hay khng?
+ Khi gi hm phi truyn tham s, ng kiu d liu v ng th t ca
tham s.
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 41
CHNG 4 MNG MT CHIU

Cch khai bo d liu kiu mng, cc thao tc nhp xut, cc k thut thao tc trn
mng. ng dng cc k thut ny trong vic ci t cc hm tm kim, kim tra, xy
dng mng, tch v ghp mng.
I. TM TT L THUYT
I.1. Khi nim
Mng thc cht l mt bin c cp pht b nh lin tc v bao gm nhiu bin
thnh phn.
Cc thnh phn ca mng l tp hp cc bin c cng kiu d liu v cng tn.
Do truy xut cc bin thnh phn, ta dng c ch ch mc.
I.2. Khai bo mng
khai bo mt mng, ta c 2 cch khai bo sau :
+ Cch 1: Con tr hng
< Kiu d liu > < Tn mng > [ < S phn t ti a ca mng> ] ;
V d:
int a[100]; // Khai bao mang so nguyen a gom 100 phan tu
float b[50]; // Khai bao mang so thuc b gom 50 phan tu
+ Cch 2: Con tr
ngha: Khi ta khai bo mt mng vi kiu d liu bt k (int, float, char,)
th tn ca mng thc cht l mt hng a ch ca phn t u tin.
< Kiu d liu > *< Tn mng >;
V d :
int *p; // khai bao con tro p
int b[100];
p = b; // p tro vao phan tu 0 cua mang b
Vi cch vit nh trn th ta c th hiu cc cch vit sau l tng ng
p[i] *(p + i) b[i] *(b+i)
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 42
Lu : Khi s dng bin con tr truy xut mng, theo cch nh trn th
thc cht con tr p ch chim 2 byte b nh cha a ch m thi. to
mng cha d liu thnh phn th ta phi cp pht vng nh cho con tr p.
Dng hm : malloc, calloc trong th vin <stdlib.h> cp pht vng nh.
V d:
+ Cch 1: dng malloc
int *px; //Khai bo con tr px
px = (int *) malloc (100); //Cp pht 100 nh kiu int cho con tr px
+ Cch 2: dng calloc
int *p; //khai bo con tr p
p=(int *) calloc (100,sizeof (int)); //cp pht 10 nh mi chim 2bytes
Sau khi s dng xong th nn gii phng vng nh bng hm free
V d : free (p) ; // gii phng vng nh cho con tr p.
I.3. Truy xut phn t ca mng
Vi khi nim v cch khai bo nh trn ta c hnh dng ca mng mt chiu
nh sau:
V d : int A[5] // Khai bo mng A gm ti a 5 phn t nguyn.
Ch s 0 1 2 3 4
A[0] A[1] A[2] A[3] A[4]
V d minh ho:
Khai bo v gn gi tr cho mng
#include <conio.h>
#include <stdio.h>

void main ( )
{
clrscr ( );
int a[4] = {5,9,3,8};
for (int i = 0; i < 4 ; i++)
printf ( a [ %d ] = %d \t, i , a[i] );
getch ( );
}
i vi con tr: Ly a ch ca phn t trong mng ta dng du &
V d:
int a[7];
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 43
int *p = a[3]; //Ly a ch phn t th 3
V d :
int a[7];
int *px;
px = a; //px tr ti phn t th 0
px = px + 4; //px tr ti phn t th 4
T v d trn ta c th m hnh ho mng nh sau:
px a[0] a[1] a[2] a[3] a[4] a[5] a[6]



V d minh ho: Vit chng trnh nhp vo mng mt chiu 10 phn t kiu s
nguyn
#include <conio.h>
#include <stdio.h>

void main ( )
{
int a[10], i;
int *p;

for (i = 0 ; i < 10 ; i ++)
{
printf ( a [ %d ] = , i );
scanf ( %d, &a[i] );
}
p = a;
printf ( \n Noi dung mang vua nhap: );
for (i = 0; i < 10 ; i ++)
printf ( %d \t , *(p + i));
getch ( );
}
II. BI TP
II.1. Mt s k thut c bn
a. K thut t c hiu
K thut ny thng c p dng cho nhng bi ton kim tra hay nh
du.
Vit hm kim tra xem mng cc s nguyn c th t tng dn khng?
(Tr v 1: Nu mng tng dn, ngc li tr v 0).
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 44
int KiemTraTang (int a[ ], int n)
{
int flag = 1;
for (int i = 0; i < n-1; i ++ )
if ( a[i] > a[i+1] ) // Vi phm iu kin tng dn
{
flag = 0;
break;
}
return flag;
}
Vit hm kim tra xem trong mng cc s nguyn c tn ti s nguyn
l ln hn 100 hay khng?
(Tr v 1: Nu c tn ti s l v ln hn 100, ngc li tr v 0).
int KiemTraLe (int a[ ], int n)
{
int flag = 0;
for (int i = 0; i < n; i ++ )
if ( a[i] % 2 != 0 && a[i][j] > 100 ) //Gp phn t tho
{
flag = 1;
break;
}
return flag;
}
b. K thut t lnh canh
K thut ny thng c p dng cho nhng bi tp v tm kim, lit k
theo mt iu kin nht nh no .
Vit hm tm v tr v gi tr ln nht trong mng mt chiu cc s
nguyn.
int TimMax (int a[], int n)
{
int max, i = 1;

max = a[0];
while ( i < n )
{
if ( a[i] > max )
max = a[i] ;
i++;
}
return max;
}
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 45
II.2. Bi tp c bn
a. Nhp xut mng mt chiu
Phng php c bn
Vit chng trnh nhp xut mng mt chiu cc s nguyn.
#include <conio.h>
#include <stdio.h>
#define MAX 100

void NhapMang (int a[], int &n)
{
printf (Nhap so phan tu: );
scanf ( %d , &n);
for (int i = 0; i < n; i ++)
{
printf ( a [%d] = , i);
scanf ( %d , &a[i]);
}
}

void XuatMang (int a[], int n)
{
printf (\nNoi dung mang: );
for (int i = 0; i < n; i ++)
printf ( %d \t , a[i]);
}

void main ( )
{
clrscr ( );
int a[MAX] , n;
NhapMang (a,n);
XuatMang (a,n);
getch ( );
}
Bi tp
1. Vit chng trnh nhp xut mng mt chiu cc s thc.
2. Vit chng trnh khi to gi tr cc phn t l 0 cho mng mt chiu cc
s nguyn gm n phn t.
3. Vit chng trnh pht sinh ngu nhin mng mt chiu cc s nguyn
m.
4. Vit chng trnh pht sinh ngu nhin mng mt chiu cc s nguyn sao
cho mng c th t tng dn (Khng sp xp).
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 46
5. Vit chng trnh nhp mng cc s thc v xut cc phn t m trong
mng.
6. Vit chng trnh nhp mng cc s nguyn v xut cc phn t l c
trong mng.
7. Vit chng trnh nhp vo mng mt chiu cc s nguyn v xut ra cc
phn t chn nh hn 20.
8. Vit chng trnh nhp vo mng mt chiu cc s nguyn v xut ra mn
hnh cc phn t l s nguyn t.
9. Vit chng trnh nhp vo s nguyn n v lit k cc s nguyn t nh
hn n, nu mng khng tn ti s nguyn t no nh hn n th phi xut ra
mt cu thng bo.
10. Vit chng trnh nhp vo mng mt chiu cc s nguyn v xut ra mn
hnh cc phn t l s chnh phng nm ti nhng v tr l trong mng.
b. Tm kim trn mng mt chiu
Phng php c bn
Vit hm tm phn t c gi tr x xut hin u tin trong mng mt
chiu.
(Nu tm thy tr v v tr xut hin x, ngc li tr v -1)
int TimX (int a[], int n, int x)
{
for (int i = 0; i < n ; i ++)
if ( x==a[i] )
return i;
return -1;
}
Bi tp
11. Vit hm tm v tr phn t c gi tr x xut hin cui cng trong mng.
12. Vit hm tm v tr ca phn t nh nht trong mng cc s nguyn.
13. Vit hm tm v tr ca phn t ln nht trong mng cc s nguyn.
14. Vit hm in v tr cc phn t nguyn t trong mng cc s nguyn.
15. Vit hm in v tr cc phn t nguyn t ln hn 23.
16. Vit hm tm v tr phn t m u tin trong mng. Nu khng c phn t
m tr v 1.
17. Vit hm tm v tr phn t m ln nht trong mng.
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 47
18. Vit hm tm v tr phn t dng u tin trong mng. Nu khng c
phn t m tr v 1.
19. Vit hm tm v tr phn t dng b nht trong mng.
20. Vit hm in cc phn t l bi ca 3 v 5.
21. Vit hm tm s chn cui cng c trong mng, nu khng tn ti s chn
hm tr v -1 .
22. Vit hm tm s l ln nht c trong mng, nu khng tn ti s l hm tr
v -1.
23. Vit hm tm v i ch phn t ln nht vi phn t nh nht trong
mng.
24. Nhp vo X. Vit hm in ra mn hnh nhng phn t c gi tr t 1 n X
c trong mng.
25. Vit chng trnh nhp vo mt dy s a gm n s thc (
100 n
), nhp
vo dy s b gm m s thc ( 100 m ).
In ra nhng phn t ch xut hin trong dy a m khng xut hin trong
dy b.
In ra nhng phn t xut hin c hai dy.
c. m Tn sut
Phng php c bn
Vit hm m cc phn t chia ht cho 5 trong mng cc s nguyn.
int Dem (int a[], int n )
{
int dem = 0;
for (int i = 0; i < n ; i++ )
if ( a[i] % 5 == 0 )
dem++;
return dem;
}
Bi tp
26. Vit hm m cc phn t m, dng trong mng.
27. Vit hm m cc phn t chn, l trong mng.
28. Vit hm m s ln xut hin ca phn t x trong mng.
29. Vit hm m cc phn t nh hn x trong mng.
30. Vit hm m cc phn t l s nguyn t trong mng.
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 48
31. Vit hm m cc phn t l s hon thin trong mng.
32. Vit hm m cc phn t l bi ca 3 v 5 trong mng cc s nguyn.
d. Tnh tng Trung bnh c iu kin
Phng php c bn
Vit hm tnh tng cc phn t trong mng.
long TinhTong (int a[], int n )
{
long tong = 0;
for (int i = 0; i < n; i++ )
tong = tong + a[i] ;
return tong;
}
Vit hm tnh gi tr trung bnh cc phn t c gi tr m trong mng.
i vi hm tnh trung bnh c iu kin phi lu khi chia gi tr (C th
mng khng c phn t no tho iu kin, nu ta chia tc l chia cho 0).
float TrungBinhAm (int a[], int n )
{
long tong = 0;
int spt=0;
for (int i = 0; i < n; i++ )
if( a[i]<0 )
{
tong = tong + a[i] ;
spt++;
}
if(spt==0)
return 0;
return 1.0*tong/spt;
}
Bi tp
33. Vit hm tnh tng cc phn t chn trong mng.
34. Vit hm tnh tng cc phn t l trong mng cc s nguyn.
35. Vit hm tnh tng cc phn t nguyn t trong mng.
36. Vit hm tnh tng cc phn t nm v tr chn trong mng cc s
nguyn.
37. Vit hm tnh tng cc phn t nm v tr nguyn t trong mng.
38. Vit hm tnh tng cc phn t chia ht cho 5 c trong mng.
39. Vit hm tnh tng cc phn t cc i trong mng cc s nguyn (phn t
cc i l phn t ln hn cc phn t xung quanh n).
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 49
V d : 1 5 2 6 3 5 1 8 6
40. Vit hm tnh tng cc phn t cc tiu trong mng cc s nguyn ( phn
t cc tiu l phn t nh hn cc phn t xung quanh n ).
V d : 6 4 2 9 5 3 7 1 5 8
41. Vit hm tnh tng cc phn t l bi ca 3 v 5 trong mng cc s
nguyn.
42. Vit hm tnh tng cc phn t l s hon thin trong mng cc s nguyn.
43. Vit hm tnh gi tr trung bnh ca cc s hon thin trong mng cc s
nguyn.
e. Sp xp
K thut c bn
Vit hm sp xp mng theo th t tng dn.
void HoanVi (int &a, int &b)
{
int tam = a;
a = b;
b = tam;
}

void SapTang (int a[], int n)
{
for (int i = 0; i < n-1 ; i++)
for (int j = i+1; j < n; j++)
if (a[i] > a [j])
HoanVi (a[i], a[j]);
}
Bi tp
44. Vit hm sp xp mng theo th t gim dn.
45. Vit hm sp xp mng theo th t tng dn ca cc phn t l s nguyn
t.
46. Vit hm sp xp cc phn t l tng dn.
47. Vit hm sp xp cc phn t chn gim dn.
48. Vit hm sp xp cc phn t chn nm bn tri theo th t tng dn cn
cc phn t l bn phi theo th t gim dn.
49. Vit hm sp xp cc phn t m gim dn t tri sang phi, phn t
dng tng dn t phi sang tri.
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 50
f. Xo
K thut c bn
Duyt mng t tri sang phi . Xut pht t v tr cn xo tin hnh di ln lt
cc phn t v pha trc cho n khi kt thc mng, sau gim kch thc
mng.
Vn t ra l tm v tr cn xa theo iu kin bi ton ri thc hin xa.
Vit hm xo phn t u tin ca mng.
void XoaDau (int a[], int &n)
{
for (int i = 0; i < n-1 ; i++)
a[i] = a[i+1];
n--;
}
Vit hm xo phn t ti v tr (vitri) cho trc trong mng.
void XoaTaiViTri (int a[], int &n, int vitri)
{
for (int i = vitri; i < n-1 ; i++)
a[i] = a[i+1];
n--;
}
Bi tp
50. Vit hm xo phn t ti v tr l trong mng.
51. Vit hm xo phn t c gi tr ln nht trong mng.
52. Nhp vo gi tr X. Vit hm xo tt c cc phn t c gi tr nh hn X.
53. Nhp vo gi tr X. Vit hm xo phn t c gi tr gn X nht.
g. Chn
K thut c bn
Duyt mng t phi sang tri. Xut pht t cui mng tin hnh y ln lt
cc phn t v pha sau cho n v tr cn chn, chn phn t cn chn vo v
tr chn v tng kch thc mng.
Trc khi chn ta phi xc nh v tr cn chn theo iu kin bi ton.
Thm phn t c gi tr X vo cui mng.
void ThemCuoi (int a[], int &n, int X)
{
a[n]=X;
n++;
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 51
}
Chn phn t c gi tr X vo mng ti v tr cho trc
void ChenX (int a[], int &n, int X, int vitri)
{
for (int i = n; i >vitri ; i--)
a[i] = a[i-1] ;
a[vitri] = X;
n++;
}
Bi tp
54. Vit hm chn phn t c gi tr X vo v tr u tin ca mng.
55. Vit hm chn phn t c gi tr X vo pha sau phn t c gi tr ln nht
trong mng.
56. Vit hm chn phn t c gi tr X vo trc phn t c gi tr l s
nguyn t u tin trong mng.
57. Vit hm chn phn t c gi tr X vo pha sau tt c cc phn t c gi
tr chn trong mng.
h. Tch / ghp mng
K thut tch c bn
Cho mng a kch thc n (n chn). Tch mng a thnh 2 mng b v c sao
cho: b c phn t u ca mng a, phn t cn li a vo mng c.
void TachMang(int a[], int n, int b[], int &m, int c[], int &l)
{
int k=n/2;

m=l=0;
for(int i=0; i<k; i++)
{
b[m++]=a[i];
c[l++]=a[k+i]
}
}
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 52
K thut ghp c bn
Cho 2 mng s nguyn a v b kch thc ln lt l n v m. Vit chng
trnh ni mng b vo cui mng a.
void NoiMang(int a[], int &n, int b[], int m)
{
for(int i=0; i<m; i++)
a[n+i]=b[i];
n=n+m;
}
Cho 2 mng s nguyn a v b kch thc ln lt l n v m. Vit chng
trnh ni xen k (an xen) ln lt cc phn t mng a v b vo mng c.
Cch thc hin: a ln lt tng phn t ca mng a v mng b vo
mng c, tng ch s tng ng. Nu mt trong hai mng ht trc th chp
tt c cc phn t cn li ca mng cha ht vo mng c.
t i l ch s ca mng a; j: ch s ca mng b v k l ch s ca mng c.
void NoiMang(int a[], int &n, int b[], int m, int c[], int &k)
{
int i=0, j=0;
k=0;
while(i<n&&j<m)
{
c[k++]=a[i++];
c[k++]=b[j++];
}
while(i<n)
c[k++]=a[i++];
while(j<m)
c[k++]=b[j++];
}
Bi tp
58. Vit chng trnh tch 1 mng cc s nguyn thnh 2 mng a v b, sao
cho mng a cha ton s l v mng b cha ton s chn.
V d: Mng ban u: 1 3 8 2 7 5 9 0 10
Mng a: 1 3 7 5 9
Mng b: 8 2 10
59. Cho 2 mng s nguyn a v b kch thc ln lt l n v m. Vit chng
trnh ni 2 mng trn thnh mng c theo nguyn tc chn u mng v l
cui mng.
V d: Mng a: 3 2 7 5 9
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 53
Mng b: 1 8 10 4 12 6
Mng c: 6 12 4 10 2 8 3 1 7 5 9
II.3. Bi tp luyn tp v nng cao
60. Vit chng trnh nhp vo mng A gm n phn t, trong qu trnh nhp
kim tra cc phn t nhp vo khng c trng, nu trng thng bo v
yu cu nhp li.
61. Vit hm tnh tng ca tng dy con gim c trong mng.
62. (*) Cho mng cc s nguyn a gm n phn t ( 30000 n ) v s dng k
( n k ). Hy ch ra s hng ln th k ca mng.
V d: Mng a: 6 3 1 10 11 18
k = 2
Kt qu: 10
63. (*) Cho 2 dy A, B cc s nguyn (kch thc dy A nh hn dy B). Hy
kim tra xem A c phi l con ca B hay khng?
64. Vit hm lit k cc b 4 s a, b, c, d trong mng cc s nguyn (c t nht
4 phn t v i mt khc nhau) sao cho a + b = c + d.
65. (*) Vit chng trnh tnh trung bnh cng ca cc tng cc dy tng dn
c trong mng cc s nguyn.
V d: 1 2 3 4 2 3 4 5 6 4 5 6 => TB = 15.
66. Vit chng trnh tnh tng tt c cc phn t xung quanh trn mng cc
s nguyn. (Phn t xung quanh l hai phn t bn cnh cng lai bng
chnh n (V d: 1 3 2 1,2 l hai phn t xung quanh ca 3).
V d : 1 3 2 5 3 9 6 tng 17
67. (**) Vit chng trnh nhp vo hai s ln a, b nguyn ( a, b c t 20 ch
s tr ln). Tnh tng, hiu, tch, thng ca hai s trn.
68. Vit hm tnh tng cc phn t l s Amstrong (s Amstrong l s c c
im nh sau: s c k k s, tng ca cc lu tha bc k ca cc k s
bng chnh s .
V d: 153 l s c cc k s 1
3
+5
3
+3
3
= 153 l mt s Amstrong).
69. Vit hm tm v xa tt c cc phn t trng vi x trong mng mt chiu
cc s nguyn, nu khng tn ti phn t x trong mng th tr v -1.
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 54
70. Vit hm xo tt c nhng phn t trng nhau trong dy ch gi li mt
phn t trong .
V d: 1 6 2 3 2 4 2 6 5 1 6 2 3 4 5
71. (**) Vit hm xo nhng phn t sao cho mng kt qu c th t tng dn
v s ln xo l t nht.
72. Cho dy a gm n s nguyn c th t tng dn. Nhp vo mt phn t
nguyn X, vit hm chn X vo dy sao cho dy vn c th t tng dn
(khng sp xp).
73. Vit chng trnh tm s l nh nht ln hn mi s chn c trong mng.
74. Vit hm tm gi tr chn nh nht nh hn mi gi tr l trong mng cc
s nguyn.
75. Vit hm tm phn t xut hin nhiu nht trong mng cc s nguyn.
76. Vit chng trnh m v lit k cc mng con tng dn trong mng mt
chiu cc s nguyn.
V d: 6 5 3 2 3 4 2 7 cc dy con tng dn l 2 3 4 v 2 7
77. Vit chng trnh tm mng con tng dn c tng ln nht trong mng mt
chiu.
78. (*) Vit chng trnh nhp vo mt dy s a gm n s nguyn (n <= 100).
Tm v in ra dy con tng di nht
V d : Nhp dy a : 1 2 3 6 4 7 8 3 4 5 6 7 8 9 4 5
Dy con tng di nht : 3 4 5 6 7 8 9
79. (**) Vit chng trnh tch 1 mng cc s nguyn thnh 2 mng a v b,
sao cho kt qu thu c l:
Mng a cha ton s l tng dn.
Mng b cha ton s chn gim dn.
(Khng dng sp xp)
Hng dn: Tm v tr chn thch hp khi trch phn t t mng ban u.
V d: Mng ban u: 9 3 8 2 7 5 1 0 10
Mng a: 1 3 5 7 9
Mng b: 10 8 2
80. (**) Vit chng trnh in ra tam gic Pascal (dng mng mt chiu).
81. Vit chng trnh nhp vo dy s a gm n s thc ( n <= 100 ), nhp vo
dy s b gm m s thc ( m <= 100 ).
Hy sp xp hai dy theo th t tng dn.
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 55
(*) Trn 2 dy trn thnh dy c sao cho dy c vn c th t tng.
Xut dy a, b, c ra mn hnh.
82. (*) Cho mng C c n phn t ( n < 200 ), cc phn t l cc ch s trong
h m c s 16 (Hexa) (iu kin mi phn t <= n ). Hy tch mng C
ra cc mng con theo iu kin sau: cc mng con c gii hn bi hai
ln xut hin th hai ca con s trong dy.
V d: 123A4518B23 c cc dy con l123A451, 23A4518B2,
23A4518B23
83. (**) Cho hai s nguyn dng A, B. Hy xc nh hai s C, D to thnh t
hai s A, B sao cho C l s ln nht, D l s nh nht. Khi gch i mt s
ch s trong C (D), th cc s cn li gi nguyn to thnh A, cc ch s
b i gi nguyn to thnh B.
V d: A = 52568, B = 462384 -> C = 54625682384, D = 45256236884.
84. Vit chng trnh nhp vo dy s a gm n s nguyn ( n <= 100 ).
Hy o ngc dy .
V d: Nhp a: 3 4 5 2 0 4 1
Dy sau khi o: 1 4 0 2 5 4 3
(*) Hy kim tra xem dy cho c th t cha (dy c gi l th
t khi l dy tng hoc dy gim ).
85. Cho mng A c n phn t hy cho bit mng ny c i xng hay khng.
86. (**) Hy vit chng trnh pht sinh ngu nhin mng cc s nguyn gm
10.000 phn t, mi phn t c gi tr t 0 n 32.000 v xy dng hm
thng k s ln xut hin cc phn t trong mng, sau cho bit phn t
no xut hin nhiu ln nht.
V d: Mng: 5 6 11 4 4 5 4
5 xuat hien 2 lan
6 xuat hien 1 lan
11 xuat hien 1 lan
4 xuat hien 3 lan

4 xuat hien nhieu lan nhat
87. Cho mng A c n phn t. Nhp vo s nguyn k ( 0 k ), dch phi xoay
vng mng A k ln.
V d: Mng A: 5 7 2 3 1 9
Nhp k = 2
Dch phi xoay vng mng A: 1 9 5 7 2 3
Mng mt chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 56
III. KT LUN
+ D liu kiu mng dng cho vic biu din nhng thng tin c cng kiu d
liu lin tip nhau.
+ Khi ci t bi tp mng mt chiu nn xy dng thnh nhng hm chun
dng li cho cc bi tp khc.
+ Cc thao tc trn mng u theo quy tc nht nh, chng ta c th ng dng
mng trong vic biu din s ln, dng bng tra, kh qui,
Chui k t
Gio trnh Bi Tp K Thut Lp Trnh Trang 57
CHNG 5 CHUI K T

Chui k t l trng hp c bit ca mng mt chiu. Chng ny m t mt s hm
th vin thao tc trn chui v cc k thut ci t x l trn chui.
I. TM TT L THUYT
I.1. Khi nim
Chui k t l mt dy cc phn t, mi phn t c kiu k t.
Lu : Chui k t c kt thc bng k t \0. Do khi khai bo di ca
chui lun lun khai bo d 1 phn t cha k t \0.
V d: char S[5]=CNTT //khai bo chui c 5 phn t kiu char v gn dy
k t CNTT v chui.
C N T T \0
Phn t S[0] Phn t S[1] Phn t S[2] Phn t S[3] Phn t S[4]
Chui rng l chui cha c k t no trong mng k hiu
I.2. Khai bo chui
khai bo mt chui, ta c 2 cch khai bo sau :
+ Cch 1: Con tr hng
char < Tn chui > [ < S k t ti a ca chui > ] ;
V d: char chuoi[25];
ngha khai bo 1 mng kiu k t tn l chuoi c 25 phn t (nh vy ti a
ta c th nhp 24 k t v phn t th 25 cha k t kt thc chui \0 )
+ Cch 2: Con tr
char *< Tn chui >;
V d : char *chuoi;
I.3. Cc thao tc trn chui
a. Nhp chui
C php : char *gets(char *s);
Nhn cc k t nhp t phm cho n khi nhn phm Enter v a vo s.
V d:
Chui k t
Gio trnh Bi Tp K Thut Lp Trnh Trang 58
void main()
{
char chuoi[80];
printf("Nhap vao chuoi:");
gets(chuoi);
printf("Chuoi vua nhap la: %s\n", chuoi);
}
b. Xut chui
C php : int puts(const char *s);
Xut chui s ra mn hnh.
V d:
void main()
{
char chuoi[] = "Vi du xuat chuoi\n";
puts(string);
}
c. Cc hm th vin (string.h)
STT TN HM CHC NNG V D
1 int strlen(char s[]); Tr v di ca chui s.
char *s = "Borland
International";
printf("Do dai s: %d\n",
strlen(s));
Kt qu: Do dai s: 21
2
strcpy(char dest[],
char src[]);
Sao chp ni dung chui src
vo chui dest.
char dest[10];
char *src = "abcdefghi";
strcpy(dest, src);
printf("%s\n", dest);
Kt qu: abcdefghi
3
strncpy(char dest[],
char src[], int n);
Chp n k t t chui src sang
chui dest. Nu chiu di src <
n th hm s in khong trng
cho n k t vo dest.
char dest[4];
char *src = "abcdefghi";
strncpy(dest, src, 3);
printf("%s\n", dest);
Kt qu: abc
4
strcat(char
s1[],char s2[]);
Ni chui s2 vi chui s1.
char *s1 = Khoa ;
char *s2 = "CNTT";
strcat(s1, s2);
printf("%s\n", s1);
Kt qu: Khoa CNTT
5
strncat(char
s1[],char s2[],int n)
Ni n k t u tin ca chui
s2 vo chui s1.
char *s1 = Khoa ;
char *s2 = "CNTT";
strncat(s1, s2, 2);
printf("%s\n", s1);
Kt qu: Khoa CN
6
Int strcmp(char
s1[],char s2[])
So snh 2 chui s1 v s2 theo
nguyn tc th t t in.
Phn bit ch hoa v thng.
char *s1 = abcd;
char *s2 = "abCD";
if(strcmp(s1, s2)==0)
Chui k t
Gio trnh Bi Tp K Thut Lp Trnh Trang 59
Tr v:
0 : nu s1 bng s2.
>0: nu s1 ln hn s2.
<0: nu s1 nh hn s2.
printf("Giong nhau");
else
printf(Khac nhau);
Kt qu: Khac nhau
7
int strncmp(char
s1[],char s2[], int
n)
Tng t nh strcmp(), nhng
ch so snh n k t u tin
ca hai chui.
char *s1 = abcd;
char *s2 = "abef";
if(strncmp(s1, s2, 2)==0)
printf("Giong nhau");
else
printf(Khac nhau);
Kt qu: Giong nhau
6
int stricmp(char
s1[],char s2[])
Tng t nh strcmp(), nhng
khng phn bit hoa thng.
char *s1 = abcd;
char *s2 = "abCD";
if(stricmp(s1, s2)==0)
printf("Giong nhau");
else
printf(Khac nhau);
Kt qu: Giong nhau
7
int strnicmp(char
s1[],char s2[], int
n);
Tng t nh stricmp(),
nhng ch so snh n k t u
tin ca hai chui.
char *s1 = aBcd;
char *s2 = "Abef";
if(strnicmp(s1, s2, 2)==0)
printf("Giong nhau");
else
printf(Khac nhau);
Kt qu: Giong nhau
8
char *strchr(char
s[], char c);
Tm ln xut hin u tin ca
k t c trong chui s. Tr v:
NULL: nu khng c.
a ch c: nu tm thy.
char s[15];
char *ptr, c = 'm';
strcpy(s, "Vi du tim ky tu");
ptr = strchr(s, c);
if (ptr)
printf("Ky tu %c tai: %d", c,
ptr-s);
else
printf("Khong tim thay");
t qu: Ky tu m tai: 8
9
char *strstr(char
s1[], char s2[]);
Tm s xut hin u tin ca
chui s2 trong chui s1. Tr
v:
NULL: nu khng c.
Ngc li: a ch bt u
chui s2 trong s1.
char *s1 = "Borland
International"; char *s2 =
"nation", *ptr;
ptr = strstr(s1, s2);
printf("Chuoi con: %s\n", ptr);
Kt qu: Chuoi con: national
10
char *strtok(char
s1[], char s2[]);
Nu s2 c xut hin trong
s1: Tch chui s1 thnh hai
chui: Chui u l nhng
k t cho n khi gp chui
s2 u tin, chui sau l
nhng k t cn li ca s1
sau khi b i chui s2
xut hin trong s1.


char input[16] = "abc,d";
char *p;
// Lay chuoi dau
p = strtok(input, ",");
if (p) printf("S11: %s\n", p);
/*Lay chuoi con lai, tham so dau
la NULL*/
p = strtok(NULL, ",");
if (p) printf("S12: %s\n", p);
Kt qu:
S11: abc
S12: d
Chui k t
Gio trnh Bi Tp K Thut Lp Trnh Trang 60
Nu s2 khng xut hin
trong s1 th kt qu chui
tch vn l s1.
Lu : Cch truy xut cc k t tng t nh mng mt chiu.
d. V d
Nhp vo mt chui k t, xut ra mn hnh chui b o ngc th t
cc k t.
V d: Nhp vo: Tran minh thai. Xut ra mn hnh: iaht hnim narT
#include<stdio.h>
#include<string.h>
#include<conio.h>

void DaoChuoi(char *s1, char *s2)
{
int l=strlen(s1);
for(int i=0; i<l; i++)
s2[i]=s1[l-i-1];

s2[i]='\0';
}

void main()
{
char *s1, *s2;
clrscr();
printf("\nNhap vao chuoi ky tu: ");
gets(s1);
DaoChuoi(s1, s2);
printf("\nKet qua sau khi dao nguoc chuoi: %s", s2);
}
II. BI TP
II.1. Bi tp c bn
1. Cho bit kt qu ca an chng trnh sau:
char input[20]=Truong cao dang CNTT, *p, *temp;
strcpy(temp, input);
do
{
p = strtok(temp, " ");
printf("%s\n",p);
Chui k t
Gio trnh Bi Tp K Thut Lp Trnh Trang 61
p = strtok(NULL, "");
strcpy(temp, p);
}while(p!=NULL);
printf(Chuoi temp: %s \n Chuoi input: %s, temp, input);
2. Cho bit kt qu ca an chng trnh sau:
char s1[20]=Truong cao dang CNTT, s1[10]=Tp. HCM, *input, *s3;
strcpy(input, s1); strcpy(s3,aeiou); strcat(input, s2);
int n=strlen(input), k=0;
printf(Chuoi: %s,input);
for(int i=0; i<n; i++)
{
if(strchr(s3, input[i]))
k++;
}
printf(\nKet qua: %d, k);
3. Vit chng trnh nhp vo mt chui k t, m s k t c trong chui.
4. Vit chng trnh m c bao nhiu khong trng trong chui.
5. Vit chng trnh nhp vo mt chui, hy loi b nhng khong trng tha
trong chui.
6. Vit chng trnh nhp vo hai chui s1 v s2, ni chui s2 vo s1. Xut
chui s1 ra mn hnh.
7. i tt c cc k t c trong chui thnh ch thng (khng dng hm
strlwr).
8. i tt c cc k t trong chui sang ch in hoa (khng dng hm struppr).
9. Vit chng trnh i nhng k t u tin ca mi t thnh ch in hoa.
10. Vit chng trnh i ch xen k 1 ch hoa v 1 ch thng.
V d: nhp ABCDEfgh i thnh AbCdEfGh
11. Vit chng trnh o ngc cc k t trong chui .
V d: nhp ABCDE, xut ra mn hnh l:EDCBA
12. Vit chng trnh tm kim 1 k t xem c trong chui hay khng, nu c
xut ra v tr ca t .
13. Vit 1 chng trnh m mt k t xut hin bao nhiu ln trong chui.
Chui k t
Gio trnh Bi Tp K Thut Lp Trnh Trang 62
14. Vit chng trnh tm kim tn trong chui h tn. Nu c th xut ra l tn
ny nhp ng, ngc li thng bo l nhp sai.
15. Vit chng o v tr ca t u v t cui.
V d: nhp bo an co xuat ra co an bo
16. Vit hm ct chui h tn thnh chui h lt v chui tn.
V d: chui h tn l:Nguyn Vn A ct ra 2 chui l chui h
lt:Nguyn Vn,chui tn l:A
17. Nhp mt chui bt k, sau hi ngi dng cn tch bt u t u trong
chui tr v sau.
V d: Nhp chui S1:trng Cao ng Cng Ngh Thng tin. Ngi
nhp mun tch bt u t ch Cng th s xut ra chui Cng Ngh
Thng Tin ra mn hnh.
18. Vit hm kim tra xem chui c i xng hay khng?.
19. Vit hm tra xem trong chui c k t s hay khng nu c tch ra thnh
mt mng s ring.
20. Nhp mt chui bt k, yu cu nhp 1 k t mun xa. Thc hin xa tt c
nhng k t trong chui.
21. Vit chng trnh tm kim xem k t no xut nhin nhiu nht trong
chui.
22. Vit 1 chng trnh xo mt t no trong chui.
V d: Chui ban u: CAO DANG CNTT
Nhp: CNTT, v kt qu xut ra:CAO DANG
II.2. Bi tp luyn tp v nng cao
23. i cc t u cu sang ch hoa v nhng t khng phi u cu sang
ch thng.
V d: nGuYen vAN a i thnh: Nguyn Vn A
24. (*) Vit chng trnh o ngc th t cc t c trong chui
V d: Nhp Truong CD CNTT TpHCM
Xut ra mn hnh l: TpHCM CNTT CD Truong
25. Nhp 1 chui bt k, lit k xem mi k t xut hin my ln.
26. Vit hm kim tra xem trong 2 chui c bao nhiu k t ging nhau.
27. Vit chng trn mnh chy t tri qua phi mn hnh.
Chui k t
Gio trnh Bi Tp K Thut Lp Trnh Trang 63
28. Vit 1 chng trnh chn 1 t bt c v tr no m ngi dng yu cu.
29. (*) Vit chng trnh nhp vo mt chui m xem chui c bao nhiu t.
Cc t cch nhau bng khong trng, du chm cu: du chm (.), du phy
(,), du chm phy (;), du hi (?) v du chm than (!).
30. (**) Vit chng trnh hin th mt chui k t. Chng trnh cho php di
chuyn du nhy sang tri, sang phi, ln dng hay xung dng bng phm
mi tn, chn hay xo k t ti v tr du nhy.
III. KT LUN
+ Cng ging nh kiu mng mt chiu, thao tc truy xut cc phn t trn
chui hon ton tng t. Bn cnh , kiu d liu ny cn c ci t sn
mt s hm th vin rt hu ch nn trong qu trnh thao tc trn chui nn
khi ci t ta c gng tn dng ti a nhng hm lin quan.
+ Khng nn s dng hm scanf() nhp chui trong trng hp chui d
liu nhp vo c cha khong trng.
+ Nu nhp chui pha sau hm scanf() nn chn hm fflush(stdin) hoc hm
flushall() gia scanf v gets() xa vng m, trnh trng hp chng
trnh b qua hm gets() do trong vng m cn lu k t xung dng ca
phm ENTER.
+ Khi thao tc trn chui lu phi m bo chui c kt thc bng k t
kt thc \0.
Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 64
CHNG 6 MNG HAI CHIU

y l kiu d liu dng biu din d liu kiu bng, kiu d liu ny rt thch hp
cho cc bi ton lin quan n th, biu din nh,
I. TM TT L THUYT
I.1. Khi nim
Mng hai chiu thc cht l mng mt chiu trong mi phn t ca mng l
mt mng mt chiu, v c truy xut bi hai ch s dng v ct.
T khi nim trn ta c th a ra mt khi nim v mng nhiu chiu nh sau:
mng c t hai chiu tr ln gi l mng nhiu chiu.
I.2. Khai bo mng
T khi nim trn ta c c php khai bo mng hai chiu nh sau:
Cch 1: Con tr hng
< Kiu d liu > < Tn mng > [ < S dng ti a > ][ < S ct ti a> ];
V d:
int A[10][10]; // Khai bo mng 2 chiu kiu int gm 10 dng, 10 ct
float b[10][10]; // Khai bo mng 2 chiu kiu float gm 10 dng, 10 ct
Cch 2 : Con tr
< Kiu d liu > **<Tn mng>;
V d :
int **A ; // Khai bo mng ng 2 chiu kiu int
float **B ; // Khai bo mng ng 2 chiu kiu float
Tng t nh mng mt chiu, s dng ta phi cp pht vng nh cho n
bng malloc hoc calloc v hu sau khi dng bng free
V d : Khai bo mng cc s nguyn A c kch thc 5x6
int **A;
A = ( int **) malloc (5) ;
for ( int i = 0 ; i < 5 ; i ++ )
A[i]=(int *) malloc (6) ;
I.3. Truy xut phn t ca mng
truy xut cc thnh phn ca mng hai chiu ta phi da vo ch s dng v
ch s ct.
Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 65
V d:
int A[3][4] = { {2,3,9,4} , {5,6,7,6} , {2,9,4,7} };
Vi cc khai bo nh trn ta c :
A[0][0] = 2; A[0][1] = 3;
A[1][1] = 6; A[1][3] = 6;
Vi v d trn ta c hnh dng ca mt ma trn nh sau
0 1 2 3
0 2 3 9 4
1 5 6 7 6
2 2 9 4 7

Lu : Khi nhp liu cho mng hai chiu, nu l mng cc s nguyn th ta
nhp liu theo cch thng thng. Nhng nu l mng cc s thc th ta phi
thng qua bin trung gian.
V d :
float a[10][10]; // Mang so thuc a
float tmp; // Bien trung gian tmp
scanf (%f, &tmp); // Nhap lieu cho bien trung gian
a[2][2] = tmp; // Gan du lieu vao phan tu a[2][2]
I.4. Ma trn vung v cc khi nim lin quan
a. Khi nim
L ma trn c s dng v s ct bng nhau.
b. Tnh cht ca ma trn vung
ng cho loi 1


o ng cho loi 1 bao gm ng cho chnh v nhng ng
cho song song vi ng cho chnh. Trong ng cho chnh
l ng cho c :
ch s dng = ch s ct
Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 66
o Truy xut cc phn t trn ng cho loi 1 : truy xut cc
phn t trn cc ng cho loi 1 ta c th da vo ch s dng v
ch s ct nh sau :
ct dng = hng s

V d : Cho ma trn vung A
(n x n).
Gi (i
o
, j
o
) l to im xut
pht, ta c th duyt ng cho xut pht t (i
o
, j
o
) nh sau :
for ( i = i
o
, j = j
o
; i < n ; i ++, j ++ )
printf (%4d,A[i][j]);

ng cho loi 2:


o ng cho loi 2 bao gm ng cho ph v nhng ng song
song vi n. Trong ng cho ph l ng cho c:
ch s ct + ch s dng = s dng ( hoc s ct )
o Truy xut cc phn t trn ng cho loi 2 : truy xut cc
phn t trn cc ng cho loi 1 ta c th da vo ch s dng v
ch s ct nh sau :
ct + dng = hng s

V d: Cho ma trn vung A(n x n). Gi (io, jo) l to im xut
pht, ta c th duyt ng cho xut pht t (io, jo) nh sau :
for ( i = io , j = jo ; i < n && j > = 0 ; i ++ , j --)
printf (%4d,A[i]][j]);
II. BI TP
n gin trong vic khai bo ma trn, ta nh ngha kiu ma trn cc phn t
vi kiu d liu bt k nh sau:
#define MAX 100
typedef <kiu d liu> MATRAN[MAX][MAX];
V d: Khai bo ma trn cc s nguyn a.
#define MAX 100
Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 67
typedef int MATRAN[MAX][MAX];
MATRAN a;
II.1. Mt s k thut c bn
Phng php nhp xut ma trn
void Nhap (MATRAN a, int &d, int &c )
{
printf (\nNhap so dong: );
scanf ( %d, &d );
printf (\nNhap so cot: );
scanf (%d, &c );
for ( int i = 0; i < d; i ++ )
for (int j = 0; j < c; j ++)
{
printf ( a[%d][%d] = , i, j );
scanf (%d, &a[i][j]);
}
}

void Xuat (MATRAN a, int d, int c)
{
printf (\nNoi dung ma tran:\n);
for (int i = 0; i < d; i++)
{
for (int j = 0; j < c; j++)
printf ( \t %d , a[i][j] );
printf (\n);
}
}
K thut t c hiu
Vit hm kim tra xem trong ma trn cc s nguyn c tn ti cc s
nguyn l ln hn 100 khng?
int KiemTraLe (MATRAN a, int d, int c)
{
int flag = 0; //tra ve 1 neu co nguoc lai tra ve 0

for (int i = 0; i < d; i ++ )
for (int j = 0; j < c; j++)
if ( a[i][j] % 2 != 0 && a[i][j] > 100 )
{
flag = 1;
break;
}
return flag;
}
Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 68
K thut t lnh canh
Vit hm tm phn t nh nht trong ma trn.
int Min (MATRAN a, int d, int c )
{
int min = a[0][0];
for ( int i = 0 ; i < d ; i ++ )
for (int j = 0 ; j < c ; j ++)
if ( a[i][j] < min )
min = a[i][j];
return min;
}
Phng php tnh tng
Vit hm tnh tng cc phn t trong ma trn.
long Tong (MATRAN a, int d, int c)
{
long tong = 0;

for ( int i = 0; i < d; i ++ )
for ( int j = 0; j < c; j ++)
tong + = a[i][j];
return tong;
}
Phng php sp xp
Vit hm sp xp ma trn tng dn t trn xung di v t tri sang phi
khng dng mng ph.
void SapTang(MATRAN a, int d, int c)
{
for (int i = 0; i <= d*c-2; i ++)
for (int j = 0; j <= d*c-1; j ++)
if (a[i/c][i%c] < a[j/c][j%c])
{
int tmp = a[i/c][i%c] ;
a[i/c][i%c] = a[j/c][j%c] ;
a[j/c][j%c] = tmp ;
}
}
Phng php m
Vit hm m cc phn t chn trong ma trn.
int DemChan (MATRAN a, int d, int c)
{
int dem = 0;

Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 69
for ( int i = 0 ; i < d ; i ++)
for ( int j = 0 ; j < c ; j ++)
if ( a[i][j] % 2 = = 0 )
dem ++;
return dem;
}
II.2. Bi tp c bn
a. Bi tp nhp xut
1. Vit hm nhp ma trn cc s nguyn dng (nhp sai bo li v khng cho
nhp).
2. Vit hm nhp/ xut ma trn cc s thc.
3. Vit hm in ra nhng phn t c k s tn cng l 5.
4. Vit chng trnh in ra cc phn t nm trn 2 ng cho.
5. Vit hm in ra cc phn t nm pha trn ng cho ph ca ma trn
vung cc s nguyn.
6. Vit hm in ra cc phn t nm pha di ng cho ph ca ma trn
vung cc s nguyn.
7. Vit hm in ra cc phn t nm pha trn ng cho chnh ca ma trn
vung cc s nguyn.
8. Vit hm in ra cc phn t nm pha di ng cho chnh ca ma trn
vung cc s nguyn.
9. Vit chng trnh khi to gi tr cc phn t l ngu nhin cho ma trn cc
s nguyn kch thc
n m
.
10. Vit hm to ma trn a cc s nguyn gm 9 dng 14 ct. Trong phn t
a[i][j] = i * j
11. Vit hm in tam gic Pascal vi chiu cao h.
V d : h = 5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
b. Bi tp tnh tng
12. Vit hm tnh tng cc phn t trn cng mt dng.
Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 70
13. Vit hm tnh tng cc phn t trn cng mt ct.
14. Vit hm tnh tng cc phn t chn c trong ma trn.
15. Vit hm tnh tng cc phn t nm trn ng cho chnh ca ma trn
vung.
16. Vit hm tnh tng cc phn t l s nguyn t c trong ma trn.
17. Vit hm tnh tng cc s hon thin trong ma trn cc s nguyn.
18. Vit hm tnh tng cc gi tr ln nht trn mi dng.
19. Vit hm tnh gi tr trung bnh ca cc phn t nh nht trn mi ct.
20. Vit hm tnh tng cc gi tr nh nht nm trn tng ng cho loi 2.
21. Vit hm tm ng cho c tng ln nht trong cc ng cho loi 1.
c. Bi tp tm kim
22. Vit hm tm v tr phn t ln nht trong ma trn cc s nguyn.
23. Vit hm tm v tr phn t nh nht trong ma trn cc s nguyn.
24. Vit hm tm v tr phn t chn cui cng trong ma trn cc s nguyn.
25. Vit hm tm phn t m l ln nht trong ma trn.
26. Vit hm tm phn t chn dng v nh nht trong ma trn.
27. Vit hm tm s hon thin u tin trong ma trn cc s nguyn.
28. Vit hm tm s hon thin ln nht trong ma trn cc s nguyn.
29. Vit hm tm v tr phn t nguyn t cui cng trong ma trn cc s
nguyn.
30. Vit hm tm phn t ln nht nm trn ng cho chnh ca ma trn
vung.
31. Vit hm in cc s nguyn t nm trn ng cho ph ca ma trn vung.
32. Vit hm tm trong 2 ma trn cc s nguyn, nhng phn t ging nhau.
33. Vit hm tm phn t nh nht trn mi ng cho loi 2 ca ma trn.
34. Vit hm tm v lit k nhng phn t cc i trong ma trn (mt phn t
c coi l cc i khi n ln hn cc phn t xung quanh n).
35. Vit hm tm dng c tng ln nht trong ma trn cc s thc.
36. Vit hm tm ct c tng nh nht trong ma trn cc s nguyn.
d. Bi tp m
37. Vit hm m cc gi tr m, dng trong ma trn cc s thc.
38. Vit hm m cc gi tr chn, l trong ma trn cc s nguyn.
Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 71
39. Vit hm m s ln xut hin ca phn t x trong ma trn cc s thc.
40. Vit hm m cc gi tr nh hn x trong ma trn cc s thc.
41. Vit hm m cc phn t nguyn t trong ma trn cc s nguyn.
42. Vit hm n cc phn t nguyn t trn ng cho chnh ca ma trn
vung cc s nguyn.
43. Vit hm m cc gi tr chn trn ng cho chnh ca ma trn vung cc
s nguyn.
44. Vit hm m cc gi tr l bi ca 3 v 5 trn ng cho chnh ca ma
trn cc s nguyn.
45. Vit hm m cc gi tr nguyn t trn 2 ng cho (chnh, ph) ca ma
trn vung cc s nguyn.
46. Vit hm m cc gi tr cc i trong ma trn cc s nguyn.
47. Vit hm m cc gi tr cc tiu trong ma trn cc s nguyn.
48. Vit hm m cc cc tr trong ma trn cc s nguyn (mt phn t c
coi l cc tr khi n l gi tr cc i hay cc tiu).
49. Vit hm m cc gi tr l s hon thin trong ma trn cc s nguyn.
e. Bi tp sp xp
50. Vit hm sp xp ma trn theo th t tng dn t trn xung di v t tri
qua phi theo phng php dng mng ph.
Hng dn: ma trn sang mng mt chiu, sp xp trn mng mt chiu
theo th t tng dn, sau chuyn ngc mng mt chiu thnh ma trn
kt qu.
51. Vit hm sp xp ma trn theo th t gim dn t trn xung di v t tri
sang phi.
52. Vit hm sp xp cc dng trn ma trn theo th t tng dn.
53. Vit hm sp xp cc ct trn ma trn theo th t gim dn.
54. Vit hm sp xp ma trn theo ng ziczc ngang.
V d :

55. Vit hm sp xp ma trn theo ng ziczc cho
Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 72
V du :

56. Vit hm sp xp ma trn theo ng xon c t ngoi vo trong theo chiu
kim ng h.
V d :

57. Cho ma trn vung, vit hm sp xp tng dn cc phn t nm trn cc
ng cho song song vi ng cho chnh.
58. Vit chng trnh nhp mt ma trn vung cc s nguyn, v thc hin
nhng cng vic sau :
Sp xp cc phn t nm trn cc ng cho loi 1 tng dn
Sp xp cc phn t nm trn cc ng cho loi 2 gim dn.
Sp xp vi iu kin: cc phn t trn ng cho chnh tng, cc phn
t trn cc ng cho song song vi ng cho chnh gim.
f. Bi tp Thm Xo Thay th
59. Vit hm xo mt dng i trn ma trn.
60. Vit hm xo mt ct j trn ma trn.
61. Vit hm xo dng c tng ln nht trn ma trn.
62. Vit hm hon v dng c tng ln nht vi dng c tng nh nht.
63. Vit hm tm v thay th cc phn t chn trong ma trn bng c s nh
nht ca n.
64. Vit hm thay th nhng phn t c gi tr x thnh phn t c gi tr y trong
ma trn (x , y nhp t bn phm).
Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 73
II.3. Bi tp luyn tp v nng cao
65. Vit chng trnh tnh tng, tch ca hai ma trn cc s nguyn.
66. Vit hm kim tra xem ma trn vung cc s nguyn c i xng qua ng
cho chnh hay khng.
67. Vit hm kim tra xem trong ma trn vung cp n c hng no trng nhau
hay khng, nu c th ch r nhng hng no. (Trng gi tr v v tr).
68. Vit chng trnh nhp vo ma trn vung kch thc n x n (
100 2 n
).
Hy vit hm thc hin nhng cng vic sau :
In ra cc phn t trn 4 ng bin ca ma trn.
Tnh tng cc phn t trn bin.
69. (*) Vit chng trnh xoay ma trn cc s thc 90
0
ngc chiu kim ng
h.
V d:

70. Vit chng trnh dch phi xoay vng mt ct trong ma trn cc s thc.
71. Vit chng trnh dch xung xoay vng mt dng trong ma trn cc s
thc.
72. (*) Cho ma trn A ( n m ) cc s nguyn hy pht sinh ma trn B sao cho B
l ma trn lt ngc ca ma trn A.
V d :

73. (**) Cho ma trn A ( n m ) hy pht sinh ma trn B ( n m ) sao cho phn
t B (i, j) l trung bnh cng ca cc phn t trong hnh vung 3x3 tm ti
(i,j) ca A.
V d :

Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 74
74. (**) Cho ma trn cc s nguyn dng A ( n m ) . Hy xy dng ma trn B
( n m ). Sao cho phn t B ( i, j ) l s ln nht trong vung 3 x 3 tm ti
(i, j) ca A.
V d :

75. (**) Cho ma trn A ( n m ). Hy xy dng ma trn B ( n m ) vi phn t
B(i,j) c xc nh theo qui tc sau: ti v tr (i, j) trn mng A k hai tia
vung gc vi nhau, to thnh vi trc honh mt gc 45
0
t trn xung
di; B(i, j) l tng ca tt c cc s ca vng mt phng to bi hai tia ny
v cc cnh ca bng.
V d :

76. (**) Cho ma trn vung A ( n n ). Hy xy dng mng B ( n n ) bng cch:
phn t B (i, j) l s ln nht trong tam gic vung v t A (i, j) ti ng
cho chnh.
V d :

77. (*) Vit chng trnh hin th ng h in t (gm gi pht), vi gi ly t
h thng v ng h c cp nht theo pht.
Hng dn: To 1 ma trn gi tr gm 0 hoc 1, v tr no cn hin th th
gn gi tr l 1, ngc li c gi tr l 0. Sau mi pht cp nht li ma trn
v hin th ln mn hnh.
V d: 01 gi 25 pht

Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 75
78. Nhp vo mng hai chiu gm n dng v m ct cc s nguyn. Hy tm
phn t ln nht trn mi dng v ng thi nh nht trn mi ct, hoc ln
nht trn mi ct v ng thi nh nht trn mi dng. C bao nhiu phn
t nh th?
V d:

79. Vit chng trnh to ngu nhin mt ma trn cc s nguyn (0 -> 50), tm
nhng phn t cc i (l phn t ln hn cc phn t xung quanh).
V d :

80. (**) Cho ma trn cc s nguyn A n m ) 3 , 3 ( m n . Hy tm ma trn con
(3x3) c tng ln nht.
V d :

81. Nhp ma trn vung cp n n (n < 10). In ra cc phn t ca ma trn ny
theo hng ca ng cho chnh.
V d : n = 4

82. (**) Hy in cc s t 1 n n
2
vo ma trn cp n (n > 2), ch xt trng
hp n l s l vi tnh cht P l tng cc s bng nhau.
Hng dn : Ma phng ca mt bng vung cp n, trong mi nhn mt
gi tr sao cho, mi hng, mi ct v mi ng cho u tho mn mt tnh
cht P no cho trc.
Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 76
V d : Vi n = 5

83. (*) Vit hm in ma trn cc s nguyn dng theo qui lut c m t nh
sau : cc phn t pha trn ng cho ph l gi tr bnh phng ca cc
gi tr 2 1 n , cc gi tr t ng cho ph tr xung l cc s nguyn
t. Ma trn c sp xp nh v d bn di.
V d : n = 5

84. Cho ma trn vung a cp n ( n l, 15 3 n ), mi phn t u c gi tr
nguyn dng. Hy xy dng hm kim tra xem ma trn a c phi l ma
phng hay khng?
85. (**) Vit chng trnh gii bi ton 8 hu. Hy t 8 con hu trn bn c
8x8 sao cho chng khng n nhau (2 hu n nhau khi cng hng, cng ct
v cng nm trn ng cho).
Hng dn:
Dng ma trn 8x8 lu bn c. Mi c 3 trng thi :
C hu 1
trng 0
khng dc i -1
86. (**) Vit chng trnh gii bi ton m i tun. Hy i con m 64 lt i
trn bn c 8x8 sao cho mi ch i qua mt ln (xut pht t mt bt k)
Hng dn :
ng ti mt trn bn c con m c th i c 1 trong 8 hng sau .
Mng hai chiu
Gio trnh Bi Tp K Thut Lp Trnh Trang 77


O O
O O

O O
O O

Khai bo 8 hng i ca m nh sau:
typedef struct DIEM
{
int x, y;
};
DIEM huongdi[8]={{-2,-1},{-2,1},{-1,2},{1,2},{2,1},{2,-1},{1,-2},{-1,-2}};
Trong mi thnh phn ca huongdi l lch ca dng v ct so vi v
tr ca con m.
V d: huongdi[0] (tc i n v tr O nh hnh v) c lch 2 dng v 1
ct. (Gi tr m biu th lch v bn tri ct hay hng ln ca dng).
Chn v tr i k tip sao cho v tr phi gn vi bin hay gc nht (tc s
ng i c th i l t nht).
87. Vit chng trnh gii bi ton Taci. Cho ma trn vung 3x3 gm cc s
nguyn t 0 -> 8 trong 0 l trng. Bi ton t ra l hy a ma trn
mt trng thi u v trng thi ch, mi ln ch dch chuyn c 1 .
V d : Trng thi u Trng thi ch
1 3 0 1 2 3
8 2 5 => 8 0 4
7 4 6 7 6 5
III. KT LUN
+ Kiu d liu mng hai chiu c ng dng rng ri trong cc bi ton v tm
ng i trong th, x l nh, x l nhng d liu dng bng,
+ Lu khi nhp mng hai chiu cc s thc phi thng qua 1 bin trung gian.
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 78
CHNG 7 KIU D LIU C CU TRC

Cung cp c ch cho php khai bo cc kiu d liu mi gii quyt theo yu cu ca
bi ton da vo nhng kiu d liu c bn c ci t sn trong ngn ng lp trnh.
I. TM TT L THUYT
I.1. Khi nim
Cu trc (struct) thc cht l mt kiu d liu do ngi dng nh ngha bng
cch gom nhm cc kiu d liu c bn c sn trong C thnh mt kiu d liu
phc hp nhiu thnh phn.
I .2. nh ngha kiu d liu
C php
struct < tn cu trc >
{
Cc kiu d liu thnh phn ;
};
Ngoi ra ta c th dng t kho typedef nh ngha mt tn mi cho kiu d
liu c.
C php
typedef struct < tn cu trc > < tn mi >;

V d1: Kiu d liu DATE gm cc thnh phn:
Th (thu): chui c ti a 4 k t.
Ngy (ngay): s nguyn 1 byte.
Thng (thang): s nguyn 1 byte.
Nm (nam): s nguyn 2 bytes.
Ta nh ngha DATE nh sau:
struct DATE
{
char thu[5];
unsigned char ngay;
unsigned char thang;
int nam;
};
typedef struct DATE d;

Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 79
Kiu d liu c cu trc c th lng vo nhau.
V d 2: nh ngha kiu d liu ca hc sinh HOCSINH gm:
M s hc sinh (MSHS): chui c ti a 5 k t.
H tn (hoten): chui c ti a 30 k t.
Ngy thng nm sinh (ngaysinh): kiu DATE.
a ch (diachi): chui c ti a 50 k t.
Gii tnh (phai): chui c ti a 3 k t.
im trung bnh (diemtb): s thc.
Ta nh ngha kiu HOCSINH nh sau:
struct DATE
{
char thu[5];
unsigned char ngay;
unsigned char thang;
int nam;
};

typedef struct HOCSINH
{
char MSHS[6];
char hoten[31];
struct DATE ngaysinh;
char diachi[51];
unsigned char phai[4];
float diemtb;
};

Khi nh ngha kiu d liu struct lng nhau, ta cn lu : Kiu d liu c
s dng phi khai bo pha trn.

I.3. Khai bo
Khi ta nh ngha kiu d liu tc l ta c mt kiu d liu mi, mun s dng ta
phi khai bo bin. C php khai bo kiu d liu cng ging nh cch khai bo
ca cc kiu d liu chun.
struct < tn cu trc > < tn bin > ;
V d :
struct DATE x ; // Khai bao bien x co kieu du lieu DATE
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 80
Tuy nhin nu ta nh ngha struct c dng t kho typedef th ta c th khai bo
trc tip m khng cn t kho struct.
V d :
DATE x ; // Khai bao bien x co kieu DATE

*Bin con tr kiu cu trc: Ngoi cch khai bo nh trn ta c th khai bo
theo kiu con tr nh sau
struct < tn cu trc > *< tn bin > ;
s dng ta cng phi cp pht vng nh ging nh kiu d liu chun.
V d :
DATE *y; // Khai bao con tro y kieu cau truc DATE
y = ( DATE * ) malloc ( sizeof ( DATE )) ;
I.4. Truy xut
truy xut mt thnh phn d liu no bn trong cu trc ta c 2 trng hp
truy xut nh sau :
Bin x l mt bin cu trc thng thng, ta dng ton t du chm .
C php :
< Tn cu trc >.< Bin thnh phn >;
V d :
DATE x ; // khai bao bien x kieu DATE
x.ngay = 5 ; // gan ngay bang 5
Bin x l mt bin con tr, ta dng ton t mi tn -> (Gm du tr v
du ln hn >).
C php :
< Tn cu trc > -> < Bin thnh phn >;
V d :
DATE *x ; // khai bao bien x kieu con tro DATE
x -> ngay = 5 ; // gan ngay bang 5
i vi kiu d liu c struct lng nhau phi truy cp n thnh phn cui
cng c kiu d liu c bn.
V d: Gi s, c kiu HOCSINH nh trn
HOCSINH hs; // khai bao bien hs kieu HOCSINH
Mun in hc sinh A sinh vo thng my ta phi truy cp nh sau:
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 81
printf(Thang sinh cua hoc sinh A la: %d,(hs.ngaysinh).thang);
I.5. V d minh ho
Vit chng trnh nhp vo to hai im trong mt phng v tnh tng hai to
ny.
#include <conio.h>
#include <stdio.h>

typedef struct DIEM //khai bao mot kieu du lieu DIEM gom toa do x va y
{
int x;
int y;
};

void Nhap (DIEM &d)
{
printf (\nNhap vao tao do diem\n);
printf (Tung do : );
scanf (%d, & d. x);
printf (Hoanh do : );
scanf (%d, & d.y);
}

void Xuat (DIEM d)
{
printf (\nToa do diem : (%d , %d),d.x,d.y);
}

DIEM Tong (DIEM d1,DIEM d2)
{
DIEM temp;
temp.x = d1.x + d2.x ;
temp.y = d1.y + d2.y ;
return Temp;
}

void main ()
{
DIEM A , B, AB; //khai bao 3 diem A, B, AB;
clrscr ();
Nhap ( A );
Xuat ( A );
Nhap ( B );
Xuat ( B );
printf (\n Tong cua hai diem vua nhap la : );
AB = Tong ( A, B);
Xuat ( AB );
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 82
getch ();
}
I.6. Mng cu trc
Cch khai bo tng t nh mng mt chiu hay ma trn (Kiu d liu by
gi l kiu d liu c cu trc).
Cch truy cp phn t trong mng cng nh truy cp trn mng mt chiu
hay ma trn. Nhng do tng phn t c kiu cu trc nn phi ch nh r
cn ly thnh phn no, tc l phi truy cp n thnh phn cui cng c
kiu l d liu c bn (xem li bng cc kiu d liu c bn) .
I.7. Nguyn tc vit chng trnh c mng cu trc
Do kiu d liu c cu trc thng cha rt nhiu thnh phn nn khi vit
chng trnh loi ny ta cn lu :
Xy dng hm x l cho mt kiu cu trc.
Mun x l cho mng cu trc, ta g xem l & ng hm x l cho m
Do ki ng cbpT@5 00 10ADE c1 I"4 (JpXy d ch
V46B5( )Tj/TT3/TT0 1 Tf0.0023 80Tw 0.544 3w 0.447 0 Tf0.00 104Tj-0.0553 1.286 01>TjETyf0./C409088C4..08 23 6 re0 BT Td4 Td(a r)Tj/C2_0 1f0 Tc 0/TTw 3.023/TTw f6265 4.0058c 0 m4[(: CTjF1>Tj0 1 Tf04Tf0.0003 Tc 0.0354 Tc 0.212336(ng hc, ta g)Tj/C2_0 1 1060 Tc 0 Tw 3.742 0 Td<04D1>T4Tf0.0003 Tc 0.0 d908c 0.2123363ng hc, ta g)Tj2C2_0 1 1070 Tc 0 T353.631 0p h<04D1>T4Tf0.0003 Tc 0.0 Tc55 Tc -32.c55ng hc, ta g
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 83
char diachi[51];
unsigned char phai[4];
float diemtb;
};

void NhapNamSinh(DATE &d);
void XuatNamSinh(DATE d);
void Nhap1HS (HOCSINH &hs);
void Xuat1HS (HOCSINH hs);
void NhapDSHS(HOCSINH lh[], int &n);
void XuatDSHS(HOCSINH lh[], int n);
int DemHSLenLop(HOCSINH lh[], int n);

void main()
{
HOCSINH lh[MAX]; //Khai bo mng lh gm c ti a 50 hc sinh
int n, sohsdau;
NhapDSHS(lh, n);
XuatDSHS(lh, n);
sohsdau = DemHSLenLop(lh, n);
printf(\nSo luong hoc sinh duoc len lop la: %d, sohsdau);
getch();
}

void NhapNamSinh(DATE &d)
{
printf(\nNhap vao ngay: );
scanf(%u, &d.ngay);
printf(\nNhap vao thang: );
scanf(%u, &d.thang);
printf(\nNhap vao nam: );
scanf(%d, &d.nam);
}

void XuatNamSinh(DATE d)
{
printf(%02u / %02u / %4d, d.ngay, d.thang, d.nam);
}

void Nhap1HS(HOCSINH &hs)
{
float d;

lushall(); //Xoa vung dem
printf(\nNhap ma so hoc sinh: );
gets(hs.MSHS);
printf(\nNhap ho ten hoc sinh: );
gets(hs.hoten);
printf(\nNhap ngay thang nam sinh: );
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 84
flushall(); //Xoa vung dem
NhapNamSinh(hs.ngaysinh);
printf(\nNhap vao dia chi: );
flushall(); //Xoa vung dem
gets(hs.diachi);
printf(\nPhai: );
gets(hs.phai);
printf(\nNhap vao diem trung binh: );
flushall(); //Xo vng m
scanf(%f, &d);//Nhp vo bin tm d sau gn vo hs.diemtb
hs.diemtb=d;
}

void NhapDSHS(HOCSINH lh[], int &n)
{
printf(\nNhap vao so luong hoc sinh: );
scanf(%d, &n);
for(int i=0; i<n; i++)
{
printf(\nNhap vao thong tin cua hoc sinh thu %d:\n, i+1);
Nhap1HS(lh[i]); //Goi ham nhap thong tin 1 hoc sinh
}
}

void Xuat1HS(HOCSINH hs)
{
printf(\nMa so hoc sinh: %s, hs.MSHS);
printf(\nHo ten hoc sinh: %s, hs.hoten);
printf(\nNgay thang nam sinh: );
XuatNamSinh(hs.ngaysinh);
printf(\nDia chi: %s, hs.diachi);
printf(\nPhai: %s, hs.phai);
printf(\nDiem trung binh: %2.2f, hs.diemtb);
}

void XuatDSHS(HOCSINH lh[], int n)
{
for(int i=0; i<n; i++) { Lunp1, is f 0.11s.6 113 4.94 re f 0 g BT /TT2 1 Tf -0.0009 Tc 1.1334 Tw 13.02 0 0 13.02 12.
3du883+ 84ozu26a02 002 12.
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 88
for(int i=0; i<n; i++)
{
XuatPS(dsps[i]);
printf("\t");
}
}

int KiemTra(PHANSO ps)
{
if(ps.mau==0)
return 0;
return 1;
}

int USCLN(int a, int b)
{
a=abs(a);
b=abs(b);
while(a!=b)
{
if(a>b)
a=a-b;
else
b=b-a;
}
return a;
}

PHANSO RutGon(PHANSO ps)
{
int us;
if(ps.tu==0)
return ps;

us=USCLN(ps.tu, ps.mau);
ps.tu=ps.tu/us;
ps.mau=ps.mau/us;
return ps;
}

PHANSO NghichDao(PHANSO ps)
{
PHANSO kq;
kq.tu=ps.mau;
kq.mau=ps.tu;
return kq;
}

PHANSO Nhan(PHANSO ps1, PHANSO ps2)
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 89
{
PHANSO kq;
kq.tu=ps1.tu*ps2.tu;
kq.mau=ps1.mau*ps2.mau;
kq=RutGon(kq);
return kq;
}

PHANSO Chia(PHANSO ps1, PHANSO ps2)
{
PHANSO kq;
kq=Nhan(ps1, NghichDao(ps2));
return kq;
}

PHANSO Tru(PHANSO ps1, PHANSO ps2)
{
PHANSO kq;
kq.tu=ps1.tu*ps2.mau-ps1.mau*ps2.tu;
kq.mau=ps1.mau*ps2.mau;
kq=RutGon(kq);
return kq;
}

PHANSO Cong(PHANSO ps1, PHANSO ps2)
{
PHANSO kq;
kq.tu=ps1.tu*ps2.mau+ps1.mau*ps2.tu;
kq.mau=ps1.mau*ps2.mau;
kq=RutGon(kq);
return kq;
}

int SoSanh(PHANSO ps1, PHANSO ps2)
{
ps1=RutGon(ps1);
ps2=RutGon(ps2);
if(ps1.tu==ps2.tu&&ps1.mau==ps2.mau)
return 0;
if(ps1.tu*ps2.mau>ps2.tu*ps1.mau)
return 1;
return -1;
}

PHANSO TimMax(PHANSO dsps[], int n)
{
PHANSO max;
max=dsps[0];
for(int i=1; i<n; i++)
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 90
if(SoSanh(dsps[i], max)==1)
max=dsps[i];
return max;
}

PHANSO TongCacPS(PHANSO dsps[], int n)
{
PHANSO s=dsps[0];
for(int i=1; i<n; i++)
{
s=Cong(s, dsps[i]);
}
return s;
}

PHANSO TichCacPS(PHANSO dsps[], int n)
{
PHANSO p=dsps[0];
for(int i=1; i<n; i++)
{
p=Nhan(p, dsps[i]);
}
return p;
}

void NghichDaoCacPS(PHANSO dsps[], int n)
{
for(int i=0; i<n; i++)
{
dsps[i]=NghichDao(dsps[i]);
}
}

Kt qu v d khi chy chng trnh:
Nhap so luong phan so: 5

Nhap vao phan so thu 1:
Nhap tu so: 1
Nhap mau so: 3

Nhap vao phan so thu 2:
Nhap tu so: 7
Nhap mau so: 4

Nhap vao phan so thu 3:
Nhap tu so: 9
Nhap mau so: 7

Nhap vao phan so thu 4:
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 91
Nhap tu so: 5
Nhap mau so: 6

Nhap vao phan so thu 5:
Nhap tu so: 4
Nhap mau so: 7

Mang cac phan so vua nhap: 1/3 7/4 9/7 5/6 4/7
Phan so co gia tri lon nhat: 7/4
Tong gia tri cac phan so co trong mang: 401/84
Tich gia tri cac phan so co trong mang: 5/14
Mang phan so sau khi nghich dao cac phan tu: 3 4/7 7/9 6/5 7/4
II. BI TP
II.1. Bi tp c bn
1. Vit chng trnh s dng con tr cu trc hin th gi, pht, giy ra mn
hnh, v tnh khong cch gia 2 mc thi gian.
2. Vit chng trnh s dng con tr cu trc th hin ngy, thng, nm ra mn
hnh, v tnh khong cch gia 2 ngy.
3. Vit chng trnh khai bo kiu d liu th hin mt s phc. S dng kiu
ny vit hm tnh tng, hiu, tch ca hai s phc.
4. Vit chng trnh khai bo kiu d liu biu din mt phn s. Hy vit
hm thc hin nhng cng vic sau:
Tnh tng, hiu, tch, thng hai phn s.
Rt gn phn s.
Qui ng hai phn s.
So snh hai phn s.
5. Vit chng trnh khai bo kiu d liu biu din mt hn s. Hy vit
hm thc hin nhng cng vic sau :
i hn s sang phn s
Tnh tng, tch hai hn s
6. Vit chng trnh khai bo kiu d liu biu din mt im trong h ta
0xy . Hy vit hm thc hin cc cng vic sau:
Tm nhng im i xng ca n qua tung , honh , to tm.
Hy tnh tng, hiu, tch ca hai im trong mt phng to 0xy.
Tnh khong cch gia hai im.
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 92
7. Cho mt hnh tr c cc thng tin sau: BanKinh (bn knh hnh tr kiu s
thc), ChieuCao (chiu cao hnh tr kiu s thc). Hy thc hin cc cng
vic sau.
Nhp d liu cho hnh tr trn.
Tnh din tch xung quanh, din tch ton phn, th tch hnh tr.
II.2. Bi Tp Luyn Tp
8. Vit chng trnh to mt mng cc s phc. Hy vit hm tnh tng, tch
cc s phc c trong mng.
9. Vit chng trnh to mt mng cc phn s. Hy vit hm thc hin cc
cng vic sau :
Tnh tng tt c cc phn s (kt qu di dng phn s ti gin)
Tm phn s ln nht, phn s nh nht.
Sp xp mng tng dn.
10. Vit chng trnh khai bo kiu d liu STACK (c ch LIFO). Vit hm
lm nhng cng vic sau :
Kim tra STACK rng
Kim tra STACK y
Thm phn t vo STACK
Ly phn t ra khi STACK
11. T chc d liu qun l sinh vin bng cu trc mu tin trong mt mng
N phn t, mi phn t c cu trc nh sau:
- M sinh vin.
- Tn.
- Nm sinh.
- im ton, l, ho, im trung bnh.
Vit chng trnh thc hin nhng cng vic sau:
Nhp danh sch cc sinh vin cho mt lp hc.
Xut danh sch sinh vin ra mn hnh.
Tm sinh vin c im trung bnh cao nht.
Sp xp danh sch lp theo th t tng dn ca im trung bnh.
Sp xp danh sch lp theo th t gim dn ca im ton.
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 93
Tm kim v in ra cc sinh vin c im trung bnh ln hn 5 v khng
c mn no di 3.
Tm sinh vin c tui ln nht.
Nhp vo tn ca mt sinh vin. Tm v in ra cc thng tin lin quan
n sinh vin (nu c).
12. T chc d liu qun l danh mc cc b phim VIDEO, cc thng tin lin
quan n b phim ny nh sau:
- Tn phim (ta phim).
- Th loi (3 loi : hnh s, tnh cm, hi).
- Tn o din.
- Tn in vin nam chnh.
- Tn din vin n chnh.
- Nm sn xut.
- Hng sn xut
Vit chng trnh thc hin nhng cng vic sau :
Nhp vo b phim mi cng vi cc thng tin lin quan n b phim
ny.
Nhp mt th loi: In ra danh sch cc b phim thuc th loi ny.
Nhp mt tn nam din vin. In ra cc b phim c din vin ny ng.
Nhp tn o din. In ra danh sch cc b phim do o din ny dn
dng.
13. Mt th vin cn qun l thng tin v cc u sch. Mi u sch bao gm
cc thng tin sau : MaSSach (m s sch), TenSach (tn sch), TacGia (tc
gi), SL (s lng cc cun sch ca u sch). Vit chng trnh thc hin
cc chc nng sau:
Nhp vo mt danh sch cc u sch (ti a l 100 u sch)
Nhp vo tn ca quyn sch. In ra thng tin y v cc sch c tn
, nu khng c th tn ca quyn sch th bo l :Khng Tm Thy.
Tnh tng s sch c trong th vin.
14. Vit chng trnh to mt mng danh sch cc my tnh ca mt ca hng,
thng tin ca mt my tnh bao gm :
- Loi my
- Ni sn xut
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 94
- Thi gian bo hnh
Vit hm nhp mt dy cc loi my tnh c thng tin nh trn.
Hy vit hm thng k xem c bao nhiu my c thi gian bo hnh l 1
nm.
In ra danh sch cc my tnh c xut x t M.
15. lp rp mt my vi tnh hon chnh cn phi c ti thiu 10 linh kin loi
A v c th lp b sung thm vo khong ti a 8 linh kin loi B. Ti mt
ca hng vi tnh cn qun l bn hng cc loi linh kin ti ca hng. Thng
tin v mt loi linh kin gm c: Tn linh kin, quy cch , loi, n gi loi
1 ( cht lng tt s nguyn), n gi loi 2 ( cht lng thng s
nguyn ). Vit chng trnh thc hin nhng cng vic sau :
Nhp vo thng tin v cc linh kin c ca hng.
Xut danh sch cc linh kin nhp theo th t tng dn ca loi linh
kin v tn linh kin.
Cho bit c 10 linh kin loi A cn thit lp rp my hay cha?
16. Mt ca hng cn qun l cc mt hng, thng tin mt mt hng bao gm:
- M hng.
- Tn mt hng.
- S lng.
- n gi.
- S lng tn.
- Thi gian bo hnh (tnh theo n v thng).
Hy nhp vo mt danh sch cc mt hng.
Tm mt hng c s lng tn nhiu nht.
Tm mt hng c s lng tn t nht.
Tm mt hng c gi tin cao nht.
In ra nhng mt hng c thi gian bo hnh ln hn 12 thng.
Sp xp cc mt hng theo th t tng dn ca s lng tn.
17. Vit chng trnh qun l h s nhn vin trong mt cng ty, chng trnh
thc hin nhng cng vic sau :
- H v tn.
- Phi.
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 95
- Ngy sinh.
- a ch.
- Lng c bn.
- Bo him x hi.
- Thng.
- Pht.
- Lng thc lnh = lng c bn + thng BH x hi pht.
Nhp vo h s ca cc nhn vin trong cng ty.
Xut danh sch cc nhn vin theo lng thc lnh gim dn bng 2 cch
sau :
- Cp pht vng nh tnh.
- Cp pht vng nh ng.
18. (*) Vit chng trnh qun l lp hc ca mt trng. Cc thng tin ca
mt lp hc nh sau :
- Tn lp.
- S s.
- Danh sch cc sinh vin trong lp.
Nhp vo danh sach cc lp vi thng tin yu cu nh trn.
In danh sch cc lp c trn 5 sinh vin c im trung bnh loi gii.
Tm lp c nhiu sinh vin nht.
Tm lp c t sinh vin nht.
Tm sinh vin c im trung bnh cao nht.
Tm lp c s lng sinh vin t im trung bnh loi gii nhiu nht.
19. Vit chng trnh qun l v tu, thng tin mt v tu nh sau :
- Ngy gi khi hnh, ngy gi n.
- Ga i, ga n.
- Loi tu, loi ch ngi ( ngi, nm, cng, mm).
- S toa, s gh.
Vit hm nhp vo danh sch cc v tu.
In danh sch cc v tu c ga n l Hu.
In danh sch cc v tu c ga n l H Ni v i ngy 8/6/2005.
m xem c bao nhiu khch i tu loi ch ngi l nm cng.
Kiu d liu c cu trc
Gio trnh Bi Tp K Thut Lp Trnh Trang 96
20. Vit chng trnh tnh tin in hng thng ca cc h gia nh, thng tin
cc khch hng nh sau :
- K thu, t ngyn ngy.
- Tn khch hng, m khch hng.
- a ch.
- in nng tiu th (Kwh).
Nhp vo danh sch cc khch hng.
Xut danh sch ho n theo th t tng dn ca in nng tiu th.
Tnh tin in ca cc khch hng theo quy nh sau.
- 100 kw u tin l 550 / kw
- 50 kw tip theo l 900 / kw
- 50 kw tip theo l 1210 / kw
- Thu 10 % trn tng s tin phi tr
Tnh tng s tin thu c ca cc khch hng.
III. KT LUN
+ Kiu d liu c cu trc cho php ta nh ngha nhng kiu d liu bt k trn
c s l nhng kiu d liu c bn c sn trong ngn ng lp trnh.
+ Khi xy dng xong kiu d liu mi ta phi nh ngha nhng thao tc cho
kiu d liu .
+ Nhng kiu d liu t nh ngha ny thng thng c rt nhiu thnh phn,
mi thnh phn cng c th l mt kiu d liu t nh ngha, vn l ta
chn kiu d liu c bn no xy dng nn chng sao cho ph hp v mt
kiu d liu v ph hp v kch thc lu tr (va ).
+ Cc s dng nhng kiu d liu t nh ngha cng ging nh cc kiu d
liu c bn. Mun s dng phi khai bo bin, khi truy cp cc thnh phn
phi truy cp theo quy c.
+ Nu thnh phn cu trc c kiu d liu l s thc th khi s dng hm
scanf() phi thng qua bin trung gian ri gn li cho thnh phn cu trc .
+ i vi mng cc kiu d liu c cu trc ta nn x l cho tng thnh phn
cu trc ri mi x l cho mng cu trc bng cch dng vng lp.

Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 97
CHNG 8 TP TIN

Trong chng ny, chng ta s tm hiu cu trc tp tin, ci t cc thao tc, mt s
hm th vin v ng dng trong vic t chc d liu trn tp tin.
I. TM TT L THUYT
I.1. Khi nim
Trong cc chng trnh trc th cc d liu a vo chng trnh ch c tn
ti trong RAM, khi thot chng trnh th tt c d liu u b mt. khc phc
tnh trng ny Borland C cung cp cho ta cc hm lu tr v truy xut tp tin,
l kiu FILE . V y ta ch cp n 2 loi tp tin :
Tp tin vn bn: l tp tin dng ghi cc k t ln a theo cc dng.
Tp tin nh phn: l tp tin dng ghi cc cu trc dng nh phn (c m
ho).
I.2. Thao tc vi tp tin
Qu trnh thao tc trn tp tin thng qua 4 bc:
Bc 1: Khai bo con tr tr n tp tin.
Bc 2: M tp tin.
Bc 3: Cc x l trn tp tin.
Bc 4: ng tp tin.
a. Khai bo
FILE *< tn bin >;
V d : FILE *f; // Khai bao bien con tro file f
b. M tp tin
fopen (< ng dn tn tp tin> , < kiu truy nhp >);

V d : FILE *f; // Khai bao bien con tro f
f = fopen ( C:\\VD1.txt , rt ) ;
Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 98
Cc kiu truy nhp tp tin thng dng:
t l kiu truy nhp tp tin i vi dng tp tin vn bn (text).
b l kiu truy nhp tp tin i vi dng tp tin nh phn (binary).
r m ra c ( ready only).
w m ra ghi (create / write).
a m ra them vo (append).
r+ m ra c v ghi (modify).
c. Cc hm c ghi ni dung tp tin
Tp tin vn bn

STT TN HM NGHA S DNG V D
C TP TIN
1
fscanf(<FILE *>, <nh
dng>, <cc tham bi a
Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 99
d. ng tp tin
Sau khi khng cn lm vic vi tp tin, m bo an ton cho d liu th
nht thit ta phi ng tp tin li.
fclose ( < bin con tr tp tin > ) ;
hoc fcloseall () ;
V d : fclose (f) ;
e. Cc thao tc khc trn tp tin
* Xo tp tin :
remove ( < ng dn tp tin> );
* i tn tp tin :
rename ( < tn tp tin c >, < tn tp tin mi > );
* Di chuyn con tr tp tin :
fseek ( < FILE * >, < di >, < mc > );
Cc mc :
SEEK_SET di dn u tp tin (gi tr 0).
SEEK_END di n cui tp tin (gi tr 2).
SEEK_CUR di v tr hin hnh (gi tr 1).
V d :
feek ( f , +5 , SEEK_CUR ); // di v tr hin hnh v cui 5 bytes
feek ( f, -4 , SEEK_CUR ); // di v tr hin hnh v trc 4 bytes
* Cho bit v tr con tr file:
ftell ( < FILE * > );
V d :
int size = ftell ( f );
/*size: khong cch t u tp tin n v tr hin hnh (tnh bng
byte)*/
f. V d minh ho
void KiemTra (FILE *f , char duongdan [ ] )
{
f = open ( duongdan , rt);
if ( f == NULL )
{
printf (Khong mo duoc tap tin %s, duongdan);
perror (\nLy do);
Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 100
getch ();
return ;
}
printf (Tap tin %s da duoc mo, duongdan);
fclose (f);
}
I.3. Cc v d minh ho
a. Tp tin vn bn
V d 1: Vit chng trnh to tp tin vn bn SO.OUT gm n s nguyn,
cc s ca dy c to ngu nhin c gi tr tuyt i khng vt qu M
( n, M c t tp tin SO.INP). Kt qu chng trnh l 1 tp tin vn bn
c dng th nht ghi s n; n dng tip theo ghi cc s to c, mi s
trn mt dng.


# include < conio.h >
# include < stdio.h >

# define in SO.INP
# define out SO.OUT
int n, M ;

void Nhap ()
{
FILE *fi;
fi = fopen ( in , rt );
fscanf ( fi, %d %d , &n, &M );
fclose ( fi );
}

void Xuat ()
{
FILE *fo;
fo = fopen ( out , wt );
fprintf ( fo , %d\n, n );
randomize ( );
for ( ; n > 0 ; n -- )
fprintf ( fo , %d\n , random ( ( 2 * M + 1 ) - M ) );
fclose ( fo );
SO.I NP
3
10
SO.OUT
3
5
7
2
Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 101
}
void main ()
{
clrscr ( );
Nhap ( );
Xuat ( );
}
V d 2: Vit chng trnh pht sinh ngu nhin ma trn a kch thc 5x6,
lu ma trn ny vo file test.inp. c li file test.inp a d liu vo ma
trn b v xut ra mn hnh xem kt qu lu ng khng? Cu trc ca file
test.inp nh sau:
- Dng u lu 2 s nguyn: m, n th hin s dng v s ct ca ma
trn.
- m dng tip theo, mi dng gm n phn t l gi tr cc phn t
trn mt dng ca ma trn.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define MAX 100
#define dl "test.inp"

void LuuFile(int a[MAX][MAX], int m, int n)
{
FILE *f;
f=fopen(dl, "wt");
if(f==NULL)
{
printf("\nKhong tao duoc file.");
getch();
exit(0);
}
fprintf(f, "%d %d\n", m, n);
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
fprintf(f, "%d\t", a[i][j]);
fprintf(f, "\n");
}
fclose(f);
}

void DocFile(int a[MAX][MAX], int &m, int &n)
{
Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 102
FILE *f;
f=fopen(dl, "rt");
if(f==NULL)
{
printf("\nKhong doc duoc file.");
getch();
exit(0);
}
fscanf(f, "%d%d", &m, &n);
for(int i=0; i<m; i++)
{
for(int j=0; j<n; j++)
fscanf(f, "%d", &a[i][j]);
}
fclose(f);
}

void main()
{
int a[MAX][MAX], m=5, n=6, i, j;
int b[MAX][MAX], x, y;

randomize();
for(i=0; i<m; i++)
for(j=0; j<n; j++)
a[i][j]=random(1000);
LuuFile(a, m, n);
DocFile(b, x, y);
for(i=0; i<x; i++)
{
for(j=0; j<y; j++)
printf("%d\t", b[i][j]);
printf("\n");
}
}
Kt qu v d sau khi chy chng trnh, file test.inp c dng sau:
5 6
480 661 395 736 998 987
31 414 211 801 774 416
166 191 454 830 508 72
121 382 35 365 567 726
159 309 1 275 870 378
b. Tp tin nh phn
Vit hm c/ ghi mt danh sch sinh vin ca mt lp vo tp tin
SV.DAT
SINHVIEN ds[100];
Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 103
int siso;

void nhap ( )
{
FILE *fi;
fi = fopen ( SV.DAT , rb );
fseek ( fi , 0 , SEEK_END );
siso = ( ftell ( fi ) + 1 ) / sizeof ( SINHVIEN );
fseek ( fi , 0 , SEEK_SET );
fread ( ds , sizeof ( SINHVIEN ) , siso , fi );
fclose ( fi );
}

void xuat ( )
{
FILE *fo;
fo = fopen ( SV.DAT, wb );
fwrite ( ds , sizeof ( SINHVIEN ) , siso , fo );
fclose ( fo );
}
II. BI TP
II.1. Bi tp c bn
1. Vit chng trnh to tp tin vn bn cha 1 dy s nguyn bt k.
2. Vit chng trnh to tp tin nh phn cha 10000 s nguyn bt k ghi
vo file SONGUYEN.INP. Mi dng 10 s, sau vit chng trnh c
file SONGUYEN.INP, sp xp theo th t tng dn v lu kt qu vo file
SONGUYEN.OUT.
3. Vit chng trnh to mt file cha 10000 s nguyn ngu nhin i mt
khc nhau trong phm vi t 1 n 32767 v t tn l
SONGUYEN.INP.
4. Vit chng trnh to mt file cha cc s nguyn c tn
SONGUYEN.INP. Sau c file SONGUYEN.INP v ghi cc s chn
vo file SOCHAN.OUT v nhng s l vo file SOLE.OUT.
5. Vit chng trnh ghi vo tp tin SOCHAN.DAT cc s nguyn chn t 0
n 100.
6. Vit chng trnh c tp tin SOCHAN.DAT v xut ra mn hnh, mi
dng 30 s.
7. Vit chng trnh gi lp lnh COPY CON to tp tin vn bn. Khi kt
thc tp tin nhn phm F6 lu.
Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 104
8. Vit chng trnh gi lp lnh TYPE in ni dung ca tp tin vn bn ra
mn hnh.
9. Vit chng trnh kim tra mt tp tin no c trong mt th mc c
ch nh hay khng?
10. Vit chng trnh gi lp lnh DEL xo tp tin. Yu cu nhp ng
dn v tn tp tin, kim tra s tn ti ca tp tin, nu c th xo tp tin
c ch nh.
11. Vit chng trnh gi lp lnh RENAME i tn mt tp tin.
12. Vit chng trnh to file vn bn c tn l MATRIX.INP c cu trc
nh sau:
Dng u ghi hai s m, n.
Trong m dng tip theo mi dng ghi n s v cc s cc nhau mt
khong cch.
Hy kim tra xem trong file c bao nhiu s nguyn t.
Kt qu cn ghi vo file MATRIX.OUT c ni dung l mt s nguyn
l s lng cc s nguyn t trong file MATRIX.INP.
13. Cho s nguyn n, hy in tam gic PASCAL gm n dng
D lu vo: tp tin vn bn PAS.INP gm 1 dng cha gi tr n.
Kt qu: a ra tp tin vn bn PAS.OUT th hin mt tam gic PASCAL
n dng.
14. Cho mng cc s nguyn , hy sp xp mng theo th t tng dn.
D liu vo : tp tin vn bn ARRAY.INP gm 2 dng
- Dng 1 cha s nguyn n ( n < = 100 ).
- Dng 2 cha n s nguyn.
Kt qu : a ra tp tin vn bn ARRAY.OUT gm hai dng
- Dng 1 cha n phn t ca mng cc s nguyn.
- Dng 2 cha n s nguyn c xp tng dn.
15. Cho mng cc s nguyn, tm phn t ln nht ca mng.
D liu vo: tp tin vn bn ARRAY.INP gm hai dng:
- Dng 1 cha s nguyn n ( n < = 100 ).
- Dng 2 cha n s nguyn.
Kt qu: a ra tp tin vn bn ARRAY.OUT gm 1 dng ghi 2 gi tr x,
y trong x l gi tr ln nht, y l v tr ca x trong mng.
Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 105
II.2. Bi tp luyn tp v nng cao
16. Cho mng cc s nguyn, tnh tng cc phn t ca mng.
D liu vo : tp tin vn bn ARRAY.INP gm hai dng
- Dng 1 cha s nguyn n ( n < = 10 )
- Dng 2 cha n s nguyn
Kt qu : a ra tp tin vn bn ARRAY.OUT gm mt dng ghi tng
cc phn t trong mng.
17. Cho mng cc s nguyn, hy lit k cc phn t l s nguyn t
D liu vo : tp tin vn bn NT.INP gm hai dng
- Dng 1 cha s nguyn n ( n < = 100)
- Dng 2 cha n s nguyn
Kt qu : a ra tp tin vn bn NT.OUT gm hai dng:
- Dng 1 cha s lng cc phn t nguyn t trong mng.
- Dng 2 lit k cc s nguyn t .
18. (*) To file vn bn c tn l INPUT.TXT c cu trc nh sau:
- Dng u tin ghi N (N l s nguyn dng nhp t bn phm).
- Trong cc dng tip theo ghi N s nguyn ngu nhin trong phm vi
t 0 n 100, mi dng 10 s (cc s cch nhau t nht mt khong
trng).
Hy c d liu ca file INPUT.TXT v lu vo mng mt chiu A.
Thc hin cc cng vic sau :
Tm gi tr ln nht ca mng A.
m s lng s chn, s lng s l ca mng A.
Hy sp xp cc phn t theo th t tng dn.
Hy ghi cc kt qu vo file vn bn c tn OUTPUT.TXT theo mu sau:

19. (*) Vit chng trnh nhp v lu h s ca sinh vin vo mt file c tn
l DSSV.TXT. Sau c file DSSV.TXT v ct vo mng, hy sp
xp cc h s sinh vin theo th t gim dn theo im trung bnh mn
OUTPUT.TXT
Cau a: 99
Cau b: 9 9
Cau c:
1 4 4 19 20 29 34 38 39 40
43 58 65 78 87 89 98 99
INPUT.TXT
18
87 39 78 19 89 4 40 98 29
65
20 43 1 99 38 34 58 4
Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 106
hc ri in ra mn hnh h s cc sinh vin theo th t ra mn hnh c
thng tin nh sau :
M s sinh vin.
H v tn sinh vin.
im trung bnh kim tra.
im thi ht mn.
im trung bnh mn hc (tnh bng (im TBKT+im thi)/2).
20. (*) To mt file text c tn l INPUT.TXT c cu trc nh sau :
- Dng u tin ghi hai s M v N (M,N l hai s nguyn dng nhp
t bn phm).
- Trong M dng tip theo mi dng ghi N s nguyn ngu nhin trong
phm vi t 0 n 100 (cc s ny cch nhau t nht mt khong
trng).
Hy c d liu t file trn v lu vo mng hai chiu. Ri thc hin cc
cng vic sau:
Tm gi tr ln nht ca ma trn.
m s lng s chn , l, nguyn t c trong ma trn.
Hy tnh tng cc phn t trn mi dng ca ma trn.
Hy ghi kt qu ny vo filetext c tn l OUTPUT.TXT


21. (**) Xt dy s a1, a2, , aN. Mt on con ca dy l dy cc phn t
lin tip nhau c xc nh bi ch s ca s bt u (L) v ch s ca s
cui cng (R).Tng cc s trn on c gi l tng on.
Yu cu : Cho dy ( aN ), hy tm on con c tng on ln nht (T)
D liu c cho trong tp tin vn bn SUMMAX.INP
- Dng th nht ch s nguyn N ( 0 < N <= 30000 )
INPUT.TXT
6 6
41 17 33 23 12 1
44 24 23 49 5 24
33 20 17 25 33 19
0 48 45 48 41 32
10 24 36 19 19 24
30 4 23 26 27 36
OUTPUT.TXT
Cau a: 49
Cau b: 17 19
Cau c: 127 169 147 214 132
146

Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 107
- N dng tip theo, mi dng cha mt s l cc s ca dy cho
theo ng th t. Gi tr tuyt i ca mi s khng vt qu 30000
Kt qu tm c ghi vo tp tin vn bn SUMMAX.OUT gm 1 dng ghi
3 s T, L, R.
V d :


22. (*) Cho dy ( aN ), hy tm on con tng dn c tng ln nht
D liu : c cho trong tp tin AMAX.INP
- Dng 1 cha s nguyn N ( 0 < N <= 30000 ).
- N dng tip theo, mi dng cha mt s l cc s ca dy y cho
theo ng th t. Gi tr tuyt i ca mi s khng vc qu 30000.
Kt qu tm c ghi vp tin vn bn AMAX.OUT gm hai dng:
- Dng 1 ghi tng ca dy con.
- Dng 2 ghi mng con tng dn c tng ln nht.
23. Vit chng trnh nhp l lch mt nhn vin vo danh sch cc nhn vin.
Khi khng nhp na bm phm Esc v ghi vo tp tin NHANVIEN.DAT
sau :
c t tp tin NHANVIEN.DAT va to v in danh sch cc nhn
vin ln mn hnh.
Tm v in l lch mt nhn vin bng cc nhp v h tn hoc m s
nhn vin.
24. (**) lp rp mt my vi tnh hon chnh cn phi c ti thiu 10 linh
kin loi A v c th lp b sung thm vo khong ti a 8 linh kin loi
B. Ti mt ca hng vi tnh cn qun l bn hng cc loi linh kin ti ca
hng. Thng tin v mt loi linh kin gm c: Tn linh kin, quy cch ,
loi, n gi loi 1 ( cht lng tt s nguyn), n gi loi 2 ( cht
lng thng s nguyn ). Vit chng trnh thc hin nhng cng vic
sau :
SUMMAX.I NP
5
-1
5
-3
2
4
SUMMAX.OUT
8 2 5

Tp tin
Gio trnh Bi Tp K Thut Lp Trnh Trang 108
Nhp vo thng tin ca cc loi linh kin c ca hng. Xut danh
sch cc linh kin nhp theo th t tng dn ca loi linh kin v
tn linh kin. Cho bit c 10 linh kin loi A cn thit lp
rp my tnh hay cha?
Vi gi nh l ca hng c 10 linh kin loi A lp rp my.
Nhp vo mt s tin lp rp mt my tnh. C th lp c mt
my tnh hon chnh vi cc linh kin ton b theo n gi loi 1 hay
n gi loi 2 hay khng? Nu s tin trong khong gia th hy tm
mt phng n gm nhng linh kin theo n gi 1 v linh kin theo
n gi 2 lp?
Tt c d liu phi lu tp tin.
III. KT LUN
+ Mc ch ca kiu d liu tp tin cho php chng ta lu li nhng thng tin cn
thit tng i ln: nhng d liu u vo, nhng kt qu ca chng trnh hoc
nhng d liu dng test chng trnh,
+ Khi thao tc trn tp tin phi thng qua 4 bc: Khai bo con tr tr n tp tin,
M tp tin, X l trn tp tin v cui cng l ng tp tin.
+ Lu khi m tp tin ghi th phi cn thn vi thao tc to mi hay chnh sa
ni dung tp tin, di chuyn con tr hp l trnh mt thng tin.
+ S dng hm thao tc trn tp tin phi dng ng loi hm cho tp tin kiu nh
phn hay kiu vn bn.
qui
Gio trnh Bi Tp K Thut Lp Trnh Trang 109
CHNG 9 QUI

Gii thiu phng php lp trnh theo k thut quy, phn loi, cch hot ng v
cch ci t cc hm quy.
I. TM TT L THUYT
I.1. Khi nim
Mt hm c gi c tnh qui nu trong thn ca hm c lnh gi li chnh
n mt cch tng minh hay tim n.
I.2. Phn loi qui
qui tuyn tnh.
qui nh phn.
qui phi tuyn.
qui h tng.
a. qui tuyn tnh
Trong thn hm c duy nht mt li gi hm gi li chnh n mt cch
tng minh.
<Kiu d liu hm> TenHam (<danh sch tham s>)
{
if (iu kin dng)
{
. . .
//Tr v gi tr hay kt thc cng vic
}
//Thc hin mt s cng vic (nu c)
. . . TenHam (<danh sch tham s>);
//Thc hin mt s cng vic (nu c)
}
V d 1: Tnh n n S + + + + = L 3 2 1 ) (
Trc khi ci t hm qui ta xc nh:
- iu kin dng: S(0) = 0.
- Qui tc (cng thc) tnh: S(n) = S(n-1) + n.
Ta ci t hm qui nh sau:
qui
Gio trnh Bi Tp K Thut Lp Trnh Trang 110
long TongS (int n)
{
if(n==0)
return 0;
return ( TongS(n-1) + n );
}
V d 2: Tnh P(n) = n!
Trc khi ci t hm qui ta xc nh:
- iu kin dng: P( 0) = 0! = 1.
- Qui tc (cng thc) tnh: P(n) = P(n-1) * n.
Ta ci t hm qui nh sau:
long GiaiThua (int n)
{
if(n==0)
return 1;
return ( GiaiThua(n-1) * n );
}
b. qui nh phn
Trong thn ca hm c hai li gi hm gi li chnh n mt cch tng
minh.
<Kiu d liu hm> TenHam (<danh sch tham s>)
{
if (iu kin dng)
{
. . .
//Tr v gi tr hay kt thc cng vic
}
//Thc hin mt s cng vic (nu c)
. . .TenHam (<danh sch tham s>); //Gii quyt vn nh hn
//Thc hin mt s cng vic (nu c)
. . . TenHam (<danh sch tham s>); //Gii quyt vn cn li
//Thc hin mt s cng vic (nu c)
}
V d 1: Tnh s hng th n ca dy Fibonaci c nh ngha nh sau:
f
1
= f
0
=1 ;
f
n
= f
n-1
+ f
n-2
; (n>1)
Trc khi ci t hm qui ta xc nh:
- iu kin dng: f(0) = f(1) = 1.
Ta ci t hm qui nh sau:
qui
Gio trnh Bi Tp K Thut Lp Trnh Trang 111
long Fibonaci (int n)
{
if(n==0 || n==1)
return 1;
return Fibonaci(n-1) + Fibonaci(n-2);
}
V d 2: Cho dy s nguyn a gm n phn t c th t tng dn. Tm phn
t c gi tr x c xut hin trong mng khng?
Trc khi ci t hm qui ta xc nh:
- iu kin dng: Tm thy x hoc xt ht cc phn t.
- Gii thut:
Do dy s c th t tng nn ta c th p dng cch tm kim
theo phng php nh phn. tng ca phng php ny l ti
mi bc ta tin hnh so snh x vi phn t nm v tr gia ca
dy thu hp phm vi tm.
Gi: l: bin tri ca dy (ban u l=0).
r: bin phi ca dy (ban u r = n-1).
m: v tr gia (m = (l+r)/2).
l m R
+ + +
a[0] a[1] a[(l+r)/2] a[n-2] a[n-1]
Thu hp da vo gi tr ca phn t gia, c hai trng hp:
i. Nu x ln hn phn t gia th x ch c th xut hin bn
phi v tr ny. (t m+1 n r).
ii. Ngc li nu x nh hn phn t gia th x ch c th xut
hin bn tri v tr ny. (t l n m-1).
Qu trnh ny thc hin cho n khi gp phn t c gi tr x, hoc
xt ht cc phn t.
Ta ci t hm qui nh sau:
int TimNhiPhan(int a[], int l, int r, int x)
{
int m = (l+r)/2;
if(l>r)
return -1;// Khng c phn t x
if(a[m]>x) return TimNhiPhan(a, l, m-1, x);
if(a[m]<x) return TimNhiPhan(a, m+1, r, x);
return m;//Tr v v tr tm thy
qui
Gio trnh Bi Tp K Thut Lp Trnh Trang 112
}
V d 3: Bi ton thp H Ni:
Bc 1: Di chuyn n -1 a nh hn t cc A sang cc B.
Bc 2: Di chuyn a cn li t cc A sang cc C.
Bc 3: Di chuyn n -1 a nh hn t cc B sang cc C.
Ta ci t hm qui nh sau:
void ThapHaNoi (int n, char A, char B, char C)
{
if (n = = 1)
printf(Di chuyen dia tren cung tu %d den %d\n, A, C);
else
{
ThapHaNoi(n-1, A, C, B);
ThapHaNoi(1, A, B, C);
ThapHaNoi(n-1, B, A, C);
}
}
c. qui phi tuyn
Trong thn ca hm c li gi hm gi li chnh n c t bn trong
vng lp.
<Kiu d liu hm> TenHam (<danh sch tham s>)
{
for (int i = 1; i<=n; i++)
{
//Thc hin mt s cng vic (nu c)
if (iu kin dng)
{
. . .
//Tr v gi tr hay kt thc cng vic
}
else
{
//Thc hin mt s cng vic (nu c)
TenHam (<danh sch tham s>);
}
}
}
V d: Tnh s hng th n ca dy {X
n
} c nh ngha nh sau:
X
0
=1 ;
X
n
= n
2
X
0
+ (n-1)
2
X
1
+ + 1
2
X
n-1
; (n1)
Trc khi ci t hm qui ta xc nh:
qui
Gio trnh Bi Tp K Thut Lp Trnh Trang 113
- iu kin dng:X(0) = 1.
Ta ci t hm qui nh sau:
long TinhXn (int n)
{
if(n==0)
return 1;
long s = 0;
for (int i=1; i<=n; i++)
s = s + i * i * TinhXn(n-i);
return s;
}
d. qui h tng
Trong thn ca hm ny c li gi hm n hm kia v trong thn ca
hm kia c li gi hm ti hm ny.
g()
f()
h()
f()
f()
g()

<Kiu d liu hm> TenHam2 (<danh sch tham s>);
<Kiu d liu hm> TenHam1 (<danh sch tham s>)
{
//Thc hin mt s cng vic (nu c)
TenHam2 (<danh sch tham s>);
//Thc hin mt s cng vic (nu c)
}
<Kiu d liu hm> TenHam2 (<danh sch tham s>)
{
//Thc hin mt s cng vic (nu c)
TenHam1 (<danh sch tham s>);
//Thc hin mt s cng vic (nu c)
}
V d: Tnh s hng th n ca hai dy {X
n
}, {Y
n
} c nh ngha nh
sau:
X
0
=Y
0
=1 ;
X
n
= X
n-1
+ Y
n-1
; (n>0)
Y
n
= n
2
X
n-1
+ Y
n-1
; (n>0)
Trc khi ci t hm qui ta xc nh:
- iu kin dng:X(0) = Y(0) = 1.
qui
Gio trnh Bi Tp K Thut Lp Trnh Trang 114
Ta ci t hm qui nh sau:
long TinhYn(int n);
long TinhXn (int n)
{
if(n==0)
return 1;
return TinhXn(n-1) + TinhYn(n-1);
}

long TinhYn (int n)
{
if(n==0)
return 1;
return n*n*TinhXn(n-1) + TinhYn(n-1);
}
I.3. Tm hiu cch hot ng ca hm qui
Phc v cho cng vic kim chng kt qu thc thi ca chng trnh bng tay.
V d 1: Ly li v d tnh P(n) = n! bng phng php qui nh m t ci
t trn vi n = 5

Lnh gi khi u trong hm main(), truyn n hm GiaiThua(). , gi tr
ca tham s n l 5, do ni gi GiaiThua(4), truyn 4 n hm GiaiThua().
gi tr ca tham s n l 4, do n gi GiaiThua(3), truyn 3 n hm
GiaiThua(). Tin trnh ny tip tc ( quy) n khi gi GiaiThua(1) c thc
hin t bn trong lnh gi GiaiThua(2). , gi tr ca tham s n l 1, do n
tr v gi tr 1, m khng thc hin thm bt k lnh gi no. Sau ln ngc v
lnh gi GiaiThua(2) tr 2*1=2 tr v lnh gi GiaiThua(3). Sau lnh gi
GiaiThua(3) tr 3*2=6 tr v lnh gi GiaiThua(4). Sau lnh gi GiaiThua(4)
tr 4*6=24 tr v lnh gi GiaiThua(5). Sau cng, lnh gi GiaiThua(5) tr v gi
tr 120 cho hm main().
V d 2: Ly li v d tnh s hng th n ca dy Fibonaci nh m t ci t
trn vi n = 5, qu trnh thc hin tng t nh trong v d trc, ta c s sau:
qui
Gio trnh Bi Tp K Thut Lp Trnh Trang 115

I.4. V d
Vit chng trnh nhp vo mng mt chiu s nguyn a, xut ra mn hnh v
tnh tng cc phn t c gi tr chn bng phng php qui.
#define MAX 100
void Nhap(int a[], int n)
{
if(n==0)
return;
Nhap(a, n-1);
printf("\nNhap phan tu thu %d: ", n);
scanf("%d", &a[n-1]);
}

void Xuat(int a[], int n)
{
if(n==0)
return;
Xuat(a, n-1);
printf("%d\t", a[n-1]);
}

long TongChan(int a[], int n)
{
if(n==0)
return 0;
int s = TongChan(a, n-1);
if(a[n-1]%2==0)
s+=a[n-1];
return s;
}

qui
Gio trnh Bi Tp K Thut Lp Trnh Trang 116
void main()
{
int a[MAX], n;
long s;

printf("\nNhap so phan tu cua mang: ");
scanf("%d", &n);
Nhap(a, n);
Xuat(a, n);
s=TongChan(a, n);
printf("\nTong cac so chan trong mang la: %ld", s);
getch();
}
II. BI TP
Vit hm qui thc hin cc yu cu sau:
II.1. Bi tp c bn
1. Ci t li nhng bi tp chng mng mt chiu.
2. Tm ch s c gi tr ln nht ca s nguyn dng n.
3. Hy xy dng mt dy gm N s c gi tr t 1 n K cho trc, sau cho
khng c hai dy con lin tip ng k nhau.
V d: N = 6
K = 3
Kt qu: 121312
4. Tm c s chung ln nht ca hai s nguyn dng a v b.
5. Tm ch s u tin ca s nguyn dng n.
6. Tm dy nh phn di nht sao cho trn dy ny khng c hai b k bt k
trng nhau. B k l dy con c k s lin tip nhau trn dy tm c.
V d: k = 3
Kt qu: 000 101 110 0
7. Tnh ) 1 2 ( 5 . 3 . 1 ) ( + = n n P K , vi
0 n

8. Tnh ) 1 2 ( 5 3 1 ) ( + + + + + = n n S L , vi
0 n

9. Tnh n n S
n 1
) 1 ( 4 3 2 1 ) (
+
+ + + = L , vi
0 > n

10. Tnh n n S K L 3 . 2 . 1 3 . 2 . 1 2 . 1 1 ) ( + + + + = , vi
0 > n

11. Tnh
2 2 2 2
3 2 1 ) ( n n S + + + + = L , vi
0 > n

qui
Gio trnh Bi Tp K Thut Lp Trnh Trang 117
12. Tnh
n
n S
1
3
1
2
1
1 ) ( + + + + = L , vi
0 > n

13. Tnh
n
n S
+ + + +
+ +
+ +
+
+
+ =
L
L
3 2 1
1
3 2 1
1
2 1
1
1 ) ( , vi
0 > n

14. Tnh
y
x y x P = ) , ( .
15. Tnh ) 3 2 1 ( ) 3 2 1 ( ) 2 1 ( 1 ) ( n n S + + + + + + + + + + + = L L , vi 0 > n
II.2. Bi tp luyn tp v nng cao
16. Cho s nguyn dng n. In ra biu din nh phn ca n.
17. (*) Ci t v minh ho bi ton thp H Ni.
18. (**) Ci t bi ton m i tun.
19. (**) Ci t bi ton tm hu.
20. (*) Tnh 1 ... ) 2 ( ) 1 ( ) ( + + + + = n n n n S , vi 0 > n
21. (*) Tnh n n S + + + + = ... 3 2 1 ) ( , vi 0 > n
22. (*) Tnh
1 1
1
1
1
1
1
1
1
1
1
1
) (
+
+
+
+
+
=
O
n S c n du phn s.
III. KT LUN
+ qui cung cp cho ta c ch gii quyt cc bi ton phc tp mt cch n
gin hn.
+ Xy dng hm qui thng qua vic xc nh iu kin dng v bc thc
hin tip theo.
+ Ch nn ci t bng phng php qui khi khng cn cch gii quyt bng
cch lp thng thng.

Vit chng trnh theo phng php Project
Gio trnh Bi Tp K Thut Lp Trnh Trang 118
CHNG 10 LP TRNH THEO PHNG PHP PROJECT
I. MC TIU
Chia mt chng trnh ln thnh cc tp tin nh hn, mi tp tin cha cc khai bo
nguyn mu hm, ci t cc hm v d liu thc hin mt s chc nng nht nh.
Vic phn chia ny gip qu trnh lp trnh:
+ D kim sot cc lnh v kim li.
+ Trnh c gii hn kch thc tp tin qu ln ca ngn ng lp trnh.
II. PHNG PHP
II.1. To mt project mi
Bc 1: To th mc s cha ton b chng trnh s c ci t.
Bc 2: Khi ng Borland C++ 3.1.
Bc 3: Thay i ng dn n th mc va to.
Vo menu File\Change Dir ... sau chn ng dn th mc v chn OK.
Bc 4: To Project.
Vo menu Project\Open Project sau t tn cho project tng ng, chn OK.
(Lu : Xem ng dn ca file Project c nm ng th mc va to bc 1
hay khng. Nu cn c th chnh sa li ng dn).
Bc 5: Thm file vo Project.
Chn menu Window\Project sau nhn phm Insert hoc vo menu Project\Add
Item ... t tn file v chn OK, mun loi file khi project th chn
Project\Delete Item (Hoc khi ang trong ca s project va to nhn phm insert
thm file, mun xo chn file ri nhn delete).
Lu : Ch Insert cc file cha ci t lp v hm main (*.cpp), khng insert file
header do ngi dng nh ngha (*.h).
II.2. M project c sn
Bc 1: ng project trc (nu c).
Vo menu Project\Close Project.
Bc 2: M project.
Vit chng trnh theo phng php Project
Gio trnh Bi Tp K Thut Lp Trnh Trang 119
Vo menu Project\Open Project chn ng dn n file project cn thc hin,
chn OK.
Bc 3: Hiu chnh ng dn th vin ca BC++ 3.1.
Vic to project cc my vi thng s ci t BC++3.1 khc nhau s dn n
ng dn th vin hm ca cc my cng khc nhau, do vy khi bin dch s gp
li v th vin hm trong BC++3.1.
Vo menu Options\Directories... sau hiu chnh li ng dn n th mc
cha th vin hm trong cc Include v Library cho ng vi ng dn ci
BC++3.1 (ng dn n th mc INCLUDE v th mc BIN ca BC++3.1 trn
my ang s dng).
II.3. Mt s lu
Nn chia tng file theo tng nhm hm. Mi mt project phi c ti thiu 3 file
nh sau:
File header (*.h): To th vin t nh ngha. Cha cc khai bo nguyn mu
hm, kiu d liu,
File ci t hm (*.cpp): Cha cc ci t hm theo nhm. Nu c s dng
th vin t nh ngha th phi include file cha th vin vo.
File cha hm main() (m*.cpp): Cha hm chnh (hm main()).
Khi ci t hay chnh sa mt hm no trc ht phi xem xt hm thuc
nhm hm no v sau m file ca nhm tng ng hiu chnh.
II.4. V d minh ho
Vit chng trnh nhp thng tin ca hc sinh gm: h tn hc sinh, im vn v
ton, xut thng tin v tnh im trung bnh cho hc sinh .
Ta chia chc nng chng trnh theo cc nhm chc nng d qun l, gm cc
file sau:
+ File hocsinh.h: Cha cc khai bo bin v nguyn mu hm.
+ File mhocsinh.cpp: Cha hm main().
+ File xuat.cpp: Cha cc thao tc xut thng tin hc sinh,
+ File nhap.cpp: Cha cc thao tc nhp thng tin hc sinh,
+ File tinhtoan.cpp: Cha cc thao tc tnh im trung bnh,
Vit chng trnh theo phng php Project
Gio trnh Bi Tp K Thut Lp Trnh Trang 120
Bc 1: To th mc HOCSINH s cha ton b cc file ca chng trnh s
c ci t (V d to a D:).
Bc 2: Khi ng Borland C++ 3.1.
Bc 3: Thay i ng dn n th mc HOCSINH va to.
+ Chn th mc HOCSINH

Bc 4: To Project: t tn file project l hocsinh

+ Ci t file hocsinh.h trong th mc HOCSINH.
# ifndef _HOCSINH_H
# define _HOCSINH_H

typedef struct HOCSINH
{
char hoten[30];
int toan, van;
};

void NhapHS(HOCSINH &hs);
float TinhDTB(HOCSINH hs);
void XuatHS(HOCSINH hs);

# endif
Vit chng trnh theo phng php Project
Gio trnh Bi Tp K Thut Lp Trnh Trang 121
Bc 5: Thm file vo Project.

+ Nhn F3, t tn file mi l nhap.cpp v vit hm nhp. Tng t cho nhng
file: xuat.cpp, tinhtoan.cpp v file mhocsinh.cpp.


Vit chng trnh theo phng php Project
Gio trnh Bi Tp K Thut Lp Trnh Trang 122
+ Ni dung file nhap.cpp.
#include <conio.h>
#include <stdio.h>

// S dng kiu d liu HOCSINH v khai bo nguyn mu hm
#include "hocsinh.h"

void NhapHS(HOCSINH &hs)
{
printf("\nNhap vao ho ten hoc sinh: ");
gets(hs.hoten);
printf("\nNhap vao diem toan: ");
scanf("%d", &hs.toan);
printf("\nNhap vao diem van: ");
scanf("%d", &hs.van);
}
+ Ni dung file xuat.cpp.
#include <conio.h>
#include <stdio.h>

// S dng kiu d liu HOCSINH v khai bo nguyn mu hm
#include "hocsinh.h"

void XuatHS(HOCSINH hs)
{
printf("\nHo ten hoc sinh: %s", hs.hoten);
printf("\nDiem toan: %d \nDiem van: %d", hs.toan, hs.van);
printf("\nDiem trung binh: %.2f", TinhDTB(hs));
}
+ Ni dung file tinhtoan.cpp.
// S dng kiu d liu HOCSINH v khai bo nguyn mu hm
#include "hocsinh.h"

float TinhDTB(HOCSINH hs)
{
return (hs.toan + hs.van)/2.0;
}
+ Ni dung file mhocsinh.cpp.
#include <conio.h>
#include <stdio.h>

// S dng kiu d liu HOCSINH v khai bo nguyn mu hm
#include "hocsinh.h"

Vit chng trnh theo phng php Project
Gio trnh Bi Tp K Thut Lp Trnh Trang 123
void main()
{
clrscr();
HOCSINH hs;

NhapHS(hs);
printf(\nKet qua:\n);
XuatHS(hs);

getch();
}

+ Nhn F9 bin dch v kim li.
+ Nhn Ctrl + F9 thc thi chng trnh.
V d kt qu chy chng trnh

Nhap vao ho ten hoc sinh: Nguyen Van A
Nhap vao diem toan: 6
Nhap vao diem van: 5

Ket qua:

Ho ten hoc sinh: Nguyen Van A
Diem toan: 6
Diem van: 5
Diem trung binh: 5.50
III. BI TP
Ci t cc bi tp chng mng cu trc bng phng php to project.

Mt s thi mu
Gio trnh Bi Tp K Thut Lp Trnh Trang 124
PH LC 1 THI MU


S 01
Thi gian: 120 pht
(Khng tham kho ti liu)
====

Cu 1: Vit chng trnh tnh tng: ! ! 2 ! 1 ) ( n n S + + + = L
Cu 2: Vit chng trnh thc hin cc yu cu sau:
a. Nhp mng mt chiu cc s nguyn.
b. m s lng gi tr chn m trong mng.
c. Tm s l cui cng trong mng.
Cu 3: Cho ma trn cc s thc. Vit hm tm gi tr trong ma trn xa gi tr x nht.
float xanhat(float a[][100], int m, int n, float x);
Cu 4: Hy khai bo kiu d liu biu din khi nim im trong mt phng Oxy
(DIEM).
a. Vit hm nhp ta im.
void nhap(DIEM &P);
b. Vit hm xut ta im.
void xuat(DIEM P);
c. Vit hm tnh khong cc gia 2 im.
float khoangcach(DIEM P, DIEM Q);

S 02
Thi gian: 120 pht
(Khng tham kho ti liu)
====

Cu 1: Vit chng trnh tnh tng:
n
x x x n x S + + + = L
2
) , (
Cu 2: Vit chng trnh thc hin cc yu cu sau:
a. Nhp mng mt chiu cc s nguyn.
Mt s thi mu
Gio trnh Bi Tp K Thut Lp Trnh Trang 125
b. m s lng gi tr l dng trong mng.
c. Tm s chn cui cng trong mng.
Cu 3: Cho ma trn cc s thc. Vit hm tm gi tr trong ma trn gn gi tr x nht.
float gannhat(float a[][100], int m, int n, float x);
Cu 4: Hy khai bo kiu d liu biu din khi nim phn s (PHANSO)
a. Vit hm nhp phn s.
void nhap(PHANSO &x);
b. Vit hm xut phn s.
void xuat(PHANSO x);
c. Vit hm tnh tng hai phn s.
PHANSO tong(PHANSO x, PHANSO y);

S 03
Thi gian: 120 pht
(Khng tham kho ti liu)
====

Cu 1:
Sn =
2 2
1 2
...
6
5
4
3
2
1
+
+
+ + + +
n
n
Vi n nguyn dng (n>0)
1. V lu thut ton (Flowchart) tnh tng trn.
2. Vit hm tnh tng trn bng phng php quy.
Cu 2:
Cho mng mt chiu cc s thc A kch thc n (0<n100). Hy xy dng hm
thc hin cc yu cu sau:
1. Nhp gi tr cc phn t vo mng.
2. Tm v tr v v tr ca phn t c gi tr m u tin trong mng. Nu khng
c gi tr m th tr v -1.
3. Tm v tr v gi tr phn t m ln nht trong mng a. Nu mng khng c
phn t cha gi tr m th tr v 0.
Cu 3:
Mt s thi mu
Gio trnh Bi Tp K Thut Lp Trnh Trang 126
Cho ma trn vung cc s nguyn A kch thc nxn (3<n<10). Hy xy dng cc
hm cho php thc hin cc yu cu sau:
1. Nhp gi tr cc phn t vo ma trn.
2. m v tr v s lng cc phn t l s nguyn t trong ma trn.
3. Tnh trung bnh cng cc phn t trn ng cho chnh.
Cu 4:
Hy khai bo kiu d liu biu din thng tin ca mt nhn vin
(NHANVIEN).
Bit mt nhn vin gm:
- M nhn vin (MaNV): Chui ti a 5 k t.
- Tn nhn vin (TenNV): Chui ti a 30 k t.
- Chc v (ChucVu): Chui ti a 20 k t
(gm cc chc v: Truong phong, Nhan vien, Giam doc, Pho giam
doc, ).
- S nm lm vic (SoNam): S nguyn 1 byte.
- H s lng (HeSo): Kiu s thc.
Cho danh sch gm n (n>0) nhn vin. Vit cc hm sau:
1. Lit k cc nhn vin c s nm lm vic t 3 nm tr ln.
2. m s nhn vin c chc v l Truong phong.
Sp xp danh sch nhn vin tng dn theo h s lng nhn vin.


S 04
Thi gian: 120 pht
(Khng tham kho ti liu)
====

Cu 1:
Sn = n
n 1
) 1 ( 4 3 2 1
+
+ + + L Vi n nguyn dng (n>0)
1. V lu thut ton (Flowchart) tnh tng trn.
2. Vit hm tnh tng trn bng phng php quy.
Cu 2:
Cho mng mt chiu cc s nguyn A kch thc n (0< n100). Hy xy dng
hm thc hin cc yu cu sau:
1. Nhp gi tr cc phn t vo mng.
Mt s thi mu
Gio trnh Bi Tp K Thut Lp Trnh Trang 127
2. Tm v tr v v tr ca phn t c gi tr l s nguyn t u tin trong
mng. Nu khng c gi tr l s nguyn t th tr v -1.
3. Tm v tr v gi tr phn t l s nguyn t ln nht trong mng a. Nu
mng khng c phn t l s nguyn t th tr v 0.
Cu 3:
Cho ma trn vung cc s thc A kch thc nxn (3<n<10). Hy xy dng cc
hm cho php thc hin cc yu cu sau:
1. Nhp gi tr cc phn t vo ma trn.
2. Lit k nhng phn t ti nhng dng l trong ma trn.
3. Tnh v tr v gi tr trung bnh cng ca nhng phn t m trong ma trn.
Cu 4:
Hy khai bo kiu d liu biu din thng tin ca mt mt hng
(MATHANG).
Bit mt mt hng gm:
- M hng (MaHang): Chui ti a 5 k t.
- Tn hng (TenHang): Chui ti a 30 k t.
- S lng (SoLuong): S nguyn 2 byte.
- n v tnh (DonViTinh): Chui ti a 5 k t.
- n gi (DonGia): Kiu s thc.
Cho danh sch gm n (n>0) mt hng. Vit cc hm sau:
1. Lit k cc mt hng c s lng ln hn 100.
2. Tm v tr v mt hng c thnh tin ln nht
(thnh tin=s lng*n gi).
Sp xp danh sch cc mt hng theo th t gim dn cu n gi.

S 05
Thi gian: 120 pht
(Khng tham kho ti liu)
====

Bi 1: Nhp s nguyn n (0<n20). Vit chng trnh xut n phn t u tin ca hai
mng A v B, cho bit cc gi tr c xc nh nh sau:
Mt s thi mu
Gio trnh Bi Tp K Thut Lp Trnh Trang 128

+ =
+ =
= =


2
B
A 2 B
B A A
1 B , 1 A
1 i
i i
2
1 i
2
1 i i
1 1

Bi 2: Vit chng trnh nhp vo ma trn vung cp n vi n nhp t bn phm. Hy
kim tra ma trn ny c phi l ma trn tam gic di hoc tam gic trn theo ng
cho ph khng?
V d:

Ma trn tam gic trn Ma trn tam gic di
Bi 3: Mi h s nhn vin gm:
h tn
nm sinh
lng c bn
Vit chng trnh thc hin cc cng vic sau:
a. Nhp n h s vi n nhp t bn phm.
b. In ra h tn v lng c bn ca nhn vin c lng c bn thp nht v nhn
vin c lng c bn cao nht.
c. Ghi xung file vn bn (vi tn file l hoso.txt) danh sch gm h tn, lng
c bn, ph cp v thc lnh ca cc nhn vin (mi nhn vin mt dng) bit
rng:
Ph cp = 30% lng c bn
Thc lnh = lng c bn + ph cp

S 06
Thi gian: 120 pht
(Khng tham kho ti liu)
====

Bi 1: Nhp vo mt dy s thc kt thc bi 0 hoc 20 phn t
a. Sp xp dy theo th t tng dn.
Mt s thi mu
Gio trnh Bi Tp K Thut Lp Trnh Trang 129
b. Cho bit dy c hi t khng? (Dy c hi t khi c na phn t tr ln nh
hn trung bnh cng ca dy).
Bi 2: Nhp vo ma trn cp mxn vi m v n nhp t bn phm. Hy kim tra xem ma
trn c cn bng theo ct hay khng? (Ma trn cn bng theo ct khi tng cc gi tr ca
cc ct bn tri bng tng cc gi tr ca cc ct bn phi, nu s ct l th khng tnh
ct gia).
V d:
8 4 5 8 9
3 5 7 4 6
4 9 7 5 1
Tng bn tri = 33 Tng bn phi = 33
Kt lun: Ma trn cn bng theo ct.
Bi 3: Mt Album ca nhc MP3 gm ti a 150 ca khc. Thng tin mi ca khc gm:
Tn ca khc
Tn nhc s
Tn ca s
Thi gian (tnh bng giy)
Vit chng trnh thc hin cc cng vic sau:
a. Nhp n ca khc vi n nhp t bn phm.
b. Xut tng thi gian ca cc ca khc (hin th theo dng hh:mm:ss) v cho bit tn
ca khc no c thi gian di nht.
Ghi xung file vn bn (vi tn file l mp3.txt) danh sch gm tn ca khc, tn nhc s,
tn ca s v thi gian (hin th theo dng hh:mm:ss), mi ca khc chim mt dng.

S 07
Thi gian: 120 pht
(Khng tham kho ti liu)
====

Cu 1. Tnh s hng th n ca h thc truy hi nh sau
f(0)=1, f(1)=2
f(n)=3f(n-1)+2f(n-2) (n>=2)
bng hai cch
a) Dng qui
b) Kh qui, dng vng lp
Mt s thi mu
Gio trnh Bi Tp K Thut Lp Trnh Trang 130
Cu 2. Xy dng mt cu trc c cc thnh phn sau
- M s hc sinh
- H v tn hc sinh
- im Ton
- im Vn
- im trung bnh=(im Ton+im Vn)/2
Vit chng trnh nhp d liu ca n hc sinh v lu vo mt tp tin c tn l
HOSOHS.DOC (hay mng 1 chiu c cu trc). Sau c d liu t tp tin
HOSOHS.DOC (hay mng 1 chiu c cu trc), sp xp theo th t im trung bnh
gim dn v xut d liu ca tng hc sinh ra mn hnh
Cu 3. Cho n l mt s nguyn dng, tnh gi tr biu thc sau bng cch vit chng
trnh s dng vng lp v ti u vng lp.
) , 3 , 2 , 1 (
!
1
! 4
1
! 3
1
! 2
1
1 K L = + + + + + n
n


S 08
Thi gian: 120 pht
(Khng tham kho ti liu)
====

PHN I: Chn cu tr li ng nht (5 im)
nh du cho vo cu tr li ng nht trong cc cu tr li cho mi cu hi.
Cu 1. on chng trnh sau s cho gi tr ca t:
for (t=i=0; (i<10) && (t<100); i++, t += 2*i);
a. 90 b. 100 c. 110 d. 120
Cu 2. Cho dy gm 12 phn t a
0
, a
1
, a
2
, , a
11
nh sau:
-9 -9 -5 -2 0 3 7 7 10 15
Dng thut ton tm nh phn tm v tr phn t x = -9, v tr tm c s l:
a. -1 b. 0 c. 1 d. 2
Cu 3. Chng trnh sau:
#include<conio.h>
#include<stdio.h>
int a=1, b=2, c=3;
int A(int &a, int b)
{
a += c + 2;
b -= a;
Mt s thi mu
Gio trnh Bi Tp K Thut Lp Trnh Trang 131
return a;
}
void main()
{
printf(" %d %d", A(b, c), a+c);
}
S in ra:
a. 7 5 b. 7 4 c. 7 3 d. 7 2
4. Chng trnh sau:
#include <conio.h>
#include <stdio.h>
int A(int a, int &b)
{
a += b + 2;
b -= a;
return a;
}
void main()
{
int x = 5;
printf(" %d %d", A(A(3, x), x), x);
}
S in ra:
a. 5 5 b. 5 7 c. 7 7 d. 7 5
5. on chng trnh di y khi thc thi s:
char buf1[100], buf2[100], *strptr1, *strptr2;
strcpy(buf1, "abcdefghijklmnopqrstuvwxyz");
strcpy(buf2, "Hello");
strptr1 = buf1 + 6;
strcpy(strptr1, buf2);
strptr2 = (strptr1 + 4);
strncpy(strptr2, buf2, 4);
printf("%s\n", buf1);
S in ra mn hnh:
a. abcdefHellHellopqrstuvwxyz b. ghijklmnHellotuvwxyz
c. abcdefghijklmnopqrstuvwxyz d. abcdefHellolmnopqrstuvwxyz
PHN II: Lp trnh (5 im)
Cu 1. Hy vit hm kim tra mt s nguyn khng n c phi l s nguyn t
hay khng, hm thc hin s tr v: 1 nu n l s nguyn t, 0 nu n khng l s
nguyn t
int LaSNT(unsigned int n);
Mt s thi mu
Gio trnh Bi Tp K Thut Lp Trnh Trang 132
Cu 2. Hy vit hm tm tng cc s nguyn t nm trong mng mt chiu a c n
phn t (unsigned int a[100], int n).
Cu 3. Vit hm xc nh v tr ca s nguyn t ln nht trn mng a c n phn
t (unsigned int a[100], int n)
(Lu : C th lm cc cu 2 v 3 m khng cn lm cu 1).
Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 133
PH LC 2 HNG DN VIT CHNG TRNH
TRN MI TRNG BORLAND C++ 3.1 (BC31)

I. CI T BC3.1
+ Vo th mc BC3.1 trn a CD chy file install.exe.

+ Nhn Enter.

+ G vo cha th mc ngun cha BC3.1 (V d trn:Gi s a cha
th mc BC3.1 trn a CD l D:) Nhn Enter.

Tn a cha BC31
Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 134
+ Enter tip (c th g li ng dn cha BC3.1 nu bc trn g sai).

+ Chn ng dn v tn th mc cn ci t BC3.1 ln a cng.
V d: Cn ci BC3.1 ln a C: tn th mc l BC31.
- thay i th mc v a ci t di chuyn vt sng (dng phm
mi tn ) n dng Directories nh hnh trn sau nhn Enter.

- Nhn tip Enter.

- G tn a v tn th mc cn ci t trong textbox Enter.
- Sau nhn ESC.
Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 135

- Tip tc nhn phm ESC.

- Tip theo kim tra xem th mc ci Windows c ng ng dn nh
dng Windows Dir hay khng (dng th 2). Nu khng ng th thay i
th mc cho ng, di chuyn vt sng n , thao tc tng t nh thay
i th mc BC3.1.
- Thng th khng cn thay i v cc my c ci Windows mc nh l
C:\Windows.
- Di chuyn vt sng n dng Start Installation nhn Enter bt u qu
trnh ci t.
Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 136

Luu : bc ny ch thay i th mc ci t BC3.1, th mc Windows
(nu c) cn nhng mc khc khng thay i.
+ Qu trnh ci t ang thc hin.

+ Nu trong qu trnh ci t gp thng bo sau:

Nhn phm C tip tc.
Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 137

Qu trnh ci t hon tt, nhn phm ESC cho n khi mt mn hnh ci t.
+ To mt th mc lu bi tp, chng hn D:\BaiTap lm th mc lm
vic ca C, trong qu trnh lm bi hay bin dch chy chng trnh th tt c cc
file u nm trong th mc BaiTap cho d qun l.
+ To Shortcut Borland C++3.1 (File bc.exe trong th mc BIN ca th mc
BC31 va ci t) Chn Properties Chn Tab Program g vo mc Cmd
line v Working ging nh hnh sau nu ci t BC3.1 trn a C:\BC3.1. Nhn
OK.
- Cmd line (ng dn n file chy BC): C:\BC3.1\BIN\BC.EXE.
- Working (th mc mi va to lu bi lm ): D:\Bai tap
Lu : ng ng dn th mc.

Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 138
II. CC BC VIT CHNG TRNH
a. Chun b vit chng trnh
Khi ng BC3.1

Vo menu Options\Environment\Editor chnh li Tab size l 4.

b. Cc phm chc nng chnh
F3: M file chng trnh c sn.
F2: Lu file
Lu : Chn ng dn v t tn file cho ng. Tn c ti a 8 k t,
phn ui khng cn nhp vo (mc nh l *.cpp).
F5: Phng to hoc tr v kch thc bnh thng ca ca s son tho.
F6: Chuyn qua li cc ca s son tho (nu m nhiu ca s).
F9: Bin dch chng trnh. Mc ch l kim tra li chng trnh.
Ctr+F9: Thc thi chng trnh (Run) khi chng trnh khng c li.
Alt+F5: Xem li mn hnh kt qu chng trnh chy trc .
Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 139
c. Vit chng trnh
Cu trc c bn ca chng trnh gm phn.
i. Phn khai bo th vin hm.
ii. Phn khai bo bin ton cc, khai bo kiu d liu, khai bo hm hay
khai bo hng (nu c).
iii. Cc hm con (nu c).
iv. Hm main().
Lu trnh by chng trnh.
d. Bin dch v sa li
Sau khi son tho xong chng trnh nhn F2 t tn chng trnh, m
bo chng trnh c th thc thi c, ta phi nhn F9 bin dch.
Nu khng c li, ta c th nhn Ctr+F9 thc thi chng trnh.

Nu my b loop nhn Ctrl+Break+Enter tr v mn hnh son tho.
Ngc li, ta cn phi sa li cho n khi ht li.

Cc bc thc hin khi c li:
i. Khi hin th mn hnh bo li, ta phi nhn phm Enter xut hin ca
s m t li (khng nhn phm ESC).
Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 140

ii. S dng phm mi tn ln xung duyt ln xung v xem m t li.
Khi di chuyn quan st vt sng bn trn khung ca s son tho
chng trnh. Thng thng vt sng s cho bit v tr li (c th ngay
chnh ti dng c li hoc trn hoc di mt dng). C nhiu cch sa
li, nhng n gin chng ta nn sa li t trn xung.
e. Mt s li thng gp
STT LI M T KHC PHC V D
LI C PHP
1
Statement
missing;
Thiu du; khi kt thc 1 lnh
B sung thm
du ; vo sau
khai bo bin
hay kt thc mt
lnh.
Sai:
int a
scanf(%d,&a)
Sa thnh:
int a;
scanf(%d,&a);
2
Compound
statement
missing }
Thiu du } khi kt thc khi
lnh hay lm.
B sung thm
du } vo tng
ng
Sai :
void main()
{
int a;
scanf(%d,&a);
if(a>0)
printf(Duong);
}
Sa thnh:
void main()
{
int a;
scanf(%d,&a);
if(a>0)
printf(Duong);
}
3
Unexpected
}
Thiu du { khi bt u khi
lnh, hm hay d du }
Kim tra xem c
d du } hoc
Sai :
void main()
Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 141
thiu du { v
sa tng ng.
{
int a;
scanf(%d,&a);
if(a>0)
printf(Duong);
}
}
Sa thnh:
void main()
{
int a;
scanf(%d,&a);
if(a>0)
printf(Duong);
}
4
Misplaced
else

Chm phy sau pht biu if
hoc khi lnh thc hin trong
pht biu if cha t trong cp
du ngoc {}
Sai :
if (a%2);
printf(a le);
else
if(a>10)
printf(a chan);
printf(, > 10);
else
printf(a < 10);
Sa thnh:
if (a%2)
printf(a le);
else
if(a>10)
{
printf(a chan );
printf(, > 10);
}
else
printf(a < 10);
5
For
statement
missing ;
Thiu thnh phn trong c php
ca vng lp for hoc qun
dng du chm phy (;) ngn
cch cc thnh phn , (Phi
c 2 du chm phy)
Kin tra cho
ng c php:
for(<Biu thc
khi>; <biu
thc iu kin
dng>; <biu
thc tng gim>)
Trong biu thc
gn hay nhiu
thnh phn th
miphn cch
nhau bi du
phy (,)
Sai :
for(int i=0, i<n; i++)
{
printf(a[%d]: ,i);
scanf(%d,&a[i]);
}
Sa thnh:
for(int i=0; i<n; i++)
{
printf(a[%d]: ,i);
scanf(%d,&a[i]);
}
6
Function call
missing )
Thiu du phy phn cch gia
phn nh dng v danh sch
bin trong hm printf v scanf.
Thm du phy
gia phn nh
dng v danh
sch bin.
Sai :
for(int i=0; i<n; i++)
{
printf(a[%d]: i);
scanf(%d,&a[i]);
Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 142
}
Sa thnh:
for(int i=0; i<n; i++)
{
printf(a[%d]: ,i);
scanf(%d,&a[i]);
}
LI KHAI BO
1
Declaration
terminated
incorrectly
Khai bo tn bin trng vi tn
hng nh ngha trc.
i tn bin.
Sai :
#define MAX 100
void main()
{
int MAX;
}
Sa thnh:
#define MAX 100
void main()
{
int x;
}
2
Multiple
declaration
for 'i'
Khai bo bin trng tn, khai
bo nhiu ln.
Kim tra v b
bt khai bo li
bin hoc i tn
bin khc.
Sai :
int i;
for(int i=0; i<n; i++)
scanf(%d,&a[i][j]);
Sa thnh:
int x;
for(i=0; i<n; i++)
scanf(%d,&a[i][j])
3
Undefined
symbol 'a'
S dng bin cha khai bo. Khai bo bin.
Sai :
printf(Nhap vao n:);
scanf(%d, &n);
Sa thnh:
int n;
printf(Nhap vao n:);
scanf(%d, &n);
4
Declaration
syntax error
Thiu du ; sau khai bo bin.
B sung du ;
sau khi kt thc
khai bo bin.
Sai :
int n
Sa thnh:
Int n;
TH VIN HM HOC SAI TN HM
1
Function
'printf'
should have
a prototype
Function
'scanf'
should have
a prototype
Function
'XXX'
should have
a prototype
Thiu sai bo th vin hm nu
s dng hm th vin, ngc li
phi kim tra xem c khai bo
nguyn mu hm, hoc gi sai
tn hm.
B sung
#include
<stdio.h>
#include
<conio.h>

Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 143
CC CNH BO
1
Possibly
incorrect
assignment
Dng k hiu trong php ton
quan h.
Dng k hiu
trong php so
snh
Sai :
if(n%2=0)
printf(n chan);
Sa thnh:
if(n%2==0)
printf(n chan);

2
Code has no
effect
Dng k hiu php tan quan h
cho php gn.
Dng k hiu
php toan s hc.

f. Debug
Mc d chng trnh khng cn li nhng khi chy chng trnh vn ra kt
qu sai, nhng li c th l:
Dng chm phy sau: if, else, for, while, m cha thc hin lnh.
nh dng nhp xut sai hay khai bo sai kiu d liu.
Chia cho 0.
Khng c iu kin dng (iu kin dng sai).
Phn tch thut ton thiu (cha vt ht cc trng hp) hoc sai.
Cc thao tc debug:
Nhn F7 hoc F8 chy tng bc (nu khng c li khi bin dch)

F7: i tng lnh ca hm con nu c gi hm.
F8: khng vo chi tit tng lnh khi gi n hm con (ch a ra kt qu ca
hm con).
Quan st vt sng bit chng trnh ang thc hin n v tr lnh no.
Nhn Ctrl+F7 (hoc nhn phm Insert nu c ca s Watch): Nhp vo
bin cn theo di gi tr cc bin khi thc hin xong lnh hay hm no .
Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 144

C th xa bin trn ca s Watch bng cch chn bin trn ca s Watch
v nhn phm Delete.
Nu khng thy ca s hin th gi tr bin (Watch) nhn Alt+W+W hoc
vo menu Window chn Watch.

Nu mun b qua mt on no (tc khng cn kim tra an ) th
nhn F4 chng trnh thc thi ti v tr dng ca du nhy ri dng li
(du nhy phi ti v tr nhng dng pha sau ca vt sng, nhn F6
chuyn qua li cc ca s).
Mun thay i gi tr ca bin ta dng phm Ctrl+F4 hin th ca s.

Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 145
Nhp vo tn bin Expression, chn nt Evaluate (ho nhn Enter),
Result s hin th kt qu ti thi im , sau nhp gi tr mi cho bin
ti New Value Enter (dng phm tab di chuyn v tr chn).

Ngoi ra c th nh du chng trnh thc thi n v tr nh du (khi
cha chy tng bc) dng phm F8 nh du ngay v tr du nhy. V tr
nh du s c vt sng mu .

C th nh du nhiu v tr khc nhau. Nhn Ctrl+F9 chng trnh thc
thi n v tr nh du theo th t t trn xung di, ng thi cng c th
dng phm F7 hoc F8 ging nh trn chy tng bc.
Ngoi ra, c th dng phm ALT+F5 xem kt qu xut trong qu trnh
debug ( kim tra nhp xut).
Trong qu trnh chy tng bc c th kt thc bng cch nhn Ctrl+F2.
g. Cc thao tc lin quan n ca s Watch
Di chuyn ca s Watch: Chn ca s Watch, nhn Ctrl+F5. Sau dng
phm mi tn di chuyn ca s ti v tr mi. Nhn phm Enter.
Thay i kch thc ca s Watch (khi ang chn bng Ctrl+F5 trn ca s
Watch) nhn Shift + phm mi tn ri nhn phm Enter.
Hng dn vit chng trnh trn mi trng Borland C++ 3.1
Gio trnh Bi Tp K Thut Lp Trnh Trang 146

TI LIU THAM KHO



1. PHM VN T: K thut lp trnh C: c s v nng cao. Nh Xut Bn
Khoa Hc K Thut 1996.
2. L HOI BC L HONG THI NGUYN TN TRN MINH
KHANG NGUYN PHNG THO: Gio trnh ngn ng C. Nh Xut
Bn i Hc Quc Gia Tp. H Ch Minh 2003.
3. NGUYN TN TRN MINH KHANG: Bi tp K thut lp trnh Tp 1.
Nh Xut Bn i Hc Quc Gia Tp. H Ch Minh 2004.
4. NGUYN NH T HONG C HI: Gio trnh l thuyt & Bi tp
ngn ng C. Nh Xut Bn Mi C Mau.
5. HUNH TN DNG HONG C HI: Bi tp ngn ng C t A n Z.
Nh Xut Bn Lao ng X Hi.
6. NGUYN THANH SN: Tp bi ging K thut lp trnh 2004.
7. TRN MINH THI: Tp bi ging K thut lp trnh 2005.
8. SANFORD LEESTMA LARRY NYHOFF: Pascal Programming and
Solving. Macmillan Publishing Company 1990.
9. JOHN R. HUBBARD: 455 Bi tp cu trc d liu ci t bng C++. Bn
dch ca Minh Trung, Gia Vit Nh Xut Bn Thng K.

Mc lc
Gio trnh Bi Tp K Thut Lp Trnh Trang i
MC LC
_

LI M U.........................................................................................................1
LCH TRNH THC HNH................................................................................2
CHNG 1 LU THUT TON (FLOWCHART) .............................3
I. TM TT L THUYT.................................................................................... 3
I.1. Khi nim.....................................................................................................3
I.2. Phng php duyt .....................................................................................3
I.3. Cc k hiu ..................................................................................................3
I.4. Cc cu trc iu khin c bn..................................................................4
a. Cu trc tun t .......................................................................................... 4
b. Cu trc la chn........................................................................................ 5
c. Cu trc lp................................................................................................. 6
d. Cc v d...................................................................................................... 8
II. BI TP............................................................................................................. 11
II.1. Bi tp c bn..................................................................................................11
II.2. Bi tp luyn tp v nng cao.........................................................................12
III. KT LUN...................................................................................................... 12
CHNG 2 CU TRC IU KHIN.......................................................13
I. TM TT L THUYT.................................................................................. 13
I.1. Cc k hiu ................................................................................................13
I.2. Cc kiu d liu c bn trong C...............................................................13
I.3. Bng k hiu cc php ton......................................................................14
I.4. Cc hm c bn.........................................................................................15
I.5. Cu trc r nhnh.....................................................................................15
a. Cu trc if.................................................................................................. 15
b. Cu trc if else...................................................................................... 16
I.6. Cu trc la chn switch..........................................................................16
I.7. Cu trc lp ...............................................................................................18
a. for .............................................................................................................. 18
b. while .......................................................................................................... 19
Mc lc
Gio trnh Bi Tp K Thut Lp Trnh Trang ii
c. do while................................................................................................. 20
I.8. break v continue ......................................................................................20
a. break.......................................................................................................... 20
b. continue ..................................................................................................... 21
II. BI TP............................................................................................................. 21
II.1. Phng php chy tay tng bc tm kt qu chng trnh.............21
II.2. Bi tp c bn...........................................................................................23
a. Cu trc if / if..else v switch.................................................................... 23
b. Cu trc lp............................................................................................... 25
II.3. Bi tp luyn tp v nng cao..................................................................29
III. KT LUN...................................................................................................... 30
CHNG 3 HM CON..................................................................................31
I. TM TT L THUYT.................................................................................. 31
I.1. Khi nim...................................................................................................31
I.2. V d...........................................................................................................31
I.3. Cu trc mt chng trnh C ...................................................................33
a. Khi khai bo ............................................................................................ 33
b. Hm chnh (main()) ................................................................................... 33
c. Cc hm con.............................................................................................. 33
d. Nguyn mu hm....................................................................................... 33
I.4. Cch xy dng mt hm con ....................................................................34
a. Kiu d liu ca hm................................................................................. 34
b. Tham s ..................................................................................................... 34
c. Tn hm..................................................................................................... 35
d. V d .......................................................................................................... 35
II. BI TP............................................................................................................. 37
II.1. Bi tp c bn...........................................................................................37
II.2. Bi tp luyn tp v nng cao..................................................................39
III. KT LUN...................................................................................................... 39
CHNG 4 MNG MT CHIU................................................................41
I. TM TT L THUYT.................................................................................. 41
I.1. Khi nim...................................................................................................41
I.2. Khai bo mng...........................................................................................41
Mc lc
Gio trnh Bi Tp K Thut Lp Trnh Trang iii
I.3. Truy xut phn t ca mng ....................................................................42
II. BI TP............................................................................................................. 43
II.1. Mt s k thut c bn...............................................................................43
a. K thut t c hiu................................................................................... 43
b. K thut t lnh canh................................................................................ 44
II.2. Bi tp c bn............................................................................................45
a. Nhp xut mng mt chiu........................................................................ 45
b. Tm kim trn mng mt chiu.................................................................. 46
c. m Tn sut.......................................................................................... 47
d. Tnh tng Trung bnh c iu kin ......................................................... 48
e. Sp xp ...................................................................................................... 49
f. Xo............................................................................................................. 50
g. Chn........................................................................................................... 50
h. Tch / ghp mng ...................................................................................... 51
II.3. Bi tp luyn tp v nng cao...................................................................53
III. KT LUN...................................................................................................... 56
CHNG 5 CHUI K T.........................................................................57
I. TM TT L THUYT.................................................................................. 57
I.1. Khi nim...................................................................................................57
I.2. Khai bo chui...........................................................................................57
I.3. Cc thao tc trn chui .............................................................................57
a. Nhp chui ................................................................................................ 57
b. Xut chui.................................................................................................. 58
c. Cc hm th vin (string.h)....................................................................... 58
d. V d .......................................................................................................... 60
II. BI TP............................................................................................................. 60
II.1. Bi tp c bn............................................................................................60
II.2. Bi tp luyn tp v nng cao...................................................................62
III. KT LUN.......................................................................................................... 63
CHNG 6 MNG HAI CHIU.................................................................64
I. TM TT L THUYT.................................................................................. 64
I.1. Khi nim...................................................................................................64
I.2. Khai bo mng...........................................................................................64
Mc lc
Gio trnh Bi Tp K Thut Lp Trnh Trang iv
I.3. Truy xut phn t ca mng ....................................................................64
I.4. Ma trn vung v cc khi nim lin quan..............................................65
a. Khi nim .................................................................................................. 65
b. Tnh cht ca ma trn vung..................................................................... 65
II. BI TP............................................................................................................. 66
II.1. Mt s k thut c bn...............................................................................67
II.2. Bi tp c bn...........................................................................................69
a. Bi tp nhp xut ...................................................................................... 69
b. Bi tp tnh tng........................................................................................ 69
c. Bi tp tm kim......................................................................................... 70
d. Bi tp m................................................................................................ 70
e. Bi tp sp xp .......................................................................................... 71
f. Bi tp Thm Xo Thay th................................................................. 72
II.3. Bi tp luyn tp v nng cao..................................................................73
III. KT LUN...................................................................................................... 77
CHNG 7 KIU D LIU C CU TRC............................................78
I. TM TT L THUYT.................................................................................. 78
I.1. Khi nim...................................................................................................78
I .2. nh ngha kiu d liu............................................................................78
I.3. Khai bo.....................................................................................................79
I.4. Truy xut....................................................................................................80
I.5. V d minh ho..........................................................................................81
I.6. Mng cu trc............................................................................................82
I.7. Nguyn tc vit chng trnh c mng cu trc .....................................82
II. BI TP............................................................................................................. 91
II.1. Bi tp c bn............................................................................................91
II.2. Bi Tp Luyn Tp....................................................................................92
III. KT LUN...................................................................................................... 96
CHNG 8 TP TIN .....................................................................................97
I. TM TT L THUYT.................................................................................. 97
I.1. Khi nim...................................................................................................97
I.2. Thao tc vi tp tin...................................................................................97
a. Khai bo .................................................................................................... 97
Mc lc
Gio trnh Bi Tp K Thut Lp Trnh Trang v
b. M tp tin .................................................................................................. 97
c. Cc hm c ghi ni dung tp tin ............................................................ 98
d. ng tp tin............................................................................................... 99
e. Cc thao tc khc trn tp tin................................................................... 99
f. V d minh ho .......................................................................................... 99
I.3. Cc v d minh ho ................................................................................100
a. Tp tin vn bn........................................................................................ 100
b. Tp tin nh phn ...................................................................................... 102
II. BI TP........................................................................................................... 103
II.1. Bi tp c bn..........................................................................................103
II.2. Bi tp luyn tp v nng cao.................................................................105
III. KT LUN.................................................................................................... 108
CHNG 9 QUI .....................................................................................109
I. TM TT L THUYT................................................................................ 109
I.1. Khi nim.................................................................................................109
I.2. Phn loi qui.......................................................................................109
a. qui tuyn tnh ..................................................................................... 109
b. qui nh phn....................................................................................... 110
c. qui phi tuyn ...................................................................................... 112
d. qui h tng....................................................................................... 113
I.3. Tm hiu cch hot ng ca hm qui ..............................................114
I.4. V d.........................................................................................................115
II. BI TP........................................................................................................... 116
II.1. Bi tp c bn................................................................................................116
II.2. Bi tp luyn tp v nng cao.......................................................................117
III. KT LUN.................................................................................................... 117
CHNG 10 LP TRNH THEO PHNG PHP PROJECT.............118
I. MC TIU....................................................................................................... 118
II. PHNG PHP........................................................................................... 118
II.1. To mt project mi.................................................................................118
II.2. M project c sn ....................................................................................118
II.3. Mt s lu .............................................................................................119
II.4. V d minh ho ..............................................................................................119
Mc lc
Gio trnh Bi Tp K Thut Lp Trnh Trang vi
III. BI TP ........................................................................................................ 123
PH LC 1 THI MU ...........................................................................124
PH LC 2 HNG DN VIT CHNG TRNH TRN MI
TRNG BORLAND C++ 3.1 (BC31) ...........................................................133
I. CI T BC3.1............................................................................................... 133
II. CC BC VIT CHNG TRNH......................................................... 138
a. Chun b vit chng trnh ..................................................................... 138
b. Cc phm chc nng chnh...................................................................... 138
c. Vit chng trnh..................................................................................... 139
d. Bin dch v sa li ................................................................................. 139
e. Mt s li thng gp ............................................................................. 140
f. Debug ...................................................................................................... 143
g. Cc thao tc lin quan n ca s Watch............................................... 145
TI LIU THAM KHO.................................................................................146
MC LC............................................................................................................... i