Você está na página 1de 215

Chng:

Tn hc phn (Ting Vit): C s lp trnh 1


Tn hc phn (Ting Anh): Introduction to Programming 1
1. Mc tiu ca hc phn:
- Kin thc: nm vng cc kin thc c bn v lp trnh my tnh, c th
nh nm r cc thnh phn trong ngn ng lp trnh C, cc thut ton c
bn, cc dng bi tp lp trnh c bn, cc cu trc d liu c sn trong C,
cch t chc chng trnh thnh cc hm, to thi quen lp trnh tt
- K nng: S dng thnh tho ngn ng lp trnh C, c k nng gii quyt
nhng bi ton lp trnh c bn s dng ngn ng C, c k nng t chc
chng trnh thnh cc hm.
- Thi , chuyn cn: Chun b bi trc khi n ln, Tham gia lp hc
y
2. Chun u ra ca hc phn: Sau khi hon thnh kha hc, sinh vin c
th:
CR1: Nm vng c cc giai on vit mt chng trnh my tnh.
CR2: Hiu r ngn ng lp trnh C.
CR3: Nm vng cc CTDL, thut ton gii quyt cc bi ton tin hc c
bn.
CR4: Bit cch t chc chng trnh thnh cc hm.
3. Tm tt ni dung hc phn:
C s lp trnh 1 l hc phn nn tng cho nhng hc phn khc lin quan
n lp trnh sau ny. Hc phn s gii thiu cho sinh vin cc bc son
tho, bin dch, lin kt khi vit mt chng trnh my tnh. Hc phn tp
trung gii thiu ngn ng lp trnh C, nh cc php ton, cc cu lnh chn
la, cc cu lnh lp, cc hm xy dng sn, cc cu trc d liu c sn
nh mng (array), cu trc (struct), xu k t (string), Ngoi ra hc phn
cn trnh by cc phng php, thut ton gii quyt cc bi ton tin hc
c bn v tng bc hc cch cch t chc chng trnh thnh cc hm.
Chng:
Chng:

Chng 1: Gii thiu ngn ng lp trnh C


Mc tiu:
Tm tt ni dung:

Tm tt: Gii thiu v lch s, cc khi nim c bn v lp trnh, v ngn ng


lp trnh C: cc c php n gin v nhp xut vi chng trnh u tin. Gii
thiu cc khi nim, cc t kha, cc loi hng s, cc kiu d liu, khai bo
bin, cc k hiu php ton v cc hm nhp xut trong ngn ng C.

1.1Lch s ngn ng lp trnh C:


Ngn ng C do Dennis Ritchie xy dng t nm 1972 ti phng th
nghim Bell Telephone vi mc ch to ngn ng vit h iu hnh
UNIX, song nh c cc tnh nng u vit v tnh mm do nn c gii
tin hc chp nhn.
Tc gi: Dennis Ritchie (9/9/1941 12/10/2011)

Thi gian: 1969 1973 ng lm vic ti AT&T Bell Labs


Nm 1978, xut bn quyn sch The C programming language do B.W
Kernighan v Dennish Ritchie vit v c ph bin rng ri n nay.
L do thit k, xy dng C:
Ken Thompson v Dennis Ritchie xy dng h iu hnh Unix
bng ngn ng Assembly.
Chng:

Cn mt ngn ng c kh nng lp trnh cp cao ( d s dng)


v mnh nh ngn ng cp thp Assemply
t tn C da trn mt s nguyn tc v tng ca ngn ng B
Mt s ngn ng da trn C
C++
Java
C#
C l ngn ng lp trnh cp cao, c s dng rt ph bin lp trnh h
thng cng vi Assembler v pht trin cc ng dng do cc c im sau:

- B lnh ph hp vi phng php lp trnh c cu trc.


- Kiu d liu phong ph, cho php nh ngha thm nhng kiu d liu
mi ph hp vi yu cu ca bi ton.
- Linh ng v c php v t t kha.
1.2im mnh v im yu ca C:
im mnh
Tc cao nh Assembly, rt mnh v linh ng, c kh nng th
hin bt c tng no.
Chy c trn nhiu h iu hnh: C tnh kh chuyn, t thay i
trn cc h thng my tnh khc nhau. c s dng rng ri bi
cc nh lp trnh chuyn nghip.
C th vin chun. Lp trnh n th, ti s dng thng qua hm.
im yu
Chng trnh C d sinh li
Kh hiu
Kh chnh sa

1.3Tng quan ngn ng lp trnh C:


Chng trnh my tnh (cn gi l phn mm) l tp hp cc lnh ch dn
tng bc cho my tnh qu trnh thc hin c mt cng vic hoc t
Chng:

c mt kt qu c th. Nhng lnh ny do cc lp trnh vin vit ra thng


qua mt ngn ng lp trnh c th. V d nh phn mm x l vn bn, phn
mm x l nh, phn mm k ton, phn mm qun l nhn s, v.v Ty
theo chc nng ca mi chng trnh th chng trnh c phn ra nhiu
loi: chng trnh h thng, chng trnh tin ch, chng trnh ng dng.

Theo nh ngha ca Niklaus Wirth th:


Chng trnh = Gii thut + Cu trc d liu

Chng trnh v gii thut u da trn ba cu trc iu khin c bn:


(1) Tun t (Sequential): Thc hin tun t t trn xung, mi lnh u
thc hin ng mt ln.
(2) La chn, r nhnh (Selection): Thc hin mt hoc mt s lnh trong
dy cc lnh ca nhiu trng hp.
(3) Lp li (Repetition): Thc hin lnh mt s ln.
Ngn ng lp trnh l h thng cc k hiu tun theo cc qui c v ng
php v ng ngha, dng xy dng thnh cc chng trnh cho my tnh.

Mt chng trnh c vit bng mt ngn ng lp trnh c th (v d


Pascal, C, Java, v.v) gi l chng trnh ngun, chng trnh dch lm nhim
v dch chng trnh ngun thnh chng trnh thc thi c trn my tnh.

Nhm h tr thun tin cho ngi lp trnh, mt s phn mm lp trnh


tch hp cc cng c thit k, vit chng trnh ngun, bin dch v kim tra.

Chng trnh vit bng ngn C gm c:


Danh sch cc cu lnh v cc hm tnh ton my tnh thc hin
Cc cu lnh c vit tun t t trn xung di
Cc cu lnh c thc hin tun t t trn xung di
File cha chng trnh C l file vn bn co ui:
*.h
*.c, *.cpp
Chng:

Tp k t ca ngn ng C: Ngn ng lp trnh C s dng cc k t sau


to nn cc t vng (t khoa) v tn cc i tng
B ch ci 26 k t Latinh thng: a, b, c, , z
B ch ci 26 k t Latinh hoa: A, B, , Z
B ch s thp phn: 0, 1, , 9
Ngn ng lp trnh C phn bit ch HOA v ch thng
Mt s t vng ca ngn ng C:
Cc t dnh ring trong ngn ng.
Khng th s dng t kha t tn cho bin, hm, tn chng trnh
con.
Trong C c cc t kha sau:
void, int, short, long, float, double, char, signed, unsigned, const,
static
struct, enum, union, typedef
if, else, switch, case, default
for, while, do, break, continue
return, sizeof
Nhn xt:
T vng trong ngn ng C cn c gi l t kha Keyword
Cc t vng ca ngn ng C lun lun vit thng
Tn/nh danh (Identifier)
o Mt dy k t dng ch tn mt hng s, hng k t, tn mt
bin, mt kiu d liu, mt hm mt hay th tc.
o Khng c trng vi cc t kha v c to thnh t cc ch ci
v cc ch s nhng bt buc ch u phi l ch ci hoc _.
o S k t ti a trong mt tn l 255 k t v c dng k t _
chen trong tn nhng khng cho php chen gia cc khong trng.

Vn phm ca ngn ng C
Chng:

Tp quy tc, sp xp cc t kha trong ngn ng to nn cc cu lnh


C php l g?
Quy tc vit 1 cu lnh
Chng:

1.4 Chng trnh C u tin:


#include<stdio.h>
int main( )
{
printf(Chuong trinh dau tien);
return 0;
}
chuyn chng trnh C thc hin c trn my, chng ta cn chuyn
chng trnh C sang ngn ng my
Cc giai on chuyn i:
Preprocessing
Compiling
Linking
Chng:
Chng:

Hm main( )
Mt chng trnh C gm nhiu hm nhng c duy nht mt hm
main().
Hm main( ) l im bt u thc thi chng trnh v gi cc hm
khc thc hin.
C php 1:
void main( )
{
}
C php 2:
void main(int argc, char *argv[ ] )
{
}
C php 3:
int main( )
{

return 0;
}
C php 4:
int main (int argc, char *argv[ ])
{

return 0;
}
Cu lnh C:
L mt ch th trc tip, hon chnh nhm ra lnh cho my tnh thc
hin mt s tc v nht nh no .
Chng:

Trnh bin dch b qua cc khong trng (hay tab hoc xung dng)
chen gia lnh.
L mt php ton, li gi hm, mt lnh iu khin chng trnh. Cu
lnh C lun kt thc bng ;
V d:
a=2912;
a = 2912;
a
=
2912;
Phn loi
Cu lnh n: ch gm mt cu lnh.
Cu lnh phc (khi lnh): gm nhiu cu lnh n c
bao bi { v }
V d:
a = 2912; // cu lnh n.
{ // cu lnh phc/khi lnh
a = 2912;
b = 1706;
}
Xut d liu c bn:
Xut d liu ra mn hnh chng ta dng hm printf trong th vin stdio.h
#include <stdio.h> (standard input/output)
C php:
printf(nh dng, cc gi tr );
Trong :
nh dng: l cch trnh by thng tin xut v c t trong cp
nhy kp .

Vn bn thng (literal text):


Chng:

c xut y ht nh lc g trong chui nh dng.


V d
Xut chui Hello World
printf(Hello ); printf(World);
printf(Hello World);
Xut chui a + b
printf(a + b);
K t iu khin (escape sequence):
Gm du \ v mt k t nh trong bng sau:

K t iu
ngha
khin

\a Ting chung
\b Li li mt bc
\n Xung dng
\t Du tab
\\ In du \
\? In du ?
\ In du

V d
printf(\t); printf(\n);
printf(\t\n);

c t (conversion specifier):
Gm du % v mt k t.
Xc nh kiu ca bin/gi tr mun xut.
Cc i s chnh l cc bin/gi tr mun xut, c lit
k theo th t cch nhau du phy.
Chng:

c t ngha Kiu d liu

%c K t char
%d, %ld S nguyn c du char, int, short, long
%f, %lf S thc float, double
%s Chui k t char[], char*
%u S nguyn khng unsigned
du int/short/long

V d:
int a = 10, b = 20;
printf(%d, a); Xut ra 10
printf(%d, b); Xut ra 20
printf(%d %d, a, b); Xut ra 10 20
float x = 15.06;
printf(%f, x); Xut ra 15.060000
printf(%f, 1.0/3); Xut ra 0.333333
Phi hp cc thnh phn

int a = 1, b = 2;

Xut 1 cong 2 bang 3 v xung dng.

printf(%d, a); // Xut gi tr ca bin a

printf( cong ); // Xut chui cong

printf(%d, b); // Xut gi tr ca bin b

printf( bang ); // Xut chui bang

printf(%d, a + b); // Xut gi tr ca a + b

printf(\n); // Xut iu khin xung dng \n


Chng:

printf(%d cong %d bang %d\n, a, b, a+b);

nh dng xut:

C php
nh dng xut s nguyn: %nd
nh dng xut s thc: %n.kd
int a = 1706;

float x = 176.85;

printf(%10d, a);printf(\n);

printf(%10.2f, x);printf(\n);

printf(%.2f, x);printf(\n);

Du chm phy ;
Dng phn cch cc cu lnh.
V d: printf(Hello World!); printf(\n);
Nhp d liu c bn:
Th vin
#include <stdio.h> (standard input/output)
C php
scanf(<chui nh dng>[, <s1>, <s1>, ]);
<chui nh dng> ging nh dng xut nhng ch c cc c t.
Cc i s l tn cc bin s cha gi tr nhp v c t trc
du &
V d: cho a v b kiu s nguyn
scanf(%d, &a); // Nhp gi tr cho bin a
scanf(%d, &b); // Nhp gi tr cho bin b
Chng:

scanf(%d%d, &a, &b);


Cc cu lnh sau y sai
scanf(%d, a); // Thiu du &
scanf(%d, &a, &b);// Thiu %d cho bin b
scanf(%f, &a); // a l bin kiu s nguyn
scanf(%9d, &a); // khng c nh dng
scanf(a = %d, b = %d, &a, &b);
Ch thch trong C:
Ch thch 1 dng
//
/* */
Ch thch nhiu dng
/*


*/
V d:
/*Ho & Ten: NVA*/,
// MSSV: 0712078

Cc bc vit chng trnh

a bi ton n gin ngoi thc t trn bng my tnh (lp trnh cho
my tnh gii) th chng ta cn phi thc hin qua cc bc nh:

- M t cc bc gii bi ton.
- Chn ngn ng lp trnh v lp trnh to thnh mt chng trnh
hon chnh.
- Thc thi chng trnh: nhp vo d liu v nhn kt qu.

Kim th chng trnh: kim tra xem chng trnh c thc thi ng theo yu
cu cho tt c cc trng hp hay khng.
Chng:

Tm tt:

Bi tp:
Chng:

Chng 2 : S dng bin v biu thc


Mc tiu:
Tm tt ni dung:

2.1 Khi nim bin:


Trong i s:
Bin (Variable) l 1 k hiu (thng l 1 k t) thay th cho 1 gi
tr s cha bit trong 1 phng trnh (n s)
V d 1: Cc bin thng thng c dng:
x, y (cc gi tr s thc cha bit)
t (thi gian)
r (bn knh)
V d 2: 5x2 + 7x 12 = 0
Trong Lp trnh:
Bin (Variable) l mt vng nh trong my tnh c t bng 1
ci tn (c th gm 1 hay nhiu k t).
Chng ta dng bin lu d liu vo b nh my tnh dng
sau ny

Khai bo bin:
dng 1 vng nh trong my tnh, chng ta phi khai bo 1 bin trc
khi dng
C php 1:
Kieu tenBien;
Chng:

C php 2:
Kieu tenBien1, tenBien2, ;
V d:
int main( )
{
int a;
int x, y;

}
Khi to bin:
Php gn: Php gn l 1 php ton dng chp mt gi tr vo bin
C php 1:
tenBien = giaTri;
C php 2:
tenBien = bieuThuc;
C php 3:
Kieu tenBien = bieuThuc;
V d 1:
int main( )
{
int a, b, c;
a = 7;
b = 9;

c = a+b;
printf(Ket qua: %d, c );
return 0;
}
V d 2:
Chng:

int main ( )
{
int a;
a = 5;
a = a+7;
printf(Ket qua: %d, a);
return 0;
}
Ch :
Lun lun khai bo bin trc khi s dng
Lun lun khi to bin trc khi s dng
Gi tr chp vo bin: c th l h nh phn, bt phn, thp phn, thp lc
phn, s khoa hc (s thc),
V d:
int a = 1506; // 150610 h thp phn
int b = 01506; // 15068 h bt phn
int c = 0x1506; // 150616 (0x hay 0X) h thp lc phn
float d = 15.06e-3; // 15.06*10-3 (e hay E) s khoa hc.
Quy tc t tn bin:
Ngn ng C dng cc k t sau t tn bin:
a, b, c, , z
A, B, , Z
0, 1, , 9
_
Quy tc:
Bt u tn bin l k t ch hay k t _
Khng dng t kha t tn bin
Tn bi n khng co khoang tr ng
V d:
Chng:

int main( )
{
int a, b, c, d;
int x, y, z;
int i, j;
int diemToan, diemLapTrinhC;
int soTrang;
int So Luong Sinh Vien; // Sai: do tn bin c khong trng.

}
2.2 Cc kiu d liu c bn:
Cc thng tin lu tr trong my tnh gi l d liu (data). Mi d liu
thuc mt kiu d liu no .

Kiu d liu l mt tp hp cc gi tr c cng mt tnh cht v tp hp


cc php ton thao tc trn cc gi tr . Kiu d liu c chia ra lm hai loi:
Kiu d liu c s v kiu d liu c cu trc.

Cu trc d liu l cch thc t chc cc d liu thnh mt n v hon


chnh biu din c thng tin cn quan tm. Cu trc d liu c th hiu l
kiu d liu c cu trc, bao gm cc thnh phn d liu v cc thao tc tc
ng ln cu trc d liu .

Kiu d liu c s l kiu d liu m gi tr ca n l n nht. Cc ngn


ng lp trnh u xy dng sn cc kiu d liu ny. Kiu d liu c s c
dng to cc kiu d liu c cu trc.

V d: Trong ngn ng lp trnh C, kiu int gi l kiu c s v kiu ny bao


gm cc s nguyn t -2,147,483,648 n 2,147,483,647 v cc php ton +,
-, *, /, %, v.v...
Trong C chun c 4 kiu d liu c bn
int, char, float v double
Chng:

Chng ta c th thay i kch thc b nh ca bin bng cc b t


short, long
Chng ta c th thay i min gi tr b nh ca bin bng cc b
t
signed, unsigned
Kiu S nguyn
Cc s nguyn trn my tnh khng c k t ngn cch phn trm, phn
ngn,
V d: 500 7965 1500
Cc kiu s nguyn (c du)
n bit c du: 2n 1 +2n 1 1
Cc kiu s nguyn (khng du)
n bit khng du: 0 2n 1

Kiu Min gi tr S bit

char -128 127 8

unsigned char 0 255 8

-2,147,483,648
int 32
2,147,483,647

unsigned int 0 4,294,967,295 32

-2,147,483,648
long [int] 32
2,147,483,647

unsigned long [int] 0 4,294,967,295 32

short [int] -32,767 32,768 16

unsigned short [int] 0 65,535 16

Kiu S thc (floating-point)


Chng:

Cc s thc trn my tnh gm c phn nguyn v phn l c cch


nhau bng k t du chm .
V d:
500.0 7.965 1.500
5e2 5e+2 5e-2
0.5 .5
17.06 = 1.706*10 = 1.706*101

Kiu Min gi tr S bit

float -3.4e38 to +3.4e38


32
38
(*) (3.4*10 3.4*10 )
38

double -1.7e308 to +1.7e308


64
(**) (1.7*10308 1.7*10308 )

(*) chnh xc n (Single-precision) chnh xc n 7 s l.


(**) chnh xc kp (Double-precision) chnh xc n 19 s l.

Kiu K t
Cc k t trong C c lu di dng m s.
Tp cc m s ca tt c cc k t c gi l bng m k t.
Bng m ph bin l bng m ASCII gm 256 k t
phn bit cc k t v cc bin trong chng trnh C, cc k t c
t trong 2 du nhy n. V d: A, a,

Kiu S bit

char 8

Chnh l kiu s nguyn do:


Lu tt c d liu dng s.
Khng lu trc tip k t m ch lu m ASCII ca k t .
V d
Chng:

Lu s 65 tng ng vi k t A
Lu s 97 tng ng vi k t a.

Bng m ASCII
Chng:

Cc k t c bit
Kiu lun l (Boolean)
c im:
C ngm nh mt cch khng tng minh:
false (sai): gi tr 0.
true (ng): gi tr khc 0, thng l 1.
C++: bool
V d:
0 (false), 1 (true), 2 (true), 2.5 (true)
1 > 2 (0, false), 1 < 2 (1, true)
Kiu d liu c cu trc
Kiu d liu c cu trc l kiu d liu m cc gi tr ca n l s kt hp
ca cc gi tr khc.

V d: Kiu chui k t trong ngn ng lp trnh C l mt kiu d liu c


cu trc.

Cc ngn ng lp trnh u cho php ngi lp trnh nh ngha kiu d


liu c cu trc nhm p ng nhu cu ring ca tng bi ton nh danh sch
lin kt, cu trc cy, v.v

2.3 Cc php ton c bn:


Khi nim v biu thc:
To thnh t cc ton t (Operator) v cc ton hng (Operand).
Ton t tc ng ln cc gi tr ca ton hng v cho gi tr c kiu
nht nh.
Ton t: +, , *, /, %.
Ton hng: hng, bin, li gi hm...
V d
2 + 3, a / 5, (a + b) * 5,
Php ton s hc:
Chng:

C hai ton hng trong biu thc, cn gi l php ton hai ngi,
gm c:
+ Cng
- Tr
* Nhn
/ Chia
% Chia ly s d
V d
a = 1 + 2; b = 1 2; c = 1 * 2; d = 1 / 2;
e = 1*1.0 / 2; f = float(1) / 2; g = float(1 / 2);
h = 1 % 2;
x = x * (2 + 3*5); x *= 2 + 3*5;
K thut tch cc ch s ra khi s
Tch hng n v:
donvi = num % 10;
B hng n v:
num = num / 10;
Php ton tng/gim:
Khi cn tng gi tr ca bin ln 1 hay gim gi tr ca bin i 1
chng ta nn dng ton t tng/gim.
Cn gi l php ton 1 ngi.
Ch c mt ton hng trong biu thc.
++ (tng 1 n v), -- (gim 1 n v)
C php 1:
tenBien1 = ++tenBien2;
tenBien1 = --tenBien2;
t trc ton hng:
V d ++x hay --x: thc hin tng/gim trc.
C php 2:
Chng:

tenBien1 = tenBien2++;
tenBien1 = tenBien2--;
t sau ton hng
V d x++ hay x--: thc hin tng/gim sau.
V d:
x = 10; y = x++; // y = 10 v x = 11
x = 10; y = ++x; // x = 11 v y = 11
Php ton trn tng bit:
Tc ng ln cc bit ca ton hng (nguyn).
Gm cc php ton sau:
~ Php ph nh cc bit (hay ly s b 1)
& Php And cc bit tng ng
| Php Or cc bit tng ng
^ Php Xor cc bit tng ng
<< Php dch sang tri cc bit
>> Php dch sang phi cc bit
Chng:

V d:
void main()
{
int a = 5; // 0000 0000 0000 0101
int b = 6; // 0000 0000 0000 0110
int z1, z2, z3, z4, z5, z6;
z1 = a & b; // 0000 0000 0000 0100
z2 = a | b; // 0000 0000 0000 0111
z3 = a ^ b; // 0000 0000 0000 0011
z4 = ~a; // 1111 1111 1111 1010
z5 = a >> 2; // 0000 0000 0000 0001
z6 = a << 2; // 0000 0000 0001 0100
}
Php gn:
=
Khi nim
Thng c s dng trong lp trnh.
Gn gi tr cho bin.
C php
<bin> = <gi tr>;
<bin> = <bin>;
Chng:

<bin> = <biu thc>;


C th thc hin lin tip php gn.
V d:
void main()
{
int a, b, c, d, e, thuong;
a = 10;
b = a;
thuong = a / b;
a = b = c = d = e = 156;
e = 156;
d = e;
c = d;
b = c;
a = b;
}
Php gn kp:
+= -= *= /= %=
&= |= ^= <<= >>=
Nhn xt:
Php gn = lun nm bn phi
Bn tri php gn phi l bin
Cc ton t quan h:
So snh 2 biu thc vi nhau
Cho ra kt qu 0 (hay false nu sai) hoc 1 (hay true nu ng)
==, >, <, >=, <, <=, !=
V d
s1 = (1 == 2); s2 = (1 != 2);
Chng:

s3 = (1 > 2); s4 = (1 >= 2);


s5 = (1 < 2); s6 = (1 <= 2);
p kiu:
Cc gi tr thuc kiu ny c th chuyn sang kiu khc thng qua php
ton p kiu
C php:

Ly kch thc b nh v a ch ca bin:


Ly kch thc b nh ca kiu d liu
C php:

Ly a ch ca bin
C php:

Cc php ton lun l:


T hp nhiu biu thc quan h vi nhau.
&& (and), || (or), ! (not)

V d:
s1 = (1 > 2) && (3 > 4);
s2 = (1 > 2) || (3 > 4);
Chng:

s3 = !(1 > 2);


Php ton iu kin
y l ton t 3 ngi (gm c 3 ton hng)
<biu thc 1> ? <biu thc 2> : <biu thc 3>
<biu thc 1> ng th gi tr l <biu thc 2>.
<biu thc 1> sai th gi tr l <biu thc 3>.
V d
s1 = (1 > 2) ? 2912 : 1706;
int s2 = 0;
1 < 2 ? s2 = 2912 : s2 = 1706;
Php ton phy (Ton t phy)
Cc biu thc t cch nhau bng du ,
Cc biu thc con ln lt c tnh t tri sang phi.
Biu thc mi nhn c l gi tr ca biu thc bn phi cng.
V d:
x = (a++, b = b + 2);
a++; b = b + 2; x = b;
u tin ca cc php ton:

Ton t u tin

() [] -> .
! ++ -- - + * (cast) & sizeof
* / %

+ -

<< >>

< <= > >=

== !=

&

Chng:

|
^
&&

||

?:

= += -= *= /= %= &=

,
Chng:

Quy tc thc hin:


Thc hin biu thc trong ( ) su nht trc.
Thc hin theo th t u tin cc ton t.
=> T ch ng thm ( )
V d:
n = 2 + 3 * 5;
=> n = 2 + (3 * 5);
a > 1 && b < 2
=> (a > 1) && (b < 2)
2.4 Xut d liu:
Xut d liu trong cc bin ra mn hnh trong C c th dng hm printf
trong th vin stdio.h
C php:
printf(nh dng, bien1, bien2, );
Trong :
nh dng:
%d
%f, %lf
%c, %s
%10d
%.2f %.2lf
%5.2f %5.3lf
Xut s nguyn:
printf(%d%d, bien1, bien2, );
Xut s thc:
float bien1, bien2;
printf(%f%f, bien1, bien2, );

double bien1, bien2;
Chng:

printf(%lf%lf, bien1, bien2, );


Xut s k t:
printf(%c%c, bien1, bien2, );
Xut xu k t:
printf(%s%s, bien1, bien2, );
2.5 Nhp d liu:
Nhp d liu t bn phm trong C c th dng hm scanf trong th vin
stdio.h
C php:
scanf(nh dng, &bien1, &bien2, );
Trong :
nh dng:
%d
%f %lf
%c
%s
Nhp s nguyn:
scanf(%d%d, &bien1, &bien2, );
Nhp s thc:
float bien1, bien2;
scanf(%f%f, &bien1, &bien2, );
.
double bien1, bien2;
scanf(%lf%lf, &bien1, &bien2, );
Nhp k t:
scanf(%c%c, &bien1, &bien2, );
Nhp xu k t:
scanf(%s%s, &bien1, &bien2, );
2.6 Bin hng:
Chng:

Khi nim: Bin hng l 1 bin c gi tr khng b thay i trong sut qu


trnh hot ng ca chng trnh
C php 1:
const kieu tenBien = giaTri;
C php 2:
#define tenBien giaTri;
V d
#define MAX 100 // Khng c du ; cui dng
#define PI 3.14 // Khng c du ; cui dng
const int MAX = 100;
const float PI = 3.14;
Hng k t v hng chui:
Hng k t: A, a,
Hng chui: Hello World!, Nguyen Van A
Ch : A khc A
2.7 Mt s hm ton hc:
Th vin <math.h> cha mt s hm ton thng dng sau:
pow(x, y), sqrt(x)
floor(x), ceil(x)
fabs(x);
exp(x), log(x), log10(x)
sin(r), cos(r), tan(r), asin(x), acos(x), atan(x)
Lu : u vo s thc double, kt qu tr v s thc double.
V d:
int x = 4, y = 3, z = -5;
float t = -1.2;
float kq1 = sqrt(x1);
int kq2 = pow(x, y);
float kq3 = pow(x, 1/3);
Chng:

float kq4 = pow(x, 1.0/3);


int kq5 = abs(z);
float kq6 = fabs(t);
Cc v d trn nn dng double thay cho float.
2.8 Thut ton (Algorithm):
Gii thut l mt h thng cht ch v r rng cc quy tc nhm xc nh
mt dy cc thao tc trn nhng d liu vo sao cho sau mt s hu hn
bc thc hin cc thao tc ta thu c kt qu ca bi ton.
Ni mt cch d hiu, gii thut l m t qu trnh thc hin chuyn
d liu nhp vo thnh kt qu mong mun. Khi m t gii thut lun
km theo m t hai thnh phn d liu u vo v kt qu thu c sau
khi thc hin gii thut.
Thut ton l mt dy hu hn cc cu lnh (cc php ton v quyt nh)
c dng gii quyt mt bi ton tin hc.
C nhiu hnh thc m t gii thut nh: m gi (Pseudo code), ngn
ng t nhin, lu (flow chart). Mi hnh thc m t u c nhng u
im c trng ph hp cho tng loi bi ton. Trong khun kh ni
dung gio trnh ny s trnh by hnh thc m t gii thut bng lu do
tnh n gin v trc quan, nht l i vi ngi mi bt u hc lp
trnh.
Thut ton:
Chin lc gii bi ton.
Phng php m t gii bi ton.
Cch thc Step by Step gii bi ton.
Phng php m t thut ton:
Ngn ng t nhin:
Bc 1: Nhp gi tr n.
Bc 2: S = n*n.
Bc 3: Xut S ra mn hnh.
Chng:

S khi: Lu gii thut la cng cu dung bi u din gii


thu t mt cch trc quan, vi c m ta d liu u vo (input), kt
qu (output) va lu ng x ly thng qua cac ky hi u hinh ho c.
Phng php duyt lu rt n gin: t trn xung v t tri
sang phi.

Cc k hiu c bn:

Khi c bn ngha

Bt u v kt thc thut ton

Nhp/ Xut d liu

Thao tc, php gn

iu kin
Chng:

Ch th t cc bc trong thut ton

Ni gia hai s khi

Gi cc chng trnh con

Mu trnh by thut ton bng ngn ng t nhin:


Thut ton: Tn thut ton.
Input:
Output:
Cc bc m t:
B1:
B2:
B3:

V d 1: Cho s t nhin n (0<n<=1000). Hy tnh tng S=1+2+..+n

Thut ton: Tnh tng S.


Input: S nguyn dng n (0<n<=1000).
Output: Tng S.
Cc bc m t:

Bc 1: Nhp gi tr ca n

Bc 2: Tnh tng S = 1+2+3+..+n

Bc 3: Xut tng S ra mn hnh

V d 2: Nhp vo 2 s thc a, b. Gii phng trnh ax+b=0.

Thut ton: Gii phng trnh bc nht ax+b=0.


Input: Nhp gi tr 2 s thc a, b.
Output: Nghim x ca phng trnh.
Chng:

Cc bc m t:

Bc 1: Nhp gi tr 2 s thc a, b.

Bc 2: tnh x = -b/a;

Bc 3: Xut nghim x ra mn hnh.

V d 3: Gii thut tm s ln nht (max) ca hai s nguyn a v b

Thut ton: Tm max ca 2 s.


Input: Nhp gi tr 2 s nguyn a, b.
Output:
Cc bc m t:

Bc 1: Gi s max = a

Bc 2: Nu b > max th

max = b

Bc 3: In ra s max

Cc c trng ca thut ton


D liu vo, d liu ra (u vo, u ra)
Tnh kt thc: Gii thut phi dng sau mt s hu hn bc.
Tnh xc nh: Cc thao tc my tnh phi thc hin c v cc
my tnh khc nhau thc hin cng mt bc ca cng mt gii
thut phi cho cng mt kt qu.
Tnh tng qut: Gii thut phi "vt' ht cc trng hp v p dng
cho mt lot bi ton cng loi.
Tnh hiu qu: Mt gii thut c nh gi l tt nu n t hai
tiu chun thc hin nhanh v s dng t ti nguyn h thng.
Tnh r rng: cc cu lnh minh bch c sp xp theo th t nht
nh.
Chng:

Bi tp: trnh by cc thut ton sau:


Hon v gi tr ca 2 bin
Cho 2 bin a, b, gi c l bin s lu gi tr ln nht trong 2 bin a
v b. Hy tm c.
Gii phng trnh bc 2.

C u truc tu n t

Tu n t thc thi tt c cc lnh. Mi l nh c thc thi mt ln theo


chui t trn xu ng di, xong l nh nay r i chuy n xu ng lnh
k ti p.

Lnh

Lnh

Lnh

Vi du: Nhp vao 3 s nguyn a, b, c va xu t ra man hinh vi gia tri cua


mi s tng ln 1.

Bt u

a, b, c

a=a+1

b=b+1

c=c+1

a, b, c

Kt thc

V d 1: Hon v hai s nguyn a v b


Chng:

Bt u

a, b

tam = a
a=b
b = tam

a, b

Kt thc

2.9 Tm tt chng 2:

2.10 Bi tp:
Bi tp l thuyt:
1. Trnh by cc kiu d liu c s trong C v cho v d.
2. Trnh by khi nim v bin v cch s dng lnh gn.
3. Phn bit hng thng v hng k hiu.
Cho v d minh ha.
4. Trnh by khi nim v biu thc.
Ti sao nn s dng cp ngoc n.
5. Trnh by cch nh dng xut.
Bi tp thc hnh:
6. Nhp nm sinh ca mt ngi v tnh tui ca ngi .
7. Nhp 2 s a v b. Tnh tng, hiu, tnh v thng ca hai s .
8. Nhp tn sn phm, s lng v n gi. Tnh tin v thu gi tr gia tng
phi tr, bit:
a. tin = s lng * n gi
b. thu gi tr gia tng = 10% tin
9. Nhp im thi v h s 3 mn Ton, L, Ha ca mt sinh vin. Tnh
im trung bnh ca sinh vin .
10.Nhp bn knh ca ng trn. Tnh chu vi v din tch ca hnh trn .
Chng:

11.Nhp vo s xe (gm 4 ch s) ca bn. Cho bit s xe ca bn c my


nt?
Chng:

Chng 3 : Cu lnh r nhnh


Mc tiu:
Tm tt: Tm hiu v ci t cc cu trc r nhnh, la chn trong ngn ng C.
M t cch hot ng ca cc cu trc ny v hng dn chy tng bc
chng trnh.
3.1 Biu thc logic:
Khi nim true v false:
o Mt s cu lnh C (if, while, for) phi kim tra gi tr ca mt biu
thc xem n c gi tr true hay false
o Nhiu ngn ng c kiu Boolean m t gi tr true/false.
o Ngn ng C dng s nguyn m t true/false:
0 l false
1 l true
o Nhn xt
false: c ngha l 0
true : c ngha l khc 0 (, -2, -1, 1, 2, ) thng dng
true vi ngha l 1
Khi nim Biu thc logic: L s kt hp gia cc s v cc php ton so
snh, cc php ton logic
Gi tr ca biu thc logic l 1 s nguyn
o 0 false
o 1 true
Xy dng biu thc logic Php ton quan h:
o Php ton quan h (ton t quan h): Cc php ton quan h trong
C tng ng vi cc php ton quan h >, <, , trong ton hc
o Ton t quan h s sinh ra gi tr
0 (false),
1 (true)
khi dng trong biu thc.
Chng:

Xy dng biu thc logic Ton t quan h:

Ton t quan h ngha V d

> ln hn x>y

< nh hn x<y

>= ln hn hoc bng x>=y

<= nh hn hoc bng x<=y

Ch :
K hiu = lun pha sau.
Gia 2 k hiu khng c khong trng.
V d:

Ton t quan h c dng so snh gia cc s nguyn, cc s thc


(floating point) v gia s nguyn v s thc
u tin ca ton t quan h thp hn u tin ca ton t s hc
Ton t quan h c th t kt hp tri
Xy dng biu thc logic Ton t bng:

Ton t bng ngha V d

== Bng X==y

!= Khng bng !=y

Ch
K hiu = lun pha sau
Gia 2 k hiu khng c khong trng
V d:
Chng:

Ton t bng sinh ra gi tr 0 (false), 1 (true)


u tin ca ton t bng thp hn u tin ca ton t quan h
Ton t bng c th t kt hp tri
Xy dng biu thc logic Ton t logic:
o Ton t logic: Ton t logic and, or, not c dng xy dng
cc biu thc logic phc tp t cc biu thc n gin hn.

Ton t logic ngha V d

! Ph nh - NOT !(x>=y)

&& V - AND (x<=y) && (x!=0)

|| Hoc - OR (x<=y) || (x!=0)

Vit biu thc cho cc mnh :


x ln hn hay bng 3
x >= 3
a v b cng du
((a>0) && (b>0)) || ((a<0) && (b<0))
(a>0 && b>0) || (a<0 && b<0)
p bng q bng r
(p == q) && (q == r) hoc (p == q && q == r)
5 < x < 5
(x > 5) && (x < 5) hoc (x > 5 && x < 5)

3.2 Khi nim v cu lnh la chn:


Cu lnh Chn la (Selection Statement): L cu lnh cho php my tnh
quyt nh c thc hin mt phn code no hay khng da vo 1 biu
thc iu kin l ng (true) hay sai (false).
i m quy t inh
cho phep cho n lnh thc hin. Ty theo tng yu cu
c th ca bi ton th vic la chn c th gm nhiu trng hp: ch xt
Chng:

mt trng hp khi iu kin ng, xt c hai trng hp ng hoc sai,


cng c th xt nhiu trng hp.
V d 1: Chng trnh cn kim tra mu s trc khi thc hin php chia.
#include<stdio.h>
void main( )
{
double a, b;
printf(Moi nhap a=); scanf(%lf, &a);
printf(Moi nhap b=); scanf(%lf, &b);
if( b == 0 )
printf(Mau so phai khac khong);
else
printf(Ket qua phep chia=%lf, a/b );
}
V d 2: Chng trnh cn kim tra username v password ca ngi s
dng chng trnh. Nu ng l ngi c quyn th cho php chy
chng trnh.

Phn loi: cc loi cu lnh Chn la trong ngn ng C gm:


Cu lnh if
Cu lnh if-else
Cu lnh if-else-if
Cu lnh switch case
Ton t iu kin
3.3 Cu lnh if: ch xt trng hp ng
Chng:

Trong :
<BT Logic> l iu kin (dk).
<Lnh 1> l Cu lnh n hoc Cu lnh phc (kp gia { v })
C php 1:

C php 2

ngha: Nu dk l true th on m trong cu lnh if c thc thi.


V d 1: Xy dng s khi v Vit chng trnh: Nhp 3 s nguyn a,
b, c v tm gi tr ln nht trong 3 s .
Lu : Khng c thm ; sau iu kin ca if.
V d: ta c on chng trnh sau:
if (a != 0); //Sai
printf(a khac 0.);
Vi du: Nhp vao s nguyn n. Ki m tra n u n > 0 th tng n ln 1
n vi . Xu t k t qua.
Chng:

Bt u

n>0 ng

n = n+1

Kt thc

Vi du 2: Tnh gi tr tuyt i ca s nguyn x

Bt u

x<0 ng

x = -x

Kt thc

Kim tra min gi tr s:

Kim tra min gi tr k t:


Chng:

Cu lnh phc (Compound statement Khi lnh): L 1 danh sch cc


cu lnh c t trong k hiu { v }. Mt khi lnh gm cc khai bo
bin v cc cu lnh
C php:

c im ca cu lnh phc
Tt c cc cu lnh trong cng 1 khi s chu cng 1 tc ng
ca 1 cu lnh khc.
Cc khi lnh c th lng nhau (khi cha, khi con)
Bin khai bo trong 1 khi cha th c th dng trong khi con.
V d:

3.4 Cu lnh if-else: xt c hai trng hp ng hoc sai


C php:
Chng:

V d 1: ta c on chng trnh sau:


void main()
{
if (a == 0)
printf(a bang 0);
else
printf(a khac 0);
if (a == 0)
{
printf(a bang 0);
a = 2912;
}
else
printf(a khac 0);
}

Vi du: Nhp vao s nguyn n. Ki m tra n u n chn xu t ra man hinh n


chn, ngc lai xu t n le.
Chng:

Bt u

Sai n chia ht cho 2 ng

n l n chn

Kt thc

V d: Xy dng s khi v Vit chng trnh: Nhp vo 1 s nguyn


a, cho bit s l s chn hay s l.
Lu : Cu lnh if v cu lnh if else l mt cu lnh n.
Chng:

3.5 Cu lnh if-else-if: xt nhiu trng hp


C php

V d: Xy dng s khi v Vit chng trnh: Nhp vo s nguyn a,


cho bit s ny l s dng hay s m hay bng 0.
3.6 Cu lnh if lng nhau:
C php
Chng:

Ch :
Trong cu lnh if lng nhau, cu lnh else s c kt hp vi cu
lnh if cng cp vi n

Nn dng else loi tr trng hp.


Khng dng else:
if (delta < 0)
printf(PT vo nghiem);
if (delta == 0)
printf(PT co nghiem kep);
if (delta > 0)
printf(PT co 2 nghiem);

Dng else loi tr:


Chng:

if (delta < 0)
printf(PT vo nghiem);
else // delta >= 0
if (delta == 0)
printf(PT co nghiem kep);
else
printf(PT co 2 nghiem);
Vi du: Gii v bin lun phng trnh bc nht: ax + b =0.

Bt u

a, b

a=0 ng
Sai

b=0 ng
Sai

Nghim V V s
x = -b/a nghim nghim

Kt thc

3.7 Cu lnh switch-case:


Chng:

Trong :
<Bin/BT> l bin/biu thc cho gi tr ri rc (hng)
C php 1:

ngha: Gi tr ca bieuThuc s c so snh vi cc gi tr Hng1,


Hng2,
Nu bng th case ca Hng s c thc thi v khi gp lnh
break s thot khi cu lnh switch
Nu khng bng th cc lnh trong default s thc thi
Cc rng buc ca cu lnh switch-case
bieuThuc phi l 1 biu thc c kt qu l gi tr s nguyn
Cc Hng ca mi case phi l 1 biu thc hng s nguyn hay k
t (m ASCII)
Th t cc case khng quan trng
default c th khng c trong cu lnh switch-case
Chng:

C php 2:

V d: ta c on chng trnh sau:


void main()
{
int a;
printf(Nhap a: );
scanf(%d, &a);
switch (a)
{
case 1 : printf(Mot); break;
case 2 : printf(Hai); break;
case 3 : printf(Ba); break;
}
}

V d: Xy dng s khi v vit chng trnh: Nhp s nguyn dng


n vi 1n7. Ty theo n=1,2,,7 hy in cc t Sunday, Monday,
Tuesday, Saturday tng ng.
Lu 1: Cu lnh switch l mt cu lnh n v c th lng nhau.
{
Chng:

switch (a)
{
case 1 : printf(Mot); break;
case 2 : switch (b)
{
case 1 : printf(A); break;
case 2 : printf(B); break;
} break;
case 3 : printf(Ba); break;
default : printf(Khong biet doc);
}
}
Lu 2: Cc gi tr trong mi trng hp phi khc nhau.
switch (a)
{
case 1 : printf(Mot); break;
case 1 : printf(MOT); break;
case 2 : printf(Hai); break;
case 3 : printf(Ba); break;
case 1 : printf(1); break;
case 1 : printf(mot); break;
default : printf(Khong biet doc);
}
Trong on chng trnh trn c 3 trng hp c gi tr l 1.
Lu 3: switch s nhy n case tng ng v thc hin n khi no gp
break hoc cui switch s kt thc.
Lu 4: Tn dng tnh cht khi b break;
switch (a)
Chng:

{
case 1 :
case 3 : printf(So le); break;
case 2 :
case 4 : printf(So chan); break;
}
Lu 5: Khi xt nhiu trng hp cho mt bin ri rc, ta nn chn
switch.
Cu lnh if
if (a == 1)
printf(Mot);
if (a == 2)
printf(Hai);
if (a == 3)
printf(Ba);
if (a == 4)
printf(Bon);
if (a == 5)
printf(Nam);
Cu lnh switch
switch (a)
{
case 1: printf(Mot);
break;
case 2: printf(Hai);
break;
case 3: printf(Ba);
break;
Chng:

case 4: printf(Bon);
break;
case 5: printf(Nam);
}

3.8 Ton t iu kin:


C php:

ngha:
Nu bieuThuc c kt qu l 1 (true) th tenBien s c gi tr l
giaTri1
Ngc li tenBien s c gi tr l giaTri2
V d: Nhp 2 s nguyn a, b. Tm gi tr ln nht trong 2 s nguyn ny.

3.9 Mt s li thng gp:


Chng:
Chng:

Tm tt chng 3:

Bi tp:
Bi tp thc hnh:
1. Nhp mt s bt k. Hy c gi tr ca s nguyn nu n c gi tr t
1 n 9, ngc li thng bo khng c c.
2. Nhp mt ch ci. Nu l ch thng th i sang ch hoa, ngc li i
sang ch thng.
3. Gii phng trnh bc nht ax + b = 0.
4. Gii phng trnh bc hai ax2 + bx + c = 0.
5. Nhp 4 s nguyn a, b, c v d. Tm s c gi tr nh nht (min).
6. Nhp 4 s nguyn a, b, c v d. Hy sp xp gi tr ca 4 s nguyn ny
theo th t tng dn.
7. Tnh tin i taxi t s km nhp vo. Bit:
a. 1 km u gi 15000
b. T km th 2 n km th 5 gi 13500
c. T km th 6 tr i gi 11000
d. Nu trn 120km c gim 10% tng tin.
Chng:

8. Nhp vo thng v nm. Cho bit thng c bao nhiu ngy.


9. Nhp di 3 cnh 1 tam gic. Kim tra c phi l tam gic khng v
l tam gic g?
10.Nh p vao s nguyn n, ki m tra xem n chn hay le va xu t ra man hinh.
11.Nh p vao ba ca nh a, b, c cua tam giac. Xu t ra man hinh tam giac o
thu c loa i tam giac gi? (Thng, cn, vung, u hay vung cn).
12.Nh p vao s nguyn n. Xu t ra n man hin h (N u n chn thi g p i gia
tri).
13.Nh p vao s nguyn n. N u n>5 thi tng n ln 2 n vi va tra v gia tri n,
ng c la i tra v gia tri 0.
Chng:

Chng 4 : Cu lnh lp
Mc tiu:
Tm tt ni dung:
Tm tt: Tm hiu v ci t cc cu lnh lp trong ngn ng C. M t cch
hot ng ca cu trc ny v hng dn chy tng bc chng trnh.

4.1 Khi nim Vng lp:


t vn :
V d
Vit chng trnh xut cc s t 1 n 10
=> S dng 10 cu lnh printf
Vit chng trnh xut cc s t 1 n 1000
=> S dng 1000 cu lnh printf !
Gii php
S dng cu trc lp li mt hnh ng trong khi cn tha
mt iu kin no .

Vng lp (loop): l cu lnh dng thc hin nhng cu lnh khc


nhiu ln (thn vng lp, loop body)
Biu thc iu khin (controlling expression): Mi vng lp c mt biu
thc iu khin quyt nh c lp tip hay khng.
Mi ln thn vng lp c thc hin (mt ln lp ca vng lp,
iteration), biu thc iu khin c kim tra. Nu biu thc bng (1)
true th vng lp tip tc chy
Cc loi vng lp
Vng lp while
Vng lp do while
Vng lp for
Chng:

4.2 Cu lnh while:


C php 1:

C php 2:

V d:

Ch :
K hiu ( v ) l bt buc
Gia du ) v thn vng lp khng c g c
Hot ng ca lnh lp while: Khi lnh while thc thi
B1: Biu thc iu khin c lng gi, ri kim tra
B2: Nu biu thc ny c gi tr 1 (true) th thn vng lp c
thc thi v quay li B1
Chng:

B3: Tin trnh ny tip tc cho n khi biu thc iu khin bng 0
(false)
V d 1: Tm s ln nht c dng 2k nhng nh hn n cho trc.
Theo di thc thi cu lnh trn khi n=10
V d 2: Theo di chng trnh m gim

Vi du: Nhp vao s nguyn n. Xu t ra man hinh t 1 n n.

Bt u

i=1

i <= n ng

i=i+1

Kt thc

Nhn xt cu lnh while


Biu thc iu khin sai khi vng lp kt thc

Thn vng lp while c th khng thc thi ln no.


Chng:

Cu lnh while l mt cu lnh n v c th lng nhau.


V d:
if (n < 10 && m < 20)
{
while (n >= 1)
{
while (m >= 1)
{
printf(%d, m);
m--;
}
n--;
}
}
Khng c thm ; ngay sau lnh lnh while.
V d:
int n = 0;
while (n < 10);
{
printf(%d\n, n);
n++;
}
while (n < 10)
{
};
{
printf(%d\n, n);
Chng:

n++;
}
Vng lp v tn (infinite loop):
Vng lp s khng bao gi kt thc nu biu thc iu khin lun
lun c gi tr khc true (gi tr khc 0).
V d:
void main()
{
int n = 1;
while (n < 10)
{
printf(%d, n);
n--;
}
n = 1;
while (n < 10)
printf(%d, n);
}
i lc, cc programmer c tnh to ra vng lp v tn bng cch
dng hng s khc 0 lm biu thc iu khin

Vng lp v tn kt thc khi thn vng lp c lnh nhy ra khi vng lp


(break, goto hay return)
V d 3: [In bng cc s bnh phng] Vit chng trnh nhp s t nhin
n, sau in ra n dng mi dng s cha 1 s t 1 n n v gi tr bnh
phng ca n
Chng:

Chng trnh s in cc s bnh phng


Nhp s n: 5
1 1
2 4
3 9
4 16
5 25
V d 4: [Tnh tng dy s] Tnh tng dy s nguyn c nhp vo t
bn phm
Chng trnh tnh tng dy s nguyn
Nhp cc s nguyn (0 kt thc): 7 3 12 201 0
Tng l: 223
4.3 Cu lnh dowhile:
C php 1:

C php 2:
Chng:

Hot ng ca lnh lp dowhile: Khi lnh dowhile thc thi th


B1: Thn vng lp c thc thi trc
B2: Sau , biu thc iu khin c lng gi v kim tra
B3: Nu kt qu bng 1 (true) thn vng lp c thc thi li
(Quay v B1)
B4: Vng lp kt thc khi biu thc iu khin c gi tr bng 0
(false) (sau khi thc thi thn vng lp)
V d 1:

Khc bit gia lnh lp dowhile v lnh lp while


Thn ca lnh lp dowhile thc hin t nht 1 ln
Thn ca lnh lp while c khi khng thc hin ln no
Ch : Nn lun lun dng {, } trong tt c cu lnh dowhile

Vi du : Nh p va o s nguyn dng n. Nu nh p sai yu cu nh p la i n.


Chng:

Bt u

ng
n

n<=0

Kt thc

V d 2: Tnh s ch s ca 1 s nguyn
Nhp 1 s nguyn dng: 60
S ny c 2 ch s
Cu lnh do while l mt cu lnh n v c th lng nhau.
V d:
int a = 1, b;
do
{
b = 1;
do
{
printf(%d\n, a + b);
b = b + 2;
}
while (b < 20);
a++;
}
while (a < 20);
Chng:

Cu lnh do while c th b lp v tn (loop)


V d:

int n = 1;
do
{
printf(%d, n);
n--;
}
while (n < 10);

n = 1;
do
printf(%d, n);
while (n < 10);

4.4 Cu lnh for:
C php 1:

C php 2:
Chng:

Hot ng ca lnh lp for: Khi lnh for thc thi:


B1: Biu thc expr1 c thc thi u tin
B2: Sau biu thc expr2 c thc thi
B3: Nu expr2 bng
true th thn vng lp c thc thi, sau biu thc expr3
c thc thi, sau expr2 li c kim tra (Quay v B2)
false vng lp kt thc.
V d:

V d 3: Tin h t ng: S 1 2 3 n , vi n>0


Chng:

Bt u

S=0
i=1

i <= n ng

S=S+i

S
i=i+1

Kt thc

1 3 5 2n 1
Vi du 4: Ti nh tng: S (n) ... , v i n0
2 4 6 2n 2

Bt u

i=1
S=0
k = 2*n+1

i <= k ng

S = S + i/(i+1)
S
i = i +2

Kt thc

Vi du 5: Ti nh tng: S (n) 1 2 3 4 (1) n 1 n , v i n>0


Chng:

Bt u

i=1
S=0
dau = 1

i <= n ng

S = S + dau*i
S dau = -dau

i = i +1
Kt thc

Nhn xt
expr1 ch thc hin duy nht 1 ln v thng c dng khi
to bin ban u (initialize)
expr2 dng kim tra iu kin c lp tip hay khng (test)
expr3 c thc thi sau khi thn vng lp c thc thi v thng
c dng thay i biu thc iu kin (update)

Cc thnh phn <expr1>, <expr2>, <expr3> cch nhau bng du ;


Nu c nhiu thnh phn trong mi phn th c cch nhau bng
du ,
V d:
for (int i = 1, j = 2; i + j < 10; i++, j += 2)
printf(%d\n, i + j);

So snh for vi while:


Cu lnh for rt gn vi cu lnh while
Chng:

Cu lnh for l cu lnh l tng cho nhng vng lp c bin


m.
S ln lp xc nh ngay trong cu lnh for

S dng lnh for


Cu lnh for thng c la chn cho nhng vng lp c bin
m tng ln hay bin m gim i
Cu lnh for m tng ln hay m gim xung n ln thng c 1
trong nhng hnh thc sau:
m tng ln t 0 n n-1

m tng ln t 1 n n

m gim xung t n-1 n 0

m gim xung t n n 1

Nhn xt:
Vng lp tng ln dng < hay <=
Vng lp gim xung dng > hay >=
B cc biu thc trong cu lnh for
Mt s cu lnh for khng cn phi y 3 biu thc iu
khin vng lp v th C cho php chng ta b mt s hay tt c
biu thc.
Cu lnh for l mt cu lnh n v c th lng nhau.
Chng:

V d: ta c on chng trnh sau


if (n < 10 && m < 20)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
printf(%d, i + j);
printf(\n);
}
}
}
Khng c thm ; ngay sau lnh lnh for.
=> Tng ng cu lnh rng.
V d:
for (i = 0; i < 10; i++); //Sai
{
printf(%d, i);
printf(\n);
}
for (i = 0; i < 10; i++)
{
};
{
printf(%d, i);
printf(\n);
}
Chng:

B expr1
Nu biu thc 1 b i th khng c phn khi to c thc thi
trc vng lp

Ch :
2 du ; vn cn, d b i bt k biu thc no
B expr3
Nu biu thc 3 b i th thn vng lp phi c lnh thay i
expr3 thnh false

B expr1 v expr3
Nu expr1 v expr3 b i th vng lp for tr thnh vng lp while

B expr2
Nu biu thc 2 b i th mc nhin n hiu l gi tr true v vy
vng lp s khng kt thc (vng lp v tn)
B c expr1, expr2, expr3
Mt s lp trnh vin dng cch ny to vng lp v tn

Khai bo bin trong lnh for


Chng:

expr1 trong for c th c thay th bng 1 khai bo bin

Khai bo bin nh trn c th lm chng trnh d hiu hn


Ton t du phy , c dng
Khi mun vit t 2 biu thc khi to tr ln trong lnh for
Khi mun tng gim t 2 bin tr ln trong lnh for
C php: Biu thc du phy

Hot ng:
B1: bieuthuc1 c tnh ton
B2: bieuthuc2 c tnh ton v kt qu ca bieuthuc2 s dng lm
kt qu ca biu thc du phy
V d

4.5 Cu lnh break, continue v goto:


Mi vng lp u c im thot
im thot trc thn vng lp: while, for
im thot sau thn vng lp: dowhile
Vn : i lc chng ta cn
im thot trong thn vng lp
C nhiu im thot
Cu lnh break
Dng nhy ra ngoi cu lnh switch
Dng nhy ra khi vng lp while, dowhile, for
Chng:

V d: Vit chng trnh kim tra s nguyn n c l s nguyn t hay


khng
V d: Vit chng trnh nhp cc s n v in ra n3, chng trnh dng khi
nhp s 0
Lnh break thot khi cu lnh while, dowhile, for, switch bao bc n
Nu c nhiu cu lnh lng nhiu cp th break ch thot khi 1 cp lng
n.

Cu lnh continue
Cu lnh continue chuyn quyn iu khin n cui vng lp (
chun b qua ln lp tip theo).
Cu lnh continue ch c th dng cho cc lnh lp while,
dowhile v for (khng dng cho cu lnh switch).
Chng:

Cu lnh goto
Cu lnh goto cho php chng trnh nhy n by k im no
trong 1 hm
Gii hn ca lnh goto
Ch nhy trong cng 1 hm
Khng th nhy qua phn khai bo ca 1 bin
Nhn (Label) l 1 danh nh c t trc mt cu lnh

Mt lnh c th c nhiu nhn

C php lnh goto

S dng cu lnh goto


Cu lnh goto l c bn v quan trng trong cc ngn ng lp trnh
c, nhng ngy nay him khi s dng trong ngn ng C.
Lnh goto c th hu dng trong mt s trng hp
Chng:

4.6 Cu lnh rng


Cu lnh rng (null): l cu lnh khng c g c ngoi tr chm phy ;
V d: i=0; ; j=5;
ng dng cu lnh null: To vng lp c loop body rng

Tm tt chng 4:

Bi tp:
Bi 1: Nhp mt s nguyn dng n (n > 0).
Hy cho bit:
a. C phi l s i xng? V d: 121, 12321,
b. C phi l s chnh phng? V d: 4, 9, 16,
c. C phi l s nguyn t? V d: 2, 3, 5, 7,
d. Ch s ln nht v nh nht?
e. Cc ch s c tng dn hay gim dn khng?
Bi 2: Nhp mt s nguyn dng n. Tnh:
a. S = 1 + 2 + + n
b. S = 12 + 22 + + n2
c. S = 1 + 1/2 + + 1/n
d. S = 1*2**n = n!
e. S = 1! + 2! + + n!-
Chng:

Bi 3: Nhp 3 s nguyn a, b v n vi a, b < n. Tnh tng cc s nguyn dng


nh hn n chia ht cho a nhng khng chia ht cho b.
Bi 4: Tnh tng cc s nguyn t nh hn n (0 < n < 50)
Bi 5: Nhp mt s nguyn dng n. Xut ra s ngc li. V d: Nhp 1706
Xut 6071.
Bi 6: Tm v in ln mn hnh tt c cc s nguyn trong phm vi t 10 n 99
sao cho tch ca 2 ch s bng 2 ln tng ca 2 ch s .
Bi 7: Tm c s chung ln nht ca 2 s nguyn dng a v b nhp t bn
phm.
Bi 8: Nhp n. In n s u tin trong dy Fibonacy.
a. a0 = a1 = 1
b. an = an 1 + an 2
Tinh n!, vi n 0
Tinh P(n) 1 3 5 (2n 1) , vi n 0
Tinh S (n) 1 3 5 (2 n 1) , vi n 0
n 1
Tinh S (n) 1 2 3 4 (1) n , vi n 0
Tinh S (n) 1 (1 2) (1 2 3) (1 2 3 n) , vi n 0

Tinh S (n) 1 2 3 n ,
2 2 2 2
vi n 0
1 1 1
S ( n) 1
Tinh 2 3 n, vi n 0

Tinh P( x, y) x
y

Tinh S (n) 1 (1 2) (1 2 3) (1 2 3 n) , vi n 0
Cho s nguyn n. Tinh tri tuy t i cua n.
Cho s nguyn dng n g m k ch s . Tim ch s co gia tri ln nh t.
m s l ng c s chn cua s nguyn dng n.
In ra ch s u tin cua s nguyn dng n g m k ch s .
Cho 2 s nguyn dng a, b. Tim USCLN cua a va b.
Cho 2 s nguyn dng a, b. Tim BSCNN cua a va b.
Chng:

Cho s nguyn dng x. Ki m tra xem x co phai la s nguyn t khng?


Cho s nguyn dng x. Ki m tra x co phai la s chinh phng khng?
Cho s nguyn dng x. Ki m tra xem x co phai la s hoan thin khng?

Bai t p luyn t p va nng cao

Tinh S (n) 1 2 3 n ,
2 3 n
vi n 0
1 2 3 n
S ( n)
Tinh 2 3 4 n 1 , vi n 0
1 1 1
S ( n) 1
Tinh 1 2 1 2 3 1 2 3 n , vi n 0
1 1 1
S ( n) 1
Tinh 2! 3! n! , vi n 0
1 2 1 2 3 1 2 3 n
S ( n) 1
Tinh 2! 3! n! , vi n 0
Giai va bin lu n phng trinh: ax bx c 0
2

S (n) n (n 1) (n 2) ... 1
Tinh , vi n 0

Tinh S (n) 1 2 3 ... n , vi n 0


Bi tp lm thm:

S = 1/2 + 1/4 + + 1/2n

S = 1 + 1/3 + 1/5 + + 1/(2n+1)

S = 1/(1x2) + 1/(2x3) + + 1/(nxn+1)

S = 1/2 + 2/3 + + n/(n+1)

S = 1 + 1/(1 + 2) + + 1/(1 + 2 + + n)

Lit k tt c c s ca s nguyn dng n

Tnh tng cc c s ca s nguyn dng n

m s lng c s ca s nguyn dng n


Chng:

Tnh tng cc c s chn ca s nguyn dng n


Chng:

Chng 5 : S dng hm
Mc tiu:

Tm tt: Trnh by khi nim v hm con, cc bc xy dng ci t chng


trnh theo phng php th tc hm v mt s k thut lin quan.

5.1 nh ngha hm:


t vn :
Vit chng trnh tnh S = a! + b! + c! vi a, b, c l 3 s nguyn
dng nhp t bn phm.

3 on lnh nhp a, b, c > 0


do {
printf(Nhap mot so nguyen duong: );
scanf(%d, &a);
} while (a <= 0);
do {
printf(Nhap mot so nguyen duong: );
scanf(%d, &b);
} while (b <= 0);
do {
printf(Nhap mot so nguyen duong: );
scanf(%d, &c);
} while (c <= 0);
Chng:

3 on lnh tnh s1 = a!, s2 = b!, s3 = c!


{ Tnh s1 = a! = 1 * 2 * * a }
s1 = 1;
for (i = 2; i <= a ; i++)
s1 = s1 * i;
{ Tnh s2 = b! = 1 * 2 * * b }
s2 = 1;
for (i = 2; i <= b ; i++)
s2 = s2 * i;
{ Tnh s3 = c! = 1 * 2 * * c }
s3 = 1;
for (i = 2; i <= c ; i++)
s3 = s3 * i;
Gii php => Vit 1 ln v s dng nhiu ln
on lnh nhp tng qut, vi n = a, b, c
do {
printf(Nhap mot so nguyen duong: );
scanf(%d, &n);
} while (n <= 0);

on lnh tnh giai tha tng qut, n = a, b, c:


{ Tnh s = n! = 1 * 2 * * n }
s = 1;
for (i = 2; i <= n ; i++)
s = s * i;

Hm (function): L mt dy cc cu lnh c nhm li vi nhau gii


quyt 1 vn v nhm lnh ny c t bng mt ci tn (tn hm).
Chng:

Mt on chng trnh c tn, u vo v u ra.


C chc nng gii quyt mt s vn chuyn bit cho chng trnh
chnh.
c gi nhiu ln vi cc tham s khc nhau.
c s dng khi c nhu cu:
Ti s dng.
Sa li v ci tin.
Hm s c gi ti mt s im trong chng trnh thng qua tn hm.
C php:

V d: Nhp s t nhin n. Vit chng trnh tnh n!


Cch 1: Ch dng hm main()

Cch 2: Tch thnh 2 hm


Chng:

Quy tc t Tn hm
Bt u bng 1 ng t
K t u tin ca mi t nn vit HOA

KieuTraVe (return type): l kiu ca d liu (gi tr) m hm tr v mi


ln n c gi.
Quy tc ca KieuTraVe:
Q1. KieuTraVe khng th l mng (array)
Q2. Nu hm khng tr v gi tr th khai bo kiu tr v l void
Q3. Nu khng ch ra kiu tr v th mc nhin hiu l int
Danh sch tham s (parameters):
Danh sch tham s l cc bin cha cc gi tr do ngi dng cung
cp cho hm khi gi hm.
Quy tc t tn tham s
Sau tn hm l danh sch tham s.
K t u tin ca cc t nn vit HOA, tr k t u tin ca
tham s l vit thng
Quy tc ca danh sch tham s:
Chng:

Q1. Tng tham s mt lun c kiu ca n ngay pha trc (ngay


c khi cc tham s c cng kiu)
Q2. Cc tham s cch nhau bng du phy ,
Q3. Nu hm khng c tham s th
B trng hay
Dng t kha void
Cc tham s l cc bin cc b ca hm

Thn ca hm: c t trong { v }.


Thn ca hm gm
Phn khai bo
Phn lnh
Cc bin x c khai bo trong hm A th
x c gi l bin cc b ca hm A
Ch c hm A dng cc bin x (cc hm khc khng dng c
bin x ca hm A)
Cc hm khc c th khai bao bin x ca ring hm .

Hm c kiu tr v l void (hm void) th thn hm c th khng cha


lnh no.
Hm c kiu tr v khc void th thn hm phi c lnh "return giatri;"
Hm c thn rng c ngha trong vic pht trin ng dng:
Chng:

Chng ta dng cc hm rng pht ha tng th chng trnh.


Sau quay tr li vit code cho cc hm c thn rng.

Cn xc nh cc thng tin sau y:


Tn hm.
Hm s thc hin cng vic g.
Cc u vo (nu c).
u ra (nu c).

V d 1:
Tn hm: XuatTong
Cng vic: tnh v xut tng 2 s nguyn
u vo: hai s nguyn x v y
u ra: khng c
void XuatTong(int x, int y)
{
int s;
s=x+y;
printf(%d cong %d bang %d, x, y, s);
}
V d 2
Chng:

Tn hm: TinhTong
Cng vic: tnh v tr v tng 2 s nguyn
u vo: hai s nguyn x v y
u ra: mt s nguyn c gi tr x + y
int TinhTong(int x, int y)
{
int s;
s=x+y;
return s;
}
V d 3
Tn hm: NhapXuatTong
Cng vic: nhp v xut tng 2 s nguyn
u vo: khng c
u ra: khng c
void NhapXuatTong( )
{
int x,y;
printf(Nhap 2 so nguyen: );
scanf(%d%d, &x, &y);
printf(%d cong %d bang %d, x, y, x+y);
}

Phng php vit 1 hm:


B1. Xc nh 1 nhim v/chc nng cn lm.
B2. Xc nh input ca chc nng.
B3. Xc nh output ca chc nng.
B4. Xc nh cc bc ca chc nng (thut ton).
B5. Vit hm cho chc nng .
Chng:

Tm vc:
Khi nim:
L phm vi hiu qu ca bin v hm.
Bin:
Ton cc: khai bo trong ngoi tt c cc hm (k c hm
main) v c tc dng ln ton b chng trnh.
Cc b: khai bo trong hm hoc khi { } v ch c tc dng
trong bn thn hm hoc khi (k c khi con n). Bin
cc b s b xa khi b nh khi kt thc khi khai bo n.

Gii thch:
Bin a: ton cc.
Bin a1: cc b trong hm Ham1.
Bin a2: cc b trong hm Ham2.
Bin a21: cc b bn trong cp ngoc {} sau cu lnh int
a2; trong hm Ham2.
Bin a3: cc b trong hm main( ).
5.2 Gi hm:
Gi hm:
Chng:

Gi hm thng qua tn hm, sau l du ( v ).


Danh sch cc gi tr cn cung cp cho hm trong du ( v ) v
cc gi tr cch nhau bng du phy
C php:

Danh sch cc gi tr: c gi l cc i s (arguments)


V d:

5 bc hot ng khi gi hm:


B1: Chng trnh s lu v tr ni gi hm vo b nh stack.
B2: Sao chp (copy) cc d liu ca i s x, y ln tham s a, b.
B3: Thc thi on m trong thn ca hm.
B4: Chng trnh quay v ni gi hm da vo v tr lu trn
stack.
B5: Gi tr tr v ca hm c lu vo bin avg.
Ch :
Hm lun lun c k hiu ( v ) d hm khng c i s no.
Khi gi hm c kiu tr v khng phi l void, gi tr hm tr v c
th c lu vo 1 bin dng sau ny.

Hm c th c gi nhiu ln vo nhng ch cn
V d: Tnh tng trung bnh cc cp ca 3 s
Nhp 3 s: 2 4 3
Trung bnh ca 2 v 4: 3
Trung bnh ca 4 v 3: 3.5
Trung bnh ca 3 v 2: 2.1
Chng:

V d: S t nhin n (n>1) gi l s nguyn t nu n ch chia ht cho 1 v


n. Vit hm kim tra s n c l s nguyn t hay khng.
5.3 Khai bo hm:
Thng thng, mi hm, phn nh ngha c t trc ni s dng
hm .
Tuy nhin chng ta c th o ngc v tr (ni gi hm t trc ni
nh ngha hm), mun lm th chng ta phi khai bo hm.
Khai bo hm (function declaration): l cu lnh m t hm, cho trnh
bin dch b qua rng buc v tr nh ngha hm phi nm trc ni dng
hm.
C php:

Ch :
Khai bo hm cn c gi l function prototype
Mt prototype cung cp y cch gi hm
S lng tham s
Cc kiu ca cc tham s
Kiu tr v ca hm
Chng:

5.4 Tham s ca hm l tham tr:


Trong C, cc i s ca hm c truyn bng gi tr (passed by value):
Khi hm c gi mi i s c lng gi sau gn ln tham s
tng ng.
Nhn xt: V cc tham s cha bn sao gi tr ca i s nn bt k thay
i no ca tham s trong hm khng nh hng n i s.
Chng:

C th truyn hng, bin, biu thc nhng hm ch s nhn gi tr.


c s dng khi khng c nhu cu thay i gi tr ca tham s sau khi
thc hin hm.
void TruyenGiaTri(int x)
{

x++;
}
Khi ra khi hm TruyenGiaTri bin x khng thay i gi tr.
u im
C th dng tham s nh l bin trong hm
Khuyn im
Khng th tr v nhiu gi tr (hm ch tr v 1 gi tr)
5.5 Tham s ca hm l tham bin (tham chiu):
khc phc hn ch trn chng ta c 2 gii php
Dng bin con tr (Pointer)
Dng bin tham chiu (Reference)
Bin tham chiu: Bin tham chiu l 1 bin dng tham chiu n 1
bin tn ti trong b nh (lc bin i s v bin tham chiu l 1
bin)
C php:

c s dng khi c nhu cu thay i gi tr ca tham s sau khi thc


hin hm.
void TruyenThamChieu(int &x)
{

x++;
Chng:

}
Khi ra khi hm, bin x s thay i (tng ln 1).
Lu :
Trong mt hm, cc tham s c th truyn theo nhiu cch.
void HonHop(int x, int &y)
{

x++;
y++;
}
S dng tham chiu l mt cch tr v gi tr cho chng trnh.
int TinhTong(int x, int y)
{
return x + y;
}
void TinhTong(int x, int y, int &tong)
{
tong = x + y;
}
void TinhTongHieu(int x, int y, int &tong, int &hieu)
{
tong = x + y; hieu = x y;
}
Cc v d v cch thc hin li gi hm:
V d 1:
{ Cc hm c khai bo y }
void main()
{
Chng:

int n = 9;
XuatTong(1, 2);
XuatTong(1, n);
TinhTong(1, 2);
int tong = TinhTong(1, 2);
TruyenGiaTri(1);
TruyenGiaTri(n);
TruyenDiaChi(1);
TruyenDiaChi(&n);
TruyenThamChieu(1);
TruyenThamChieu(n);
}
V d 1:
void HoanVi(int &a, int &b);
void main()
{
HoanVi(2912, 1706);
int x = 2912, y = 1706;
HoanVi(x, y);
}
void HoanVi(int &a, int &b)
{
int tam = a;
a = b;
b = tam;
}
5.6 Cu lnh return:
Chng:

Hm c kiu tr v khc void phi dng cu lnh return ch gi tr s


c tr v
C php:

bieuThuc thng l hng s hay bin


return 0;
return retValue;
Ch : Khi gi lnh return, nhng lnh sau return s khng c thc
thi.
Hm c kiu tr v l void c th dng lnh return kt thc hm
C php:

5.7 Kt thc chng trnh:


C 2 cch kt thc chng trnh trong C
Dng lnh return trong hm main
Dng hm exit() trong bt k hm no
Hm exit()
Th vin: <stdlib.h>
Tham s truyn cho hm exit() ging nh tham s truyn cho
lnh return
Kt thc ng dng bnh thng

Kt thc ng dng c li

5.8 Chia chng trnh thnh cc hm:


Phng php vit chng trnh bng nhiu hm:
B1. Tm cch gii bi ton tin hc
B2. Chia cch gii thnh cc nhm gii php
B3. Mi gii php l 1 hm.
Chng:

5.9 Hm quy:
Mt chng trnh con c th gi mt chng trnh con khc.
S ln gi ny phi c gii hn (im dng).
Li gii quy: Mt li gii c gi l li gii quy nu trong li gii
gi li chnh n
V d: Tnh n! mt cch quy theo cng thc

Ta thy n! = 1*2**(n-1)*n
Ta thy S(n) = S(n-1)*n
Vy thay v tnh S(n) ta s i tnh S(n-1)
Tng t tnh S(n-2), , S(2), S(1), S(0) = 1
int GiaiThua(int n)

if (n == 0)

return 1;

else

return GiaiThua(n 1) * n;

int GiaiThua(int n)

if (n > 0)

return GiaiThua(n 1) * n;

else
Chng:

return 1;

Mt li gii quy gm c 2 thnh phn:


Thnh phn 1: Thnh phn khng quy (trng hp c bn, trng
hp c s, trng hp suy bin, iu kin dng)
Cha nhng trng hp n gin nht xy dng nn li gii
Thnh phn 2: Thnh phn quy (trng hp quy)
Cha nhng quy tc, cng thc to nn li gii da trn li
gii nh hn cng kiu
Ci t li gii quy
Cch 1: Tm cch chuyn li gii quy v li gii bng vng lp.
Dng vng lp ci t
Cch 2: Dng hm quy trong ngn ng lp trnh
Hm/th tc quy: Mt hm A c gi l Hm quy nu trong nh
ngha hm A c li gi n chnh hm A

S ci t
S 1:

S ci t
S 2:
Chng:

Hy theo vt thc thi ca hm quy sau:

Tm tt chng:

Bi tp:
Bi 1: Vit hm i mt k t hoa sang k t thng.
Bi 2: Vit th tc gii phng trnh bc nht.
Bi 3: Vit th tc gii phng trnh bc hai.
Bi 4: Vit hm tr v gi tr nh nht ca 4 s nguyn.
Bi 5: Vit th tc hon v hai s nguyn.
Bi 6: Vit th tc sp xp 4 s nguyn tng dn.
Bi 7: Cho trc mt s nguyn dng (n>0), vit hm tnh s o ca s .
Bi 8: Cho trc mt s nguyn dng (n>0), vit hm kim tra xem n c phi
l s i xng (Tr v True/False)
Bi 9: Cho trc mt s nguyn dng (n>0), vit hm kim tra xem n c phi
l s chnh phng.
Bi 10: Cho trc mt s nguyn dng (n>0), vit hm kim tra xem n c phi
l s nguyn t.
Bi 11: Cho trc mt s nguyn dng (n>0), vit hm tnh tng cc ch s l.
Chng:

Bi 12: Cho trc mt s nguyn dng (n>0), vit hm tnh tng cc ch s


nguyn t.
Bi 13: Cho trc mt s nguyn dng (n>0), vit hm tnh tng cc ch s
chnh phng.
Bi 14: Cho trc mt s nguyn dng (n>0), vit hm tnh S = 1 + 2 + + n
Bi 15: Cho trc mt s nguyn dng (n>0), vit hm tnh S = 12 + 22 + +
n2
Bi 16: Cho trc mt s nguyn dng (n>0), vit hm tnh S = 1 + 1/2 + +
1/n
Bi 17: Cho trc mt s nguyn dng (n>0), vit hm tnh S = 1 * 2 * * n
Bi 18: Cho trc mt s nguyn dng (n>0), vit hm tnh S = 1! + 2! + +
n!
Bi 19: Vit hm tnh USCLN ca 2 s nguyn.
Bi 20: Vit hm In ra n phn t ca dy Fibonacy.
Chng:

Chng 6 : Mng
Mc tiu:
Tm tt: Gii thiu v cu trc mng mt, hai chiu, khai bo d liu, 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.

6.1 Mng mt chiu:


Khi nim mng:
Vn : [In ngc dy s]
V d 1:
Chng trnh cn lu tr 3 s nguyn?
=> Khai bo 3 bin int a1, a2, a3;
Chng trnh cn lu tr 100 s nguyn?
=> Khai bo 100 bin kiu s nguyn!
Ngi dng mun nhp n s nguyn?
=> Khng thc hin c!
Gii php
Kiu d liu mi cho php lu tr mt dy cc s
nguyn v d dng truy xut.

V d 2: Vit chng trnh nhp vo dy n s nguyn (2n100).


Sau in o ngc dy s ny
Nhp s phn t ca dy: 5
Nhp cc phn t: 8 18 9 6 10
Dy o ngc: 10 6 9 18 8
Gii php:
Chng:

Mng (Array): Mng l mt cu trc d liu cha mt dy gi tr c cng


kiu.
Mng l mt kiu d liu c cu trc do ngi lp trnh nh ngha.
Mng biu din mt dy cc bin c cng kiu. V d: dy cc s nguyn,
dy cc k t
Kch thc ca mng c xc nh ngay khi khai bo v khng bao gi
thay i.
NNLT C lun ch nh mt khi nh lin tc cho mt bin kiu mng.
Phn t mng (Element): Mt gi tr trong mng c gi l 1 phn t.
Phn loi mng:
Mng 1 chiu
Mng nhiu chiu
Khai bo mng (tng minh):
C php:

V d:

Hnh nh

Kiu ca mng: Cc phn t ca mng c th c bt k kiu no.


S phn t ca mng:
L 1 biu thc hng s nguyn
Phi xc nh c th s phn t ngay lc khai bo, khng c s
dng bin hoc hng thng.
int n1 = 10; int a[n1];
Chng:

const int n2 = 20; int b[n2];

V s phn t ca mng c th thay i khi chng trnh c nng


cp, nn thng thng ngi ta dng macro hay bin hng s
nh ngha chiu di mng.

Lu :
Phi xc nh <s phn t> c th (hng) khi khai bo.
Mng nhiu chiu: <tng s phn t> = N1*N2**Nn
B nh s dng = <tng s phn t>*sizeof(<kiu c s>)
B nh s dng phi t hn 64KB (65535 Bytes)
Mt dy lin tc c ch s t 0 n <tng s phn t>-1
V d:
int Mang1Chieu[10];

Khai bo mng (khng tng minh):


C php
Khng tng minh (thng qua khai bo kiu)
typedef <kiu c s> <tn kiu mng>[<s phn t>];
<tn kiu mng> <tn bin mng>;
V d
typedef int Mang1Chieu[10];
Mang1Chieu m1, m2, m3;

S dng mng:
truy cp 1 phn t trong mng, chng ta vit tn mng sau l s
nguyn trong ngoc vung (indexing hay subscripting)
Chng:

C php:

Cc phn t lun lun bt u bng 0:


Nu mng c chiu di n th c ch mc t 0 n-1

V d:

V d: cho mng sau int a[4];

Cc truy xut
Hp l: a[0], a[1], a[2], a[3]
Khng hp l: a[-1], a[4], a[5],
=> Cho kt thng khng nh mong mun!

Dng vng lp for thc hin mt s thao tc trn mi phn t ca mng


Clear mng

c d liu
Chng:

Tnh tng cc phn t:

Tnh tch cc phn t

Ch :
Khng c cc php ton trn mng:
Khng th thc hin php gn gia 2 mng
Khng th thc hin nhng php ton + - * / %, gia 2
mng
Khng th thay i kch thc mng khai bo.
Khng c s dng php gn thng thng m phi gn trc tip
gia cc phn t tng ng
<bin mng ch> = <bin mng ngun>; //sai
<bin mng ch>[<ch s th i>] = <gi tr>;
V d:
#define MAX 3
typedef int MangSo[MAX];
MangSo a = {1, 2, 3}, b;
b = a; // Sai
for (int i = 0; i < 3; i++)
b[i] = a[i];
Chng:

V d: [In ngc dy s]
Vit chng trnh nhp vo dy n s nguyn (1 n100). Sau in o
ngc dy ny.
Nhp s phn t ca dy: 5
Nhp cc phn t: 8 18 9 6 10
Dy o ngc: 10 6 9 18 8
Khi to mng:
Khi to mng (array initializer): Mng ging nh bt k bin khc,
chng ta c th khi to trong khi khi bo mng
C php: Danh sch cc gi tr c t trong { v } v cch nhau
bng k hiu ,

Khi to gi tr cho mt s phn t u mng


int a[4] = {2912, 1706};

Khi to gi tr 0 cho mi phn t ca mng


int a[4] = {0};

T ng xc nh s lng phn t
int a[] = {2912, 1706, 1506, 1904};

Ch :
Nu initializer ngn hn mng cc phn t cn li ca mng c
gi tr 0
Chng:

Nu chiu di ca mng khng ch ra s lng initializer l


chiu di mng
Nu s lng initializer nhiu hn chiu di mng li
Intializer khng th rng
V d: Vit chng trnh kim tra mt s c cc ch s lp li hay khng
Nhp 1 s: 2254233
C ch s lp li
Nhp 1 s: 12345
Khng c ch s lp li
Ton t sizeof vi mng:
Tnh s byte ca 1 phn t mng

Tnh s byte ca mng s dng khai bo

Tnh s phn t ca mng

ng dng ca sizeof: khi chng trnh cn s phn t ca mng chng ta


c th dng cng thc trn

Mt s li thng gp:
Khai bo khng ch r s lng phn t
int a[]; => int a[100];
S lng phn t lin quan n bin hoc hng
int n1 = 10; int a[n1]; => int a[10];
const int n2 = 10; int a[n2]; => int a[10];
Khi to cch bit vi khai bo
int a[4]; a = {2912, 1706, 1506, 1904};
Chng:

=> int a[4] = {2912, 1706, 1506, 1904};


Ch s mng khng hp l
int a[4];
a[-1] = 1; a[10] = 0;
6.2 Mng nhiu chiu:
Khai bo mng:
Mng c th c bt k s chiu no
C php: Kieu tenMang[ChieuDai1][ChieuDai2];
V d: int a[5][10];
int n, m;
Hnh nh:

Chng ta thng trc quan mng 2 chiu bng bng hnh ch nht, nhng
y khng phi l cch lu tr thc s ca mng 2 chiu trong b nh
my tnh. Cn gi l ma trn (matrix).
C lu tr cc mng nhiu chiu theo th t cc dng lin tip nhau

S dng mng:
truy cp phn t dng i, ct j:
C php: tenMang[i][j]
Thng thng chng ta dng cc vng lp for lng nhau x l cc
mng nhiu chiu
Chng:

V d: Hy dng vng lp for lng nhau Khi to ma trn n v

1 0 0 0 0

0 1 0 0 0

0 0 1 0 0

0 0 0 1 0

0 0 0 0 1

Khi to mng:
Chng ta c th khi to (initializer) cho mng 2 chiu bng cc lng cc
initializer mt chiu

Ch :
Nu s dng ca initializer t hn s dng ca mng th nhng
dng cn li bng 0
Nu s phn t trn 1 dng ca intializer t hn mng th nhng
phn t cn li trn dng bng 0
Chng:

Chng ta c th b qua k hiu { v } ca cc initializer lng


bn trong
Ma trn vung:
L mng 2 chiu c s dng v s ct bng nhau.

6.3 Mng hng:


Bt k mng no (1 chiu hay nhiu chiu) cng c th c khai bo l
mng hng bng cch thm t kha const trc kiu ca mng

6.4 Mng dng lm tham s ca hm:


Mng rt hay dng lm i s cho hm
C php 1: Tham s ca hm l mng
Chng:

C php 2: S phn t ca chiu u tin c th b

C php 3: Nu hm cn chiu di mng chng ta ta phi cung cp thm


chiu di mng cho hm

Ch :
Khng th dng hm sizeof tnh s phn t ca tham s mng.
truyn i s mng, chng ta ch cn truyn tn mng (khng c [
v ])

Ch :
Chng:

Hm khng c cch no kim tra s phn t chng ta truyn vo c


phi chnh xc l s phn t ca mng hay khng
Hm c php thay i cc phn t d liu ca mng
Truyn mng cho hm
Tham s kiu mng trong khai bo hm ging nh khai bo bin
mng.
void SapXepTang(int a[100]);
Tham s kiu mng truyn cho hm chnh l a ch ca phn t
u tin ca mng
C th b s lng phn t hoc s dng con tr.
Mng c th thay i ni dung sau khi thc hin hm.
void SapXepTang(int a[]);
void SapXepTang(int *a);
S lng phn t thc s truyn qua bin khc
void SapXepTang(int a[100], int n);
void SapXepTang(int a[], int n);
void SapXepTang(int *a, int n);
Li gi hm
void NhapMang(int a[], int &n);
void XuatMang(int a[], int n);
void main()
{
int a[100], n;
NhapMang(a, n);
XuatMang(a, n);
}
6.5 Mt s thut ton trn Mng
Chng:

I. Mt s thao tc c bn trn mng s nguyn

I. 1. Hm nhp xut mng mt chiu

#define MAX 100

- Nhp mng mt chiu s nguyn kch thc n


void NhapMang (int a[], int n)

for (int i = 0; i < n; i ++)

printf(Nhap phan tu thu %d: ,i);

scanf(%d, &a[i]);

- Xut mng mt chiu s nguyn kch thc n


void XuatMang (int a[], int n)

for (int i = 0; i < n; i ++)

printf(%d\t, a[i]);

- S dng trong hm main()


void main ( )

int a[MAX] , n;

printf(Nhap kich thuoc mang: );


Chng:

scanf(%d, &n);

NhapMang (a,n);

printf(Cac gia tri cua mang vua nhap: \n);

XuatMang (a,n);

I. 2. Lit k (xut) nhng phn t tha iu kin cho trc

Dng lc nhng phn t tha mn iu kin cho trc

Mu 1:

void LietKeXXX(int a[], int n)

for (int i = 0; i<n; i++)

if (a[i] tha iu kin)

Xut a[i];

V d 1: Lit k cc phn t c gi tr chn trong mng

void LietKeChan(int a[], int n)

for (int i = 0; i<n; i++)

if (a[i] %2 ==0)
Chng:

printf(%d\t, a[i]);

Mu 2:

void LietKeXXX(int a[], int n, int x)

for (int i = 0; i<n; i++)

if (a[i] tha iu kin so vi x)

Xut a[i];

V d 2: Lit k cc phn t c gi tr ln hn x trong mng

void LietKeLonHonX(int a[], int n, int x)

for (int i = 0; i<n; i++)

if (a[i] > x)

printf(%d\t, a[i]);
Chng:

V d 3: Chng trnh nhp vo mng mt chiu s nguyn a, kch thc


n. In ra cc phn t c gi tr ln hn x c trong mng

#define MAX 100

void NhapMang(int a[], int n);

void XuatMang(int a[], int n);

void LietKeLonHonX(int a[], int n, int x);

void main()

int a[MAX], n, x;

printf(Nhap kich thuoc mang: );

scanf(%d, &n);

NhapMang(a, n);

printf("Cac phan tu cua mang:\n");

XuatMang(a, n);

printf(Nhap gia tri x: );

scanf(%d, &x);

printf("Cac phan tu co gia tri lon hon %d:\n" , x);

LietKeLonHonX(a, n, x);

void NhapMang(int a[], int n)

{ //Ci t

}
Chng:

void XuatMang(int a[], int n)

{ //Ci t

void LietKeLonHonX(int a[], int n, int x)

{ //Ci t

I. 3. m s lng phn t trong mng

Mu 1:

int DemXXX(int a[], int n)

int d = 0;

for (int i = 0; i<n; i++)

if (a[i] tha iu kin)

d++;

return d;

V d 1: m cc phn t c gi tr l s nguyn t

int LaSNT(int k) for (int i = 1; i <= k;


i++)
{
{
int d = 0;
if (k % i == 0)
Chng:

{ {

d++; int d = 0;

} for (int i = 0; i<n; i++)

} {

if(d == 2) if (LaSNT(a[i]) ==1)

{ {

return 1; d++;

} }

return 0; }

} return d;

int DemSNT(int a[], int n) }

Mu 2:

int DemXXX(int a[], int n, int x)

int d = 0;

for (int i = 0; i<n; i++)

if (a[i] tha iu kin so vi x)

d++;

return d;

}
Chng:

V d 2: m cc phn t c gi tr nh hn x c trong mng

int DemNhoHonX(int a[], int n, int x)

int d = 0;

for (int i = 0; i<n; i++)

if (a[i] < x)

d++;

return d;

I. 4. Tm kim v tr v v tr phn t c gi tr ln nht

int TimVTMax(int a[], int n)

int vtmax = 0;

for (int i = 1; i < n; i++)

if (a[i] > a[vtmax])

vtmax = i;

return vtmax;

}
Chng:

I. 5. Tm v tr phn t c gi tr x

Nu x khng xut hin trong mng th tr v -1

int TimVTX(int a[], int n, int x)

for (int i = 0; i < n; i++)

if (a[i] == x)

return i;

return -1;

I. 6. Kim tra xem mng c tha iu kin cho trc

Trng hp 1: kim tra tn ti mt phn t trong mng tha iu kin


no cho trc tm phn t tha iu kin kt lun.

int KiemTraTonTaiXXX(int a[], int n)

for (int i = 0; i<n; i++)

if (a[i] tha iu kin)

return 1;

return 0;

V d: Kim tra xem mng c tn ti s l khng?


Chng:

int KiemTraTonTaiLe(int a[], int n)


{
for (int i = 0; i < n; i++)
{
if (a[i] % 2 != 0)
return 1;
}
return 0;
}

Trng hp 2: kim tra tt c cc phn t tha iu kin no cho


trc tm phn t khng tha iu kin kt lun mng khng tha iu
kin.

int KiemTraXXX(int a[], int n)

for (int i = 0; i<n; i++)

if (a[i] khng tha iu kin)

return 0;

return 1;

V d 2: Kim tra xem mng c ton b l gi tr m khng?

int KiemTraToanAm(int a[], int n)

for (int i = 0; i<n; i++)


Chng:

if (a[i] >= 0)

return 0;

return 1;

I. 7. Tnh tng c iu kin

int TongXXX(int a[], int n)

int s = 0;

for (int i = 0; i<n; i++)

if (a[i] tha iu kin)

s += a[i];

return s;

V d: Tnh tng cc phn t c gi tr l trong mng

int TongLe(int a[], int n)

{
Chng:

int s = 0;

for (int i = 0; i<n; i++)

if (a[i] %2!=0)

s += a[i];

return s;

I. 8. Tnh gi tr trung bnh c iu kin

float TrungBinhXXX(int a[], int n)

int s = 0, d = 0;

for (int i = 0; i<n; i++)

if (a[i] tha iu kin)

s += a[i];

d ++;

if (d==0)

return 0;

return (float) s / d;

}
Chng:

i vi ham tinh trung binh co i u ki n phai lu y khi chia gia tri (Co th
mang khng co ph n t nao thoa i u ki n, n u ta chia tc la chia cho 0).

V d: Tnh gi tr trung bnh cc phn t c gi tr m trong mng

float TrungBinhAm(int a[], int n)

int s = 0, d = 0;

for (int i = 0; i<n; i++)

if (a[i] < 0)

s += a[i];

d++;

if (d == 0)

return 0;

return (float)s / d;

I. 9. Sp xp mng theo th t tng

void HoanVi(int &a, int &b)

int tam = a;

a = b;

b = tam;

}
Chng:

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]);

I. 10. Xo phn t trong mng

Duy t mang t trai sang phai. Xu t phat t vi tri c n xoa ti n hanh di l n


l t cac ph n t v phia trc cho n khi k t thuc mang, sau o giam kich
thc mang.
Vn t ra l tm v tr cn xa theo iu kin bi ton ri thc hin
xa.

V d 1: Xoa ph n t u tin cua mang

void XoaDau (int a[], int &n)

{
for (int i = 0; i < n-1 ; i++)

a[i] = a[i+1];

n--;

V d 2: Vi t ham xoa ph n t ti v tr (vitri) cho trc trong mang.

void XoaTaiViTri (int a[], int &n, int vitri)


Chng:

{
for (int i = vitri; i < n-1 ; i++)

a[i] = a[i+1];

n--;

I. 11. Chn mt phn t x vo mng

Duy t mang t phai sang trai. Xu t phat t cu i mang ti n hanh y l n


l t cac ph n t v phia sau cho n vi tri c n chen, chen ph n t c n chen vao
chen va tng kich thc mang.
vi tri

Trc khi chn ta phi xc nh v tr cn chn theo iu kin bi ton.

V d 1: Thm ph n t co gia tri x vao cui mng

void ThemCuoi (int a[], int &n, int x)

{
a[n]=x;

n++;

V d 2: Chen ph n t co gia tri X vao mang ta i vi tri (vitri) cho trc

void ChenX (int a[], int &n, int x, int vitri)

{
for (int i = n; i >vitri ; i--)

a[i] = a[i-1] ;

}
Chng:

a[vitri] = x;

n++;

I. 12. Tch v ghep mang

I. 12. 1. K thut tach c ban

Cho mang a kich thc n (n chn). Tach mang a thanh 2 mang b va c sao
cho: b co ph n t u cua mang a, ph n t con la i a vao mang 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]

I. 12. 2. K thut ghep c ban

Cho 2 mang s nguyn a va b kich thc l n l t la n va m. Vi t chng


trinh n i mang b vao cu i mang a.

void NoiMang(int a[], int &n, int b[], int m)

for(int i=0; i<m; i++)


Chng:

a[n+i]=b[i];

n=n+m;

Cho 2 mang s nguyn a va b kich thc l n l t la n va m. Vi t chng


trinh n i xen k (an xen) ln lt cc phn t mang 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++];

}
Chng:

while(j<m)

c[k++]=b[j++];

Mng 1 chiu:
Hm nhp mng
void NhapMang(int a[], int &n)
{

}
Hm xut mng
void XuatMang(int a[], int n)
{

}
Thut ton Tnh tng, tch mng
Cho dy a[0], a[1], , a[n-1] hy tnh tng hay tch ca dy s
nguyn hay s thc trn

Thut ton Tnh tng, tch mng c iu kin


Cho dy a[0], a[1], , a[n-1] hy tnh tng hay tch 1 s phn t
ca dy tha iu kin DK
Chng:

Thut ton m
Cho dy a[0], a[1], , a[n-1] hy m s lng phn t tha iu
kin DK

Thut ton tm kim: Tm 1 phn t xut hin u tin


Yu cu: Tm xem phn t x c nm trong mng a kch thc n
hay khng? Nu c th n nm v tr u tin no.
tng: Xt tng phn ca mng a. Nu phn t ang xt bng x
th tr v v tr . Nu k tm c th tr v -1.
Cho dy a[0], a[1], , a[n-1] hy tm phn t u tin trong dy
tha iu kin DK

Thut ton tm kim: Tm mi phn t xut hin


Cho dy a[0], a[1], , a[n-1] hy tm mi phn t trong dy tha
iu kin DK
Input: a, kch thc n
Output: b, kch thc m
Chng:

Thut ton tm min/max


Cho dy a[0], a[1], , a[n-1]

Thut ton sp xp mng tng dn/gim dn


Yu cu
Cho trc mng a kch thc n. Hy sp xp mng a sao
cho cc phn t c gi tr tng dn.
tng
S dng 2 bin i v j so snh tt c cp phn t vi nhau
v hon v cc cp nghch th (sai th t).
void HoanVi(int &x, int &y)

int tam = x; x = y; y = tam;

void SapXepTang(int a[], int n)

int i, j;
Chng:

for (i = 0; i < n 1; i++)

for (j = i + 1; j < n; j++)

if (a[i] > a[j])

HoanVi(a[i], a[j]);

Thao tc xa trn mng


Cho dy a c n phn t. Hy xa m phn t ti v tr k trong dy a
B1: Di cc phn t t v tr (k+m) ln trc (m) v tr
B2: Gim (n) i (m) n v
void Xoa(int a[], int &n, int vt)

if (vt >= 0 && vt < n)

for (int i = vt; i < n 1; i++)

a[i] = a[i + 1];


Chng:

n--;

Thao tc thm trn mng


Cho dy a c n phn t, Hy thm m phn t mng b vo mng a
ti v tr k
B1: Di cc phn t trong dy a t v tr (k) ra sau (m) v tr
B2: Chp m phn t ca dy b vo dy a t v tr (k) trong
dy a
B3: Tng (n) ln (m) n v
void Them(int a[], int &n, int vt, int x)

if (vt >= 0 && vt <= n)

for (int i = n; i > vt; i--)

a[i] = a[i - 1];

a[vt] = x;

n++;

Mng 2 chiu:
Chng:

Hm nhp mng
void NhapMang(int a[][MAXM], int &n, int &m)
{

}
Hm xut mng
void XuatMang(int a[][MAXM], int n, int m)
{

}
Cc phng php duyt mng 2 chiu
Duyt ton mng
Duyt dng k. V d: tnh tng tng ct
int TongCot(int a[][MAXC], int m, int c)

int i, tong;

tong = 0;

for (i=0; i<m; i++) // Duyt cc dng

tong = tong + a[i][c];

return tong;

Duyt ct k. V d: Tnh tng tng dng.


int TongDong(int a[][MAXC], int m, int n, int d)

{
Chng:

int j, tong;

tong = 0;

for (j=0; j<n; j++) // Duyt cc ct

tong = tong + a[d][j];

return tong;

Duyt ng cho chnh. V d: tnh tng ng cho chnh.


int TongDCChinh(int a[][MAXC], int n)

int i, tong;

tong = 0;

for (i=0; i<n; i++)

tong = tong + a[i][i];

return tong;

Duyt ng cho ph:


int TongDCPhu(int a[][MAXC], int n)

int i, tong;
Chng:

tong = 0;

for (i=0; i<n; i++)

tong = tong + a[i][n-i-1];

return tong;

Duyt tam gic trn:


int TongTrenDCChinh(int a[][MAXC], int n)
{
int i, j, tong;
tong = 0;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
if (i < j)
tong = tong + a[i][j];
return tong;
}
Duyt tam gic di:
int TongDuoiDCChinh(int a[][MAXC], int n)
{
int i, j, tong;
tong = 0;
for (i=0; i<n; i++)
for (j=0; j<n; j++)
if (i > j)
tong = tong + a[i][j];
return tong;
Chng:

Thao tc trn mng 2 chiu


Xa dng k
Xa ct k
Chn 1 mng mt chiu vo dng k
Chn 1 mng mt chiu vo ct k

Tm tt chng:

Bi tp:
1. Cc thao tc nhp xut
a. Nhp mng
b. Xut mng
2. Cc thao tc kim tra
a. Mng c phi l mng ton chn
b. Mng c phi l mng ton s nguyn t
c. Mng c phi l mng tng dn
3. Cc thao tc tnh ton
a. C bao nhiu s chia ht cho 4 nhng khng chia ht cho 5
b. Tng cc s nguyn t c trong mng
4. Cc thao tc tm kim
a. V tr cui cng ca phn t x trong mng
b. V tr s nguyn t u tin trong mng nu c
c. Tm s nh nht trong mng
d. Tm s dng nh nht trong mng
5. Cc thao tc x l
a. Tch cc s nguyn t c trong mng a a vo mng b.
Chng:

b. Tch mng a thnh 2 mng b (cha cc s nguyn dng) v c


(cha cc s cn li)
c. Sp xp mng gim dn
d. Sp xp mng sao cho cc s dng ng u mng gim dn, k
n l cc s m tng dn, cui cng l cc s 0.
6. Cc thao tc thm/xa/sa
a. Sa cc s nguyn t c trong mng thnh s 0
b. Chn s 0 ng sau cc s nguyn t trong mng
c. Xa tt c s nguyn t c trong mng

I. 13. Bi t p c ban

7. Vi t chng trinh nh p xu t mang m t chi u cac s thc.


8. Vi t chng trinh khi ta o gia tri ca
c ph n t la 0 cho mang m t chi u
cac s nguyn g m n ph n t.
9. Vi t chng trinh phat sinh ngu nhin mang m t chi u cac s nguyn
m.
10.Vi t chng trinh phat sinh ngu nhin mang m t chi u cac s nguyn
sao cho mang co th t tng d n (khng s p x p).
11.Vi t chng trinh nh p mang cac s thc va xu t cac ph n t m trong
mang.
12.Vi t chng trinh nh p mang cac s nguyn va xu t cac ph n t le co
trong mang.
13.Vi t chng trinh nh p vao mang m t chi u cac s nguyn va xu t ra cac
ph n t chn nho hn 20.
14.Vi t chng trinh nh p vao mang m t chi u cac s nguyn va xu t ra
man hinh cac ph n t la s nguyn t .
15.Vi t chng trinh nh p vao mng s nguyn kch thc n va s nguyn k,
lit k cac phn t trong mng l s nguyn t nho hn k (n u mang
khng t n ta i s nguyn t nao nho hn k thi phai xu t ra m t cu thng
bao).
Chng:

16.Vi t chng trinh nh p vao mang m t chi u cac s nguyn va xu t ra


man hinh cac ph n t la s chinh phng n m ta i nhng vi tri le trong
mang.
17.Vi t ham tim vi tri ph n t co gia tri x xu t hin cu i cung trong mang.
18.Vi t ham tim vi tri cua ph n t nho nh t trong mang cac s nguyn.
19.Vi t ham tim vi tri cua ph n t ln nh t trong mang cac s nguyn.
cac ph n t nguyn t trong mang cac s nguyn.
20.Vi t ham in vi tri
cac ph n t nguyn t ln hn 23.
21.Vi t ham in vi tri
22.Vi t ham tim vi tri ph n t m u tin trong mang. N u khng co ph n
t m tra v 1.
23.Vi t ham tim vi tri ph n t m ln nh t trong mang.
24.Vi t ham tim vi tri ph n t dng u tin trong mang. N u khng co
ph n t m tra v 1.
25.Vi t ham tim vi tri ph n t dng be nh t trong mang.
26.Vi t ham in cac ph n t la b i cua 3 va 5.
27.Vi t ham tim s chn cu i cung co trong mang, n u khng t n ta i s chn
ham tra v -1.
28.Vi t ham tim s le ln nh t co trong mang, n u khng t n ta i s le th tra
v -1.
29.Vi t ham tim va i ch ph n t ln nh t vi ph n t nho nh t trong
mang.
30.Nh p vao s nguyn X. Vi t ham in ra man hinh nhng ph n t co gia tri
t 1 n X co trong mang s nguyn.
31.Vi t chng trinh nh p vao m t day s a g m n s thc ( n 100 ), nh p
vao day s b g m m s thc ( m 100 ).
32.In ra nhng ph n t chi xu t hin trong day a ma khng xu t hi n trong
dy b.
33.In ra nhng ph n t xu t hi n ca hai day .
34.Vi t ham m cac ph n t m, dng trong mang.
Chng:

35.Vi t ham m cac ph n t chn, le trong mang.


36.Vi t ham m s l n xu t hin cua ph n t x trong mang.
37.Vi t ham m cac ph n t nho hn x trong mang.
38.Vi t ham m cac ph n t la s nguyn t trong mang.
39.Vi t ham m cac ph n t la s hoan thi n trong mang.
40.Vi t ham m cac ph n t la b i cua 3 va 5 trong mang cac s nguyn.
41.Vi t ham tinh t ng cac ph n t chn trong mang.
42.Vi t ham tinh t ng cac ph n t le trong mang cac s nguyn.
43.Vi t ham tinh t ng cac ph n t nguyn t trong mang.
44.Vi t ham tinh t ng cac ph n t n m vi tri chn trong mang cac s
nguyn.
45.Vi t ham tinh t ng cac ph n t n m vi tri nguyn t trong mang.
46.Vi t ham tinh t ng cac ph n t chia h t cho 5 co trong mang.
47.Vi t ham tinh t ng cac ph n t cc a i trong mang cac s nguyn (ph n
t cc a i la ph n t ln hn cac ph n t xung quanh no).
Vi du : 152635186
48.Vi t ham tinh t ng cac ph n t cc ti u trong mang cac s nguyn (ph n
t cc ti u la ph n t nho hn cac ph n t xung quanh no).
Vi du : 6429537158
49.Vi t ham tinh t ng cac ph n t la b i cua 3 va 5 trong mang cac s
nguyn.
50.Vi t ham tinh t ng cac ph n t la s hoan thin trong mang cac s
nguyn.
51.Vi t ham tinh gia tri trung binh cua cac s hoan thin trong mang cac s
nguyn.
52.Vi t ham s p x p mang theo th t giam d n.
53.Vi t ham s p x p mang theo th t tng d n cua cac ph n t la s nguyn
t .
54.Vi t ham s p x p cac ph n t le tng d n.
Chng:

55.Vi t ham s p x p cac ph n t chn giam d n.


56.Vi t ham s p x p cac ph n t chn n m bn trai theo th t tng d n con
cac ph n t le bn phai theo th t giam d n.
57.Vi t ham s p x p cac ph n t m giam d n t trai sang phai, ph n t
dng tng d n t phai sang trai.
58.Vit ham xoa ph n t ta i vi tri
le trong mang.
n nh t trong mang.
59.Vi t ham xoa ph n t co gia tri l
hn X.
60.Nh p vao gia tri X. Vi t ham xoa t t ca cac ph n t co gia tri nho
61.Nh p vao gia tri X. Vi t ham xoa ph n t co gia tri g
n X nh t.
u tin cua mang.
vao vi tri
62.Vi t ham chen gia tri X
vao phia sau ph n t co gia tri ln nh t trong
63.Vi t ham chen gia tri X
mang.
64.Vi t ham chen ph n t co gia tri X vao trc ph n t co gia tri la s
nguyn t u tin trong mang.
65.Vi t ham chen ph n t co gia tri X vao phia sau t t ca cac ph n t co gia
tri chn trong mang.
66.Vi t chng trinh tach 1 mang cac s nguyn thanh 2 mang a va b, sao
cho mang a cha toan s le va mang b cha toan s chn.
Vi du : Mang ban u: 1 3 8 2 7 5 9 0 10
Mang a: 1 3 75 9
Mang b: 8 2 10
67.Cho 2 mang s nguyn a va b kich thc l n l t la n va m. Vi t chng
trinh n i 2 mang trn thanh mang c sao cho chn u mang va le cu i
mang.
Vi du : Mang a: 3 2 7 5 9
Mang b: 1 8 10 4 12 6
Mang c: 6 12 4 10 2 8 3 1 7 5 9
Chng:

I. 14. Bi t p luyn t p va nng cao

68.Vi t chng trinh nh p vao mang a g m n ph n t, trong qua trin h nh p


ki m tra cac ph n t nh p vao khng c trung, n u trung thng bao va
yu c u nh p la i.
69.Vi t ham tinh t ng cua tng day con giam co trong mang.
70.(*) Cho mang cac s nguyn a g m n ph n t ( n 30000 ) va s dng k (
k n ). Hay chi ra s ha ng ln th k cua mang.

Vi du : Mang a: 6 3 1 10 11 18
k=2
K t qua: 10
71.(*) Cho 2 day A, B cac s nguyn (kich thc day A nho hn day B).
Hay ki m tra xem A co phai la con cua B hay khng?
72.Vi t ham li t k cac b 4 s a, b, c, d trong mang cac s nguyn (co it
nh t 4 ph n t va i m t khac nhau) sao cho a + b = c + d.
73.(*) Vi t chng trin h trung bin h c ng cua cac t ng cac day tng d n
h tin
co trong mang cac s nguyn.
Vi du : 1 2 3 4 2 3 4 5 6 4 5 6 => TB = 15.
74.Vi t chng trinh tinh t ng t t ca cac ph n t xung quanh trn mang cac
s nguyn. (Ph n t xung quanh la hai ph n t bn ca nh c ng lai b ng
chin h no (Vi du : 1 3 2 1, 2 la hai ph n t xung quanh cua 3).
Vi du : 1 3 2 5 3 9 6 t ng 17
75.(**) Vi t chng trinh nh p vao hai s ln a, b nguyn ( a, b co t 20 ch
tich, thng cua hai s trn.
s tr ln). Tinh t ng, hiu,
76.Vi t ham tinh t ng cac ph n t la s Amstrong (s Amstrong la s co c
i m nh sau: s co k ky s , t ng cua cac luy tha b c k cua cac ky s
b ng chinh s o.
Vi du : 153 la s co cac ky s 13+53+33= 153 la m t s Amstrong).
77.Vi t ham tim va xoa t t ca cac ph n t trung vi x trong mang m t chi u
cac s nguyn, n u khng t n ta i ph n t x trong mang thi tra v -1.
Chng:

78.Vi t ham xoa t t ca nhng ph n t trung nhau trong day chi gi la i m t


ph n t trong o.
Vi du : 1 6 2 3 2 4 2 6 5 1 6 2 3 4 5
79.(**) Vi t ham xoa nhng ph n t sao cho mang k t qua co th t tng
d n va s l n xoa la it nh t.
80.Cho day a g m n s nguyn co th t tng d n. Nh p vao m t ph n t
nguyn X, vi t ham chen X vao day sao cho day vn co th t tng d n
(khng s p x p).
81.Vi t chng trinh tim s le nho nh t ln hn mo i s chn co trong mang.
82.Vi t ham tim gia tri chn nho nh t nho hn mo i gia tri le trong mang cac
s nguyn.
83.Vi t ham tim ph n t xu t hin nhi u nh t trong mang cac s nguyn.
84.Vi t chng trinh m va lit k cac mang con tng d n trong mang m t
chi u cac s nguyn.
Vi du : 6 5 3 2 3 4 2 7 cac day con tng d n la 2 3 4 va 2 7
85.Vi t chng trinh tim mang con tng d n co t ng ln nh t trong mang
m t chi u.
86.(*) Vi t chng trin
h nh p vao m t day s a g m n s nguyn (n <= 100).
Tim va in ra day con tng dai nh t
Vi du : Nh p day a : 1 2 3 6 4 7 8 3 4 5 6 7 8 9 4 5
Day con tng dai nh t : 3 4 5 6 7 8 9
87.(**) Vi t chng trinh tach 1 mang cac s nguyn thanh 2 mang a va b,
sao cho k t qua thu c la:
88.Mng a cha toan s le tng d n.
89.Mng b cha toan s chn giam d n. (Khng dung s p x p)
90.Hng dn: Tim vi tri chen thich h p khi trich ph n t t mang ban u.
Vi du : Mang ban u: 9 3 8 2 7 5 1 0 10
Mang a: 1 3 5 7 9
Mang b: 10 8 2
Chng:

91.(**) Vi t chng trinh in ra tam giac Pascal (dung mang m t chi u).
92.Vi t chng trinh nh p vao day s a g m n s thc ( n <= 100 ), nh p
vao day s b g m m s thc ( m <= 100 ). Hay s p x p hai day theo th
t tng d n.
93.(*) Tr n 2 day trn thanh day c sao cho day c vn co th t tng. Xu t
day a, b, c ra man hinh.
94.(*) Cho mang C co n ph n t (n < 200), cac ph n t la cac ch s trong
h m c s 16 (Hexa) (i u kin mi ph n t <= n ). Hay tach mang C
ra cac mang con theo i u kin sau: cac mang con c gii ha n bi hai
l n xu t hi n th hai cua con s trong day .
Vi du : Dy 123A4518B23
co cac day con la123A451, 23A4518B2, 23A4518B23
95.(**) Cho hai s nguyn dng A, B. Hay xac inh
hai s C, D ta o thanh
t hai s A, B sao cho C la s ln nh t, D la s nho nh t. Khi ga ch i m t
s ch s trong C (D), thi cac s con la i gi nguyn ta o thanh A, cac ch
s bo i gi nguyn ta o thanh B.
96.V du : A = 52568, B = 462384 C = 54625682384, D = 45256236884.
97.Vi t chng trinh nh p vao day s a g m n s nguyn (n <= 100). Hay
ao ng c day o.
Vi du : Nh p a: 3 4 5 2 0 4 1
Day sau khi ao: 1 4 0 2 5 4 3
98.Hy ki m tra xem day a cho co th t cha (day c go i la th t khi
la day tng ho c day giam).
99.Cho mang A co n ph n t hay cho bi t mang nay co i xng hay khng.
100. (**) Hay vi t chng trinh phat sinh ngu nhin mang cac s
0 n 32.000 va
nguyn g m 10.000 ph n t, mi ph n t co gia tri t
xy dng ham th ng k s l n xu t hin cac ph n t trong mang, sau o
cho bi t ph n t nao xu t hin nhi u l n nh t.
Vi du : Gi tr ca mang: 5 6 11 4 4 5 4
Chng:

5 xuat hien 2 lan


6 xuat hien 1 lan
11 xuat hien 1 lan
4 xuat hien 3 lan
Kt qu : 4 xuat hien nhieu lan nhat
101. Cho mang A co n ph n t. Nh p vao s nguyn k ( k 0 ), dich
phai
xoay vong mang A k l n.
Vi du : Mang A: 572319
Nh p k = 2
phai xoay vong mang A: 1 9 5 7 2 3
Dich
Chng:

Chng 7: Con tr
Mc tiu:
Tm tt ni dung:

7.1 Bin con tr:


Bytes: B nh c chia thnh cc bytes. Mi byte c th lu tr 8 bits
thng tin

a ch ca byte: Mi bytes c mt a ch (address) duy nht phn bit


n vi nhng byte khc
Nu b nh c n bytes th chng ta c th ngh a ch c nh s t 0
n-1

V d:
RAM 512MB c nh a ch t 0 n 229 1
RAM 2GB c nh a ch t 0 n 231 1
Chng:

a ch ca bin: Mi bin trong chng trnh chim mt s byte. a ch


ca byte u tin l a ch ca bin

Mc d a ch l 1 s nguyn, nhng cc php ton trn a ch khc vi cc


php ton trn s nguyn nn khng th lu a ch vo cc bin s nguyn.
Chng ta s lu chng trong nhng bin c bit c gi l bin con tr
(Pointer Variables)
Khi lu a ch ca bin i vo bin con tr p ta ni p tr n i
Con tr (pointer): Mt Con tr l mt a ch
Bin con tr (pointer variable): Bin con tr l 1 bin chuyn dng lu
cc a ch v thc hin nhng thao tc (php ton) trn a ch

Quy trnh x l ca trnh bin dch khi khai bo bin


Dnh ring mt vng nh vi a ch duy nht lu bin .
Lin kt a ch nh vi tn bin.
Khi gi tn bin, n s truy xut t ng n nh lin kt vi tn
bin.
V d: int a = 0x1234; // Gi s a ch 0x0B
Chng:

a ch ca bin l mt con s.
Ta c th to bin khc lu a ch ca bin ny Con tr.

Khai bo cc bin con tr


C php:

V d:

Gii thch:
Bin i, j, n, m l cc bin kiu int bnh thng.
Bin a l mng cha ti a 10 phn t s nguyn int.
Bin b l mng cha ti a 20 phn t s nguyn int.
p l bin con tr, tr ti vng nh kiu int (4 bytes).
S dng t kha typedef:
Chng:

typedef <kiu d liu> *<tn kiu con tr>;


<tn kiu con tr> <tn bin con tr>;

V d:
typedef int *pint;
int *p1;
pint p2, p3;

Lu khi khai bo kiu d liu mi


o Gim bi ri khi mi tip xc vi con tr.
o Nhng d nhm ln vi bin thng.

7.2 Ton t a ch v ton t gin tip:


Con tr NULL l con tr khng tr v u c. Khc vi con tr cha c
khi to.
Phi khi to bin con tr trc khi dng, bng cch:
Gn cho bin con tr a ch ca 1 bin.
Gn cho bin con tr a ch ca 1 vng nh.
To 1 vng nh mi, gn cho bin con tr a ch vng nh .
V d:
int n;
int *p1 = &n;
int *p2; // unreferenced local varialbe
int *p3 = NULL;

Khi mi khai bo, bin con tr c t a ch no (khng bit trc).


Chng:

cha gi tr khng xc nh
tr n vng nh khng bit trc.
t a ch ca bin vo con tr (ton t &)
Ton t a ch: ly a ch ca 1 bin chng ta dng ton t &
C php: &tenBien
V d:

Ton t gin tip (Indirection operator): khi bin con tr tr n 1 i tng


(bin, vng nh), chng ta c th dng ton t gin tip , k hiu l *,
truy cp d liu c lu trong i tng .
C php: *tenBienConTro
V d:

Ch :
C th xem ton t * l o ca ton t &
p dng ton t & cho 1 bin s sinh ra 1 con tr n bin
p dng ton t * cho 1 con tr s cho chng ta ly li bin gc ban
u

Khi p tr n i chng ta ni *p l b danh (alias) ca i


Con tr l khi nim quan trng v kh nht trong C. Mc thnh
tho C c nh gi qua mc s dng con tr.
Chng:

Nm r quy tc sau, v d int a, *pa = &a;


*pa v a u ch ni dung ca bin a.
pa v &a u ch a ch ca bin a.
Khng nn s dng con tr khi cha c khi to. Kt qu s khng
lng trc.
Truy xut n nh m con tr tr n
Con tr cha mt s nguyn ch a ch.
Vng nh m n tr n, s dng ton t *.
V d
int a = 5, *pa = &a;
printf(%d\n, pa); // Gi tr bin pa
printf(%d\n, *pa); // Gi tr vng nh pa tr n
printf(%d\n, &pa); // a ch bin pa

Kch thc ca con tr


char *p1;
int *p2;
float *p3;
double *p4;

Con tr ch lu a ch nn kch thc ca mi con tr l nh nhau:


Chng:

Mi trng MD-DOS (16 bit): 2 bytes


Mi trng Windows (32 bit): 4 bytes

7.3 Php gn con tr:


C cho php dng ton t gn sao chp cc con tr c cng kiu
V d 1:

V d 2:

Ch :
Cn phn bit 2 lnh sau

7.4 Dng con tr lm i s ca hm:


C truyn cc i s bng tr hm khng cho thay i gi tr ca cc i s
Chng:
Chng:

Gii php:
Thay v truyn bin x nh l i s, chng ta cung cp &x, a ch ca x.
Chng ta s khai bo tham s tng ng l *p l b danh ca x.
Khi dng *p trong thn hm chnh l tham chiu gin tip n x.
Chng:

7.5 Dng con tr lm gi tr tr v ca hm:


Hm c th tr v 1 con tr n bt k kiu g
C php:

7.6 Cp php b nh ng:


Khi nim Cp pht b nh ng:
Vn :
(1) Vit chng trnh nhp vo dy n s nguyn (n100). Sau o
ngc dy ny

(2) Vit chng trnh nhp vo dy n s nguyn. Sau o ngc


dy ny

Nhn xt
Cc cu trc d liu trong C thng thng c kch thc c nh
Trong mt s trng hp cu trc d liu c kch thc c nh c th
hn ch chng ta khi vit chng trnh v chng p chng ta phi chn
kch thc cho chng khi vit chng trnh
nh ngha [Cp pht b nh ng] (Dynamic Storage Allocation hay Heap-
based Memory allocation): L kh nng chng trnh c th t c cc
khi b nh khi cn trong lc chng trnh ang chy.
Chng:

Dng cp pht b nh ng cho nhng kiu d liu no?


Cho tt c cc kiu nhng thng dng nht l cho
Chui
Mng
Cu trc (struct)
Cc hm cp pht b nh
malloc
Cp pht khi b nh nhng khng khi to n
calloc
Cp pht khi b nh v clear n
realloc
Thay i kch thc ca khi b nh c cp pht trc

C php tng qut

Nhn xt:
Hm malloc c dng ph bin nht
Cc hm khng cn tham s ch kiu d liu cn cp pht cho khi b
nh. V vy cc hm khng tr v con tr n kiu c th
void * hm tr v kiu con tr tng qut, ch l a ch ca b nh
p kt qu sang con tr c kiu c th
Khi nim Con tr NULL
(1) Khi hm cp pht b nh khng tm c vng nh c kch thc theo
cu ca lp trnh vin, hm s tr v con tr NULL
Chng:

(2) Con tr NULL l mt con tr khng tr n vng nh no c (pointer to


nothing). l 1 gi tr c bit phn bit vi nhng con tr khc.
V d:

Trong C, cc con tr cng c th kim tra l true hay false ging nh cc s


Con tr khc NULL gi l true
Con tr NULL l false

Cu trc chng trnh trong b nh


B nh stack
B nh heap
Program Code
Chng:

B nh stack
c dng cp pht cho nhng bin automatic c kch thc c
nh
Cc bin c cp pht lin tc v theo c ch vo sau, ra trc
LIFO

B nh heap
c dng cp pht b nh ng trong qu trnh chng trnh
chy
Nhn xt
Kch thc b nh Heap > Kch thc b nh Stack
B nh Heap chm hn b nh Stack
B nh Heap do lp trnh vin t qun l
B nh Stack do my t qun l
Cp pht b nh ng cho mng:
Dng hm malloc:
Dng hm malloc cp pht b nh cho mng ging nh dng cp
pht cho chui, im khc bit l 1 phn t ca mng khng phi lun
l 1 byte nh trong chui
C php:
Chng:

Dng hm calloc:
Dng hm calloc cp pht b nh cho mng i lc tt hn v sau khi
cp pht mng, hm ny s khi to cc bit bng 0
C php:

Cp pht li b nh ng cho mng:


Hm realloc:
Sau khi cp pht b nh cho mng, chng ta c th thay i kch
thc ca mng
C php:

Ch :
ptr phi tr n khi b nh do cc hm malloc, calloc, realloc
trc cp pht
size c th ln hn hay nh hn kch thc b nh ban u
Gii phng b nh cp pht:
Hm free
C php:

Chc nng: Gii phng khi b nh cp pht bng nhng hm cp


pht b nh.
Ch : Con tr ptr phi l mt con tr c tr v bi nhng hm cp
pht b nh
Cp pht b nh ng cho chui:
Chng:

C php [Dng hm malloc]:

Hnh nh

Ch :
Nh dnh ch cho k t null
Cc gi tr trong khi b nh ny cha c khi to
P tr n 1 mng gm n+1 phn t
Khi to chui trc khi dng

ng dng 1: [Dng cp pht b nh ng trong cc hm chui]:


Dng cp pht b nh ng, chng ta c th to ra nhng hm tr v 1
con tr tr n mt chui mi
V d:
Chng:

Ch :
Dng nhng hm nh th phi rt cn thn v sau khi dng xong vng
nh c hm tr v, chng ta phi xa vng nh
ng dng 2: [Mng cc chui c cp pht ng]:
Bi ton: Hy vit chng trnh lu tr cc chui
Gii php 1: [Mng 2 chiu]
Chng ta c th dng mng 2 chiu lu cc chui, mi chui
l 1 dng trong mng 2 chiu
Chng:

Gii php 2: [Mng cc chui c cp pht ng]


Chng ta to mt mng cc con tr chui. Khi cn chui c bao
nhiu k t chng ta s cp chui c chnh xc s lng k t
cn.
Chng:

Cp pht b nh ng cho 1 i tng:


V hm calloc khi to cc bit bng 0 nn thng c dng cp pht cho
1 i tng hn l 1 mng
V d:

Truy cp cc thnh vin trong cu trc


C php 1:

C php 2:

V d:
Chng:

struct PHANSO
{
int tu, mau;
};
PHANSO ps1, *ps2;
ps1.tu = 1; ps1.mau = 2; // ps1 = 1/2
ps2 = &ps1;
ps2->tu = 3; ps2->mau = 4; // ps1 = 3/4

Tm tt chng:

Bi tp:
Bi 1: Cho on chng trnh sau:
float pay;
float *ptr_pay;
pay=2313.54;
ptr_pay = &pay;
Hy cho bit gi tr ca:
a. pay
b. *ptr_pay
c. *pay
d. &pay
Bi 2: Tm li
#include<stdio.h>
#include<conio.h>
void main()
{
Chng:

int *x, y = 2;
*x = y;
*x += y++;
printf("%d %d",*x,y);
getch();
}
Bi 1: Ton t no dng xc nh a ch ca mt bin?
Bi 2: Ton t no dng xc nh gi tr ca bin do con tr tr n?
Bi 3: Php ly gi tr gin tip l g?
Bi 4: Cc phn t trong mng c sp xp trong b nh nh th no?
Bi 5: Cho mng mt chiu data. Trnh by 2 cch ly a ch phn t u
tin ca mng ny.
Bi 6: Nu ta truyn cho hm i s l mng mt chiu. Trnh by hai cch
nhn bit phn t cui ca mng?
Bi 7: Trnh by 6 php ton c th thc hin trn con tr?
Bi 8: Cho con tr p1 tr n phn t th 3 cn con tr p2 tr n phn t
th 4 ca mng int. p2 p1 = ?
Bi 9: Ging nh cu trn nhng i vi mng float?
Bi 10: Trnh by khai bo con tr pchar tr n kiu char.
Bi 11: Cho bin cost kiu int. Khai bo v khi to con tr pcost tr n
bin ny.
Bi 12: Gn gi tr 100 cho bin cost s dng hai cch trc tip v gin tip.
Bi 13: In gi tr ca con tr v gi tr ca bin m n tr ti.
Bi 14: S dng con tr lm li cc bi tp v mng mt chiu.
Bi 15: Cho on chng trnh sau:
int *pint;
float a;
Chng:

char c;
double *pd;
Hy chn pht biu sai c php:
a. a = *pint;
b. c = *pd;
c. *pint = *pd;
d. pd = a;
Bi 16: Vit chng trnh nhp s nguyn dng n gm k ch s (0 < k 5)
, sp xp cc ch s ca n theo th t tng dn.
V d:
Nhp n = 1536
Kt qu sau khi sp xp: 1356.
Chng:

Chng 8 : Cu trc File


Mc tiu:
Tm tt ni dung:

8.1 Khi nim file:


File: File l mt i tng trn my tnh lu d liu.
File trong C: File l mt kiu d liu dng thao tc vi file trong my
tnh.
Phn loi file da trn dng d liu:
File vn bn:
File vn bn dng ghi cc k t ln a theo cc dng.
Mi dng c kt thc bng du EOLN (End Of Line) gm 2
k t CR (\r) v LF (\n).
Kt thc file bi k t EOF (End Of File) c m ASCII l 26
c xc nh bi t hp phm CTRL+Z (k hiu ^Z).
File nh phn:
File nh phn dng ghi cc cu trc di dng cc byte nh
phn.
Kt thc file bi k t EOF (-1).

8.2 Cc bc thao tc trn file:


Thao tc vi kiu file:
Bc 1: Khai bo bin con tr FILE
Bc 2: M file
Bc 3: Thc hin cc thao tc trn file (c/ghi)
Bc 4: ng file
Chng:

Khai bo bin file:


ghi d liu ra file hay c d liu t file chng ta khai bo bin file

8.3 M file:
m file chng ta dng hm fopen()
C php:

filename: chui cha tn file trn a bao gm ng dn n tn file.


mode:
r: M file c d liu ra
w: M file ghi d liu vo
Nu m file thnh cng hm tr v con tr tr ti kiu FILE tng
ng vi file va m, ngc li tr v gi tr NULL.

8.4 c ghi d liu trn file:


Sau khi file m, chng ta dng hm fscanf() c d liu v fprintf()
ghi d liu ra file
c d liu
C php:

Ghi d liu
C php:

8.5 ng file:
Chng:

Sau khi dng xong file, phi ng file li nu khng s mt d liu:


C php:

8.6 Vng m file (File Buffer)


Mi ln c/ghi file, C s dng mt vng b nh c gi l vng m ca
file (file buffer) trao i d liu.

Tm tt chng:

Bi tp:
Chng:

Chng 9: Con tr v mng


Mc tiu:
Tm tt ni dung:

9.1 Php ton s hc trn con tr:


V d: ta c khai bo sau
int array[3];
Tn mng array l mt hng con tr
khng th thay i gi tr ca hng ny.
array l a ch u tin ca mng
array == &array[0]

Con tr v phn t mng


Bin con tr c th tr n cc phn t ca mng

Truy cp phn t ca mng thng qua bin con tr


Chng:

Thc hin cc php ton s hc trn con tr cho php chng ta x l cc


phn t khc ca mng
Cc loi php ton s hc trn con tr
Cng mt s nguyn vo con tr
Tr mt s nguyn khi con tr
Tr 2 con tr vi nhau
Cng mt s nguyn vo con tr:
Cng s nguyn j vo con tr p s sinh ra 1 con tr n phn t th j
sau phn t p ang tr n
V d:
Chng:

Tr mt s nguyn khi con tr:


Tr s nguyn j khi con tr p s sinh ra 1 con tr n phn t th j
trc phn t p ang tr n
V d:

Tr 2 con tr vi nhau
Khi tr con tr p v con tr q kt qu l khong cch (s phn t
mng) gia cc con tr

So snh cc con tr
Chng:

Chng ta c th so snh cc con tr bng cc ton t quan h (>, <,


>=, <=, ==, !=) ch c ngha khi c 2 u tr n cng 1 mng
Quan h gia 2 con tr l quan h th t trc sau ca 2 phn t trong
mng

Khng th thc hin cc php ton: * / %

9.2 Dng con tr x l mng:


Cc php ton s hc cho php chng ta ving thm cc phn t ca mng
V d: tnh tng mng

9.3 Dng tn mng nh con tr:


Mng v con tr c quan h mt thit vi nhau:
Thng qua cch dng ton t trn con tr
Thng qua tn mng
Tn ca mng c th c dng nh l 1 con tr n phn t u tin ca
mng
Quan h ny gip n gin ha thao tc s hc trn con tr v lm mng v
con tr linh hot hn
Chng:

V d: Tnh tng mng

Ch : Mc d tn mng c dng nh l con tr nhng khng th gn gi


tr mi cho tn mng

i s ca hm l mng: Khi truyn 1 mng vo hm, tn mng lun lun


c coi nh l con tr (khng sao chp c mng ln)
u im:
Hm c th thay i d liu ca mng
Thi gian truyn mng cho hm khng ph thuc kch thc mng
Tham s mng c th c khai bo nh l 1 con tr nu mun
Chng:

u im
C th yu cu hm x l 1 on no gm mt s phn t ca
mng
V d: Tm gi tr ln nht ca on a[5], a[6], a[7], a[8], a[9], a[10],
a[11], a[12], a[13], a[14]

9.4 Dng con tr nh tn mng:


C cho php chng ta dng ch mc trn cc bin con tr

Trnh bin dch coi p[i] nh l *(p+i)

9.5 Con tr v mng nhiu chiu:


Bin con tr cng c th tr n nhng phn t ca mng nhiu chiu nh 1
chiu
Chng:

Trong phn ny ch trnh by k thut thng dng trn mng 2 chiu (mng
c s chiu ln hn cng tng t)
C lu tr cc mng nhiu chiu theo th t cc dng lin tip nhau

X l mi phn t ca mng nhiu chiu


B1: Cho con tr p tr n phn t u tin ca mng (phn t ti dng
0, ct 0)
B2: Ving thm mi phn t trong mng bng cch tng dn p

X l cc dng ca mng nhiu chiu: X l dng i trong mng


B1: Cho con tr p tr n phn t u tin ca dng i (phn t ti
dng i, ct 0)

B2: Ving thm mi phn t trong dng i bng cch tng dn p

X l cc ct trong mng nhiu chiu: X l ct i trong mng


Chng:

Dng tn ca mng nhiu chiu nh mt con tr

a khng l mt con tr n a[0][0]


a l mt con tr n a[0]
Khi dng nh con tr, a c kiu
int (*) [NUMCOLS]
Khi bit a tr n a[0], chng ta c th n gin vng lp

Tm tt chng:

Bi tp:
Chng:

Chng 10: Chui Xu k t


Mc tiu:
Tm tt ni dung:

10.1 Hng s, hng k t v Escape Sequence:


Hng s (number constant number literal): l nhng gi tr s xut hin
trong vn bn ca chng trnh
V d: 1, 234, 439
Hng k t (character constant character literal): l mt k t c t bn
trong cp du nhy n
V d: a, A, 1
Khi nim Escape Sequence: Escape Sequence l mt thut ng ca C
dng ch cch g nhng k t c cng dng c bit vo vn bn chng
trnh
Phn loi Escape Sequence: c 2 loi
Character escape
Numeric escape
Mt s character escapes

Tn cng dng Escape Sequence

Alert (bell) \a

Backspace \b

New line \n

Carriage return \r

Horizontal tab \t
Chng:

Backslash \\

Single quote \

Double quote \

Numeric escape:
S octal: Bt u k hiu \, sau l 1 s octal c ti a 3 ch s
V d: \34 hay \034
S hexa: Bt u k hiu \x, sau l 1 s hexa
V d: \x1f hay \x1F
Khi dng Escape Sequence nh 1 k t th phi trong cp nhy n
V d: \34

10.2 Hng chui:


Chui (string): l mt dy cc k t
Hng chui (string constant string literal): Hng chui l 1 dy cc k t
c t trong cp du nhy i
V d:
"Don't cry because its over, Smile because it happened "
Escape sequence trong string literal
String literal c th cha cc escape sequence nh l 1 k t
V d: Ket qua\ncua x + y\nla z s xut ra mn hnh dng

Ket qua
cua x+y
la z
Chng:

Tip tc hng chui: khi mt hng chui di qu 1 dng, C cho php chng
ta tip tc hng chui ny trn dng tip theo
Cch 1:
Kt thc dng bng backslash \
Dng k tip phi bt u ngay dng tip theo

Cch 2: Theo quy tc sau: khi hai hay nhiu hng chui lin k nhau
(cch nhau ch bng cc khong trng) trnh bin dch s ni chng
thnh 1 dng

Cch lu tr cc hng chui:


Ngn ng C xem cc hng chui l nhng mng k t
Khi gp hng chui c n k t, trnh bin dch cp pht 1 mng k t
c n+1 bytes dng cha
n k t trong chui
1 k t thm null character nh du kt thc chui
null character: l 1 byte c 8 bit u bng 0. Vi vy thng c biu din
bng escape sequence \0
V d: Hng chui abc
a b c \0
V d: Hng chui rng
\0
Truyn hng chui cho cc hm: V hng chui c lu nh l 1 mng nn
trnh bin dch coi ni nh l kiu const char *
V d:
Chng:

Khi hm printf c gi n truyn a ch ca Khanh Phuong (con


tr n a ch k t K c lu trong b nh)
Cc ton t trn hng chui
Nguyn tc: Chng ta c th dng hng chui nhng ni C cho php
con tr char *

Ch : cc hng chui khng c chnh sa

S khc bit gia hng chui cha 1 k t v hng k t:


Hng chui cha 1 k t l 1 con tr n k t c lu trong b nh
(theo sau k t l 1 k t null)
Hng k t l 1 con s nguyn (m s ca k t)

10.3 Bin chui:


Chng:

Kiu char ch cha c mt k t. lu tr mt chui (nhiu k t) ta s


dng mng (mt chiu) cc k t.
Chui k t kt thc bng k t \0 (null)
di chui = kch thc mng 1
V d:
char hoten[30]; // Di 29 k t
char ngaysinh[9]; // Di 8 k t

Khi nim Bin chui: Trong C, 1 bin chui l 1 mng mt chiu gm cc


k t v kt thc bng k t null.
C php:

V d:

Khi to bin chui


Cch 1:

Cch 2:

V d:
Chng:

Ch :
Nu initializer ngn hn di chui th cc k t cn li bng 0
Nu initializer bng hay di hn di chui li
Nu chiu di chui khng ch ra th trnh bin dch t cp pht va
nh cho bin chui
So snh gia mng k t v con tr k t

Ging nhau
Tham s ca hm l mng k t hay con tr k t u c coi
nh l con tr k t
Nu tham s hm l mng k t hay con tr k t th i s ca
n l phin bn no cng c
Khc nhau
D liu trong mng k t c php thay i
Nu d liu ca con tr k t l hng chui th d liu khng
c thay i
Con tr k t l 1 bin nn n c th tr n mt chui khc
Trong mng k t, tn mng l 1 con tr v khng c tr n
1 chui khc
To chui c th c chnh sa
Cch 1: Hng chui khng chnh sa c
Cch 2: To 1 mng k t lu chui cn chnh sa OK
Dng con tr k t thao tc chui

Cch 1: Cho p tr n 1 mng k t


Chng:

Cch 2: Cho p tr n 1 chui c cp pht ng


Ch : bin p cha c khi to m dng nh 1 bin chui li

Khi to nh mng thng thng


di c th:
char s[10] = {T, H, C, S, A, , \0};
char s[10] = THCS A; // T ng thm \0

T xc nh di
char s[] = {T, H, C, S, , A, \0};
char s[] = THCS A; // T ng thm \0

10.4 c v ghi chui:


Xut chui ln mn hnh
Cch 1:

Sau khi xut th khng xung dng


Cch 2:

Sau khi xut th t ng xung dng


Chng:

c chui vo bin
Cch 1:

Ch :
Khng cn t k t & trc bienChuoi
Hm scanf dng c chui khi gp k t white space (khong
trng) hay k t new line hay tab (cc k t ny khng lu
trong mng)
Hm scanf lun lun thm k t null vo cui chui.
Nhp chui khng c khong trng.
Cch 2:

Ch :
Hm gets dng c chui khi gp k t new line (k t ny
khng lu trong mng)
Hm gets lun lun thm k t null vo cui chui.
Nhp chui c khong trng.
Hn ch ca hm c chui:
Khng th no xc nh c y mng k t trong qu trnh c
chui
Gii php 1:

Gii php 2: T vit hm c chui ring bng hm getchar()


int ReadLine(char str[], int n)
{
Chng:

10.5 Truy cp cc k t trong chui:


Chui c lu nh l 1 mng nn chng ta c th dng ch mc truy cp
cc k t trong chui
C php:

V d: [m khong trng]
Vit hm m s khong trng trong chui

10.6 Dng th vin chui:


Chui trong C c coi nh l mng cho nn khng th dng nhng ton t
trn chui (nh php so snh, php gn, )
V d:

Tp hm thao tc trn chui c cc prototype trong <string.h>

Ch :
Cc hm khai bo trong <string.h> c t nht 1 tham s chui
Cc tham s chui c khai bo kiu char *
Ch :
Chng:

Cc tham s chui char * c th truyn cc i s: mng k t, bin


kiu char * nhng khng c l hng chui
Cc tham s chui const char * c th truyn cc i s: mng k t,
bin kiu char * hay hng chui
Hm strcpy (String Copy):

Chc nng:
Sao chp chui s2 sang chui s1.
(Chnh xc hn: Sao chp chui c tr bi s2 sang mng c tr
bi s1)
Hm strcpy() tr v s1.
Hm strncpy:

Chc nng:
Sao chp n k t trong chui s2 sang chui s1.
(Chnh xc hn: Sao chp chui c tr bi s2 sang mng c tr
bi s1 ch vi n k t)
Hm strncpy() tr v s1
Hm strlen (String Length):

Chc nng:
Ly chiu di chui s: s k t trong chui s, khng tnh n k t
NULL
Hm strcat (String Concatenation):

Chc nng:
Chng:

Ni chui s2 vo cui chui s1.


Hm strcat() Tr v chui s1.
Hm strncat:

Chc nng:
Ni n k t trong chui s2 vo cui chui s1.
Hm strncat() Tr v chui s1.
Hm strcmp (String Comparison):

Chc nng:
So snh 2 chui s1 v s2 tng k t t tri sang phi theo th t t
in (da trn m ASCII).
Gi tr tr v:
<0 th s1<s2
>0 th s1>s2
==0 th s1==s2
Hm strchr:

Chc nng:
Tm kim v tr xut hin u tin ca k t c trong chui s.
Hm strchr() tr v con tr n k t tm thy hay l gi tr NULL
nu khng tm thy.
Hm strstr:

Chc nng:
Tm kim v tr xut hin u tin ca chui s2 trong chui s1.
Chng:

Hm strstr() tr v con tr n chui tm thy hay gi tr NULL


nu khng tm thy.
10.7 Vit li mt s hm th vin chui:
Vit li hm strlen
size_t MyStrLen(const char *s)
{

}
Vit li hm strcat
char *MyStrCat(char *s1, const char *s2)
{

10.8 Mng cc chui:


Cch 1:

V d:

0 1 2 3 4 5 6

0 N h i \0 \0 \0 \0

1 M i n h \0 \0 \0

2 T r a m \0 \0 \0
Chng:

Cch 2:

V d:

Ch :
T t ca d liu khi go vo t ban phim u c lu vao 1 b nh tm thi
gi l b m (buffer)
Cac bc o c d liu t ban phim ca hm gets()
Bc 1: Nu buffer c d liu: Hm tun t ly d liu trong
buffer a vo bin cho n khi gp k t newline.
Bc 2: Nu buffer rng: Ch ngi dng nhp d liu vo.
Tng k t c a vo buffer ri vo bin cho n khi gp
k t newline.
Bc 3: Xa b k t newline khi buffer.
Cac bc o c d liu t ban phim ca hm scanf()
Bc 1: Xa b k t newline khi buffer.
Bc 2: Nu buffer c d liu: Hm tun t ly d liu trong
buffer a vo bin cho n khi gp k t newline, tab hay
white space.
Chng:

Bc 3: Nu buffer rng: Ch ngi dng nhp d liu vo.


Tng k t c a vo buffer ri vo bin cho n khi gp
k t newline , tab hay white space.
V n o c s r i sau o c chui:

Giai phap: Xoa b m


trc khi nh p chui
Bc 1: scanf("%d", &so);
Bc 2: flushall();
Bc 3: gets(chuoi);
Hay dng hm chun: fflush(stdin);

Tm tt chng:

Bi tp:
Bi 1: Xem thm mt s hm khc nh
atoi, atol, atof : i chui thnh s
itoa, ltoa, ultoa: i s thnh chui
strtok
Bi 2: Vit hm upper(char s[]) i ton b cc k t sang k t hoa (ging
hm strupr)
Chng:

Bi 3: Vit hm lower(char s[]) i ton b cc k t sang k t thng


(ging hm strlwr)
Bi 4: Vit hm proper(char s[]) i cc k t u tin ca mi t sang k t
hoa.
Bi 5: Vit hm standard(char s[]) b ton b khong trng u chui, cui
chui v gia 2 t trong s ch cn 1 khong trng.
Bi 6: Xa tt c cc khong trng ca s
Bi 7: m xem c bao nhiu t trong s. Xut cc t trn cc dng lin tip.
Bi 8: Tm t c chiu di di nht v in ra.
Bi 9: Trch ra n k t u tin/cui cng/bt u ti v tr pos.
Chng:

Chng 11: Structure v enum


Mc tiu:
Tm tt ni dung:

11.1 Khi nim cu trc (structure):


t vn :
Thng tin 1 SV:
MSSV : kiu chui
Tn SV : kiu chui
NTNS : kiu chui
Phi : k t
im Ton, L, Ha : s thc
Khai bo cc bin lu tr 1 SV
char mssv[7]; // 0012078
char hoten[30]; // Nguyen Van A
char ntns[8]; // 29/12/82
char phai; // y Nam, n N
float toan, ly, hoa; // 8.5 9.0 10.0
Truyn thng tin 1 SV cho hm
void xuat(char mssv[], char hoten[], char ntns[], char phai, float
toan, float ly, float hoa);
Nhn xt
t tn bin kh khn v kh qun l
Truyn tham s cho hm qu nhiu
Tm kim, sp xp, sao chp, kh khn
Tn nhiu b nh

Chng:

tng
Gom nhng thng tin ca cng 1 SV thnh mt kiu d liu
mi => Kiu struct

Yu cu
Lu thng tin ca n SV?
Truyn thng tin ca n SV vo hm?

C u truc struct (vit tt ca structure): c u truc la mt loi kiu d liu


gm c nhiu thnh phn (member, field, bin), c th c kiu khc nhau,
c gm li thnh mt kiu duy nht m t cho mt i tng.
Thnh vin cua c u truc (Member): M t bin trong c u truc c gi l 1
thnh vin (member) hay field hay bin thnh vin.

11.2 Khai bo cu trc:


Nhng thut ng lin quan n struct:
Structure = Record
Member = Field
C php:

V d: Chng ta cn lu tr thng tin ca sinh vin. Thng tin ca sinh vin


gm:
M sinh vin (chui),
H tn (chui),
Chng:

Nm sinh (s nguyn)

Hinh anh
Chi ti t

Chu y:
Cac thanh vin cua c u truc c lu tr trong b nh theo th t
chung c khai bao
Mi c u truc la 1 pha m vi (scope) mi, b t k tn bi n nao c khai
bao trong pha m vi nay se khng xung t vi nhng tn khac trong
chng trnh
Vi du :
Chng:

Khi to bi n c u truc (structure initializer): Bi n c u truc ging nh bt k


bin khc, chng ta c th khi to trong khi khi bo bi n c u truc.
C php: Danh sch cc gi tr c t trong { v }, cch nhau bng k
hiu , va t theo ung th t cac thanh vin cua c u truc.
struct
{
char maSV[10];
char hoTen[50];
int namSinh;
} sv1 = {TH09001, Nguyen Van A, 1992},
sv2 = {TH09002, Le Thi B, 1993};
11.3 Ton t trn cu trc:
Toan t truy c p thanh vin (toan t .):
Ta khng th truy cp trc tip n cc thnh vin ca cu trc. truy c p
chng, ta dung tn bi n c u truc, theo sau la d u . v tn thanh vin
Cu phap:
Chng:

tenBienCauTruc.tenThanhVien;
Vi du :
printf(Ho ten: %s \n, sv1.hoTen);
sv1.namSinh++;

strcpy(sv1.maSV, TH0903);
sv1.namSinh = 1994;

scanf(%d, &sv1.namSinh);
gets(sv1.hoTen);
Toan t gan gia 2 bi n c u truc:
Co th sao chep toan b d liu t bi n c u truc nay sang bi n c u truc
tng thich b ng phep gan
Vi du :
sv1 = sv2;
Chc nng: Sao chep cac thanh vin tng ng gia 2 c u truc tng
thich cho nhau.
C 2 cch
<bin cu trc ch> = <bin cu trc ngun>;
<bin cu trc ch>.<tn thnh phn> = <gi tr>;

V d
struct DIEM
{
int x, y;
} diem1 = {2912, 1706}, diem2;
Chng:


diem2 = diem1;
diem2.x = diem1.x;
diem2.y = diem1.y * 2;

Ch y:
2 mang khng c dung phep gan sao chep d liu
Khi mang c nhung vao trong c u truc, phep toan gan trn bi n c u
truc se t ng sao chep d liu cua mang a nhung o
Ngoai toan t gan =, chng ta khng th thc hin nhng toan t
khac trn toan c u truc (vi du : >, <, ==, !=, )
Cac bi n c u truc tng thich: Hai bi n c u truc c go i la tng
thich n u 2 bi n c u truc c khai bao
Cung thi i m. Vi du sv1, sv2
Cung tn ki u c u truc.
struct struct
{ {
char maSV[10]; char maSV[10];
char hoTen[50]; char hoTen[50];
int namSinh; int namSinh;
} sv1, sv2; } sv3;

t tn cho Ki u c u truc:
t tn cho Ki u c u truc: vi c khai bao cac bi n c u truc ng n go n, d
thay i va cac bi n c u truc cung ki u tng thich vi nhau, C cho phep
chng ta t tn cho ki u c u truc
Chng:

Phn loa i: Co 2 loa i


Khai bao structure tag
Dung typedef inh
nghia structure
Structure Tag: la m t tn c dung xac inh
loa i c u truc cu th
Cu phap:
struct tenStructureTag
{
<Kieu1> tenMember1;
<Kieu2> tenMember2;

};
Vi du :
struct SinhVienTag
{
char maSV[10];
char hoTen[50];
int namSinh;
};
S du ng structure tag:
Cu phap:
struct tenStructureTag tenBien1, tenBien2, ;
Vi du :
struct SinhVienTag sv1, sv2;
struct SinhVienTag sv3;
Chng ta co th va khai bao structure tag va khai bao bi n
Vi du :
struct SinhVienTag
Chng:

{
char maSV[10];
char hoTen[50];
int namSinh;
} sv1, sv2;
Ch y: T t ca cac bi n thu c cung structure tag se tng thich vi nhau
Dung typedef chng ta co th inh
nghia tn cho ki u structure
Cu phap:
typedef struct
{
<Kieu1> tenMember1;
<Kieu2> tenMember2;

} TenKieu;
Vi du :
typedef struct
{
char maSV[10];
char hoTen[50];
int namSinh;
} SinhVien;
SinhVien sv1, sv2;
SinhVien sv3;
Nh n xet: Dung SinhVien nh la 1 ki u co sn trong C
Thnh phn ca cu trc l cu trc khc
struct DIEM
{
Chng:

int x;
int y;
};
struct HINHCHUNHAT
{
struct DIEM traitren;
struct DIEM phaiduoi;
} hcn1;

hcn1.traitren.x = 2912;
hcn1.traitren.y = 1706;
Cu trc quy (t tr)
struct PERSON
{
char hoten[30];
struct PERSON *father, *mother;
};
struct NODE
{
int value;
struct NODE *pNext;
};
Thnh phn ca cu trc c kch thc theo bit
struct bit_fields
{
Chng:

int bit_0 : 1;
int bit_1_to_4 : 4;
int bit_5 : 1;
int bit_6_to_15 : 10;
};

11.4 Cu trc v hm:


Ging nh truyn kiu d liu c s
Tham tr (khng thay i sau khi kt thc hm)
Tham chiu
Con tr
C u truc co th c dung lam tham s v ki u tr v cua ham
Vi du :
void Func1( struct SinhVienTag sv)
{
}
void Func2( SinhVien sv)
{
}
struct SinhVienTag Func3()
{
struct SinhVienTag sv;
.
return sv;
Chng:

}
SinhVien Func4()
{
SinhVien sv;

return sv;
}
V n : Khi truy n tham s c u truc cho ham v tr v ki u d liu c u truc,
ca 2 u thc hin sao chep t t ca cac thanh vin trong c u truc T n thi
gian
Giai phap:
Truy n con tr n c u truc
Tr v con tr n c u truc
V d
struct DIEM {
int x, y;
};
void xuat1(int x, int y) { };
void xuat2(DIEM diem) { };
void xuat3(DIEM &diem) { };
void xuat4(DIEM *diem) { };
Hp nht union:
Khi nim
c khai bo v s dng nh cu trc
Cc thnh phn ca union c chung a ch u (nm chng ln nhau
trong b nh)
Chng:

Khai bo
union <tn kiu union>
{
<kiu d liu> <tn thnh phn 1>;

<kiu d liu> <tn thnh phn 2>;
};
So snh struct v union:
V d:
struct MYSTRUCT union MYUNION
{ {
char c; char c;
int n; int n;
} s; } u;
s.c = 1; s.n = 2; u.c = 1; u.n = 2;

struct trong union


union date_tag
{
char full_date[9];
struct part_date_tag
Chng:

{
char month[2];
char break_value1;
char day[2];
char break_value2;
char year[2];
};
} date = {29/12/82};
union trong struct
struct generic_tag
{
char type;
union share_tag
{
char c;
int i;
float f;
};
};
11.5 Mng cu trc:
Tng t nh mng vi kiu d liu c s (char, int, float, )
V d:
struct DIEM
{
int x;
Chng:

int y;
};
DIEM mang1[20];
DIEM mang2[10] = {{3, 2}, {4, 4}, {2, 7}};
Mang v c u truc co th t h p vi nhau
Mang cac c u truc: Mang co cac ph n t thu c ki u c u truc
C u truc co th cha
Mang nh la thanh vin
C u truc khac nh la thanh vin
Con tr n c u truc ang inh
nghia
Trong nhi u trng h p, c u truc nay l ng c u truc kia se giup vi t chng
trnh d dang hn
Vi du :
struct HoVaTenTag
{
char ho[10];
char tenLot[30];
char ten[10];
};
Chng ta co th dung c u truc HoVaTenTag trong 1 c u truc ln hn:
struct SinhVienTag
{
char maSV[10];
struct HoVaTenTag hoTen;
int namSinh;
};
Chng:

struct SinhVienTag sv1, sv2;


Truy c p thanh vin: ho, tenLot, ten cua bi n sv1, sv2 c n dung 2 toan
t .
strcpy(sv1.hoTen.ho, Nguyen);
gets(sv1.hoTen.ten);
u i m: 1 c u truc c coi la 1 n vi
S l ng tham s truy n cho ham giam
S l ng phep gan sao chep d liu giam
Cac ph n t cua mang co th thu c 1 ki u c u truc. Va c coi la 1 c s
d li u (database) n gian
Vi du : ta o danh sach t i a 100 sinh vin.
struct SinhVienTag dsSinhVien[100];
Truy c p ph n t cua mang dung chi mu c:
Function(dsSinhVien[i]);
Truy c p thanh vin c u truc trong mang dung chi mu c v toan t
truy c p thanh vin:
dsSinhVien[i].namSinh = 1994;
Mix:
dsSinhVien[i].masv[0] = \0;
gets(dsSinhVien[i].hoTen.ho);
dsSinhVien[i].hoTen.ten[0] = \0
Bai t p: Vi t chng trnh quan l CSDL g m n (n100) sinh vin. Chng
trnh co nhng thao tac sau:
[Insert] Thm 1 SV vao CSDL. N u SV t n ta i trong CSDL hay
CSDL y bao li
[Search] Cho ma SV, in ho tn v nm sinh ca SV . N u ma SV
khng co trong CSDL bao li
Chng:

[Update] Cho ma SV, c p nh t la i nm sinh ca SV . N u ma SV


khng co trong CSDL bao li
[Delete] Cho ma SV, xa SV . N u ma SV khng co trong CSDL
bao li
[Print] Xu t t t ca SV trong CSDL.
11.6 Khi nim enum:
V n : Trong nhi u chng trnh chng ta c n 1 bi n co gia tri chi n m
trong m t t p nho gia tri co y nghia .
Vi du :
Bi n ki u boolean, chi co th nh n 1 trong 2 gia tri: true,
false
Bi n dung xp hng: gii, kh, trung bnh, yu
Gii php 1: [Gii php s nguyn]
Khai bo bin l s nguyn v quy c nhng gi tr s nguyn tng
ng vi cc gi tr cc bin
V d:
//Qui c
// +0 gii, 1 kh, 2 trung bnh, 3 yu
int loai;
loai = 1; //loi kh.
Giai phap 2: [Gii php macro]
Dng macro nh ngha kiu v cc gi tr cho cc kiu
V d:
#define HANG int
#define GIOI 0
#define KHA 1
#define TRUNGBINH 2
Chng:

#define YEU 3
Hang loai;
Loai = KHA;
Giai phap 3: [Giai phap Enum]
C cung cp 1 loa i ki u c bit cho nhng bin ch c mt s nh cc
gi tr, l kiu enum.
Kiu enum l mt kiu m tp gi tr ca n do lp trnh vin ch ra. Nhng
gi tr ca kiu enum (gi l enumeration constant) l 1 tn do lp trnh vin
to.
11.7 Khai bo enum:
C php:
enum {giatri1, giatri2, } tenBien1, tenBien2, ;
V d:
enum {GIOI, KHA, TRUNGBINH, YEU, } loai1, loai2;
loai1 = GIOI;
loai2 = KHA;
To tn cho kiu enum
Khai bo 1 tag
Dng typedef nh ngha kiu
Khai bo 1 tag
C php:
enum tenTag {giatri1, giatri2, };
V d:
enum HangTag {GIOI, KHA, TRUNGBINH, YEU};
enum HangTag loai1, loai2;
11.8 S dng enum:
Dng typedef
Chng:

C php:
typedef enum {giatri1, giatri2, } TenKieu;
V d:
typedef enum {GIOI, KHA, TRUNGBINH, YEU} Hang;
Hang loai1, loai2;
typedef enum {TRUE, FALSE} Bool;
Bool ok=TRUE:
enum v int
C xem cc bin enum v cc hng enum l cc s nguyn. Mc nhin
trnh bin dch gn cc s nguyn 0, 1, 2, , cho cc hng enum
V d: Gioi = 0, Kha = 1
Chng ta c quyn t do chn la cc gi tr cho cc hng enum
V d:
enum HangTag {GIOI=1, KHA=2, TRUNGBINH=3, YEU=4};
Cc hng enum c th l cc s nguyn ty
V d:
enum HangTag {GIOI=100,KHA=150,TRUNGBINH=300,YEU=500};
Nu khng ch r gi tr ca hng enum th gi tr ca n bng gi tr
ca hng trc cng 1.
enum HangTag {GIOI,KHA=100,TRUNGBINH,YEU=500};
C th dng ln ln gia s nguyn v hng enum

Tm tt chng:

Bi tp:
1. Phn s
Khai bo kiu d liu phn s (PHANSO)
Chng:

Nhp/Xut phn s
Rt gn phn s
Tnh tng, hiu, tch, thng hai phn s
Kim tra phn s ti gin
Quy ng hai phn s
Kim tra phn s m hay dng
So snh hai phn s
2. n thc
Khai bo kiu d liu n thc (DONTHUC)
Nhp/Xut n thc
Tnh tch, thng hai n thc
Tnh o hm cp 1 ca n thc
Tnh gi tr n thc ti x = x0
3. a thc
Khai bo kiu d liu a thc (DATHUC)
Nhp/Xut a thc
Tnh tng, hiu, tch, thng hai n thc
Tnh o hm cp 1 ca n thc
Tnh o hm cp k ca n thc
Tnh gi tr n thc ti x = x0
4. im trong mt phng Oxy
Khai bo kiu d liu im (DIEM)
Nhp/Xut ta im
Tnh khong cch gia hai im
Tm im i xng qua gc to /trc Ox/Oy
Kim tra im thuc phn t no?
5. Tam gic
Chng:

Khai bo kiu d lu tam gic (TAMGIAC)


Nhp/Xut tam gic
Tnh chu vi, din tch tam gic
6. Ngy
Khai bo kiu d liu ngy (NGAY)
Nhp/Xut ngy (ngy, thng, nm)
Kim tra nm nhun
Tnh s th t ngy trong nm
Tnh s th t ngy k t ngy 1/1/1
Tm ngy trc , sau k ngy
Tnh khong cch gia hai ngy
So snh hai ngy
7. Mng phn s
Nhp/Xut n phn s
Rt gn mi phn s
m s lng phn s m/dng trong mng
Tm phn s dng u tin trong mng
Tm phn s nh nht/ln nht trong mng
Sp xp mng tng dn/gim dn
8. Mng im
Nhp/Xut n im
m s lng im c honh dng
m s lng im khng trng vi cc im khc trong mng
Tm im c honh ln nht/nh nht
Tm im gn gc ta nht
Chng:

Ti liu tham kho:


[1] Brian W. Kernighan v Dennis M. Ritchie, The C Programming
Language (2nd Edition), Prentice Hall PTR, 1988
[2] K. N. King, C Programming: A Modern Approach, 2nd Edition,
Norton, 2008
[3] Stephen Prata, C Primer Plus, Sams, 2004
[4] Peter van der Linden, Expert C Programming, Prentice Hall PTR 1994
http://www.cprogramming.com/tutorial.html
http://www.java2s.com/Tutorial/C/CatalogC.htm
Phn mm
Visual Studio
Code::Blocks