K
Biên
www.hutech.edu.vn
*1.2021.CMP164*
-mail
:
I
...................................................................................................................I
.......................................................................................................... IV
............................................................................................................... 1
......................................................................................................... 1
.............................................................................................. 1
1.2.1 Khai báo ........................................................................................................ 1
.................................................................. 3
..................................................................... 4
...................................................................... 4
............................................................. 5
.......................................................... 7
.................................................... 7
1.3.1 Cách kha
......................................................................................... 8
....................................................................................... 8
............................................................................................. 9
............................................................................................... 9
1.4.1 Khai báo ........................................................................................................ 9
................................................................ 10
..................................................................... 11
..................................................................... 13
............................................................ 13
.................................................................................................... 15
................................................................................................ 17
.................................................... 17
2.2 KHAI BÁO VÀ
............................................................ 18
..................................................................................... 18
................................................................................ 18
....................................................................... 21
2.3.1 Phép gán...................................................................................................... 21
................................................................................... 22
................................................................................... 22
2.3.4 Phép so sánh ................................................................................................ 23
2
......................... 23
........................................................................... 24
................................................................. 26
...................................................................................... 27
.............................................................................................. 27
........................................................................ 28
II
...................................................28
he
..................29
..........................................................................................30
........................................................................ 32
..........................................................................................32
2.6.2 Cách 1 ..........................................................................................................32
2.6.3 Cách 2 ..........................................................................................................34
2.7
............................................................................... 36
.......................................................................................36
............................................................................36
........................................................37
...................................... 44
2.8.1
..........................................................................................................44
..........................................................................................................44
.............................................................46
................................................................................................................ 48
.................................................................................................... 48
........................................................................................................... 52
....................................................................................................... 52
......................................................... 53
................................................................. 54
................................................................ 54
.................................................................................. 55
..........................................................................................55
............................................................................................55
............................................................................................56
............................................................................................57
.................................................................................................... 58
............................................................................................... 59
4.
....................................................................................................... 59
.......................................................................... 60
......................................................................................60
.....................................................................................................61
..................................................................................................62
..................................................................62
- Hàm rewind() ......................................62
............................................................................ 63
.........................................................................63
..........................................................................64
.......................................................................... 66
- Hàm fwrite() ...................................................66
III
- Hàm fread() ................................................... 66
.................................................................................................... 71
BÀI 5: TÌM KI M ....................................................................................................... 72
5.1 GI I THI U V
B I TO N T M KI M ................................................................ 72
5.2 T M KI M TUY N T NH..................................................................................... 73
........................................................................................ 74
.................................................................................................... 76
P ......................................................................................................... 77
6.1 GI I THI U V
B I TO N S P X P .................................................................. 77
..................................................................................................... 77
6.1.2
........................................................................................................... 77
PH C T P C A GI I THU T........................................................................ 78
........................................................... 78
................................................................ 79
................................................................................ 80
6.
t.................................................................................... 80
............................................................................ 82
................................................................................ 83
................................................................................. 85
t ...................................................................................... 87
..................................................................................... 89
.................................................................................... 91
..................................................................................... 93
.................................................................................................... 95
............................................................................................ 96
IV
M
C
C/C++. Ngoài ra,
h
-
,c
-
Hi
.
-
.
-
và nâng cao.
-
B i 1:
-
B i 2:
-
B i 3:
-
B i 4:
-
B i 5:
-
B i 6:
C
t v logic v ki n th c to n h
n.
V
L
gi p sinh viên l m quen v
p l p tr
trên m y t nh
gi p sinh viên c
c ch ti p c n v gi i quy t c c b i to
c n v i c
C, là môn h
i l p tr nh, v
kh i ni m
n v
, gi p sinh viên c kh
l
p
ti p c n v i c c h c ph n
quan tr ng c n l i c a ng nh Công ngh Thông tin. V v y, yêu c
u t i li
n l p và g ch chân nh ng v
1.
2.
khơng hi
trình (ch
c t i li
nl
)
-
c khi
i.
1
BÀI 1:
BÀI 1:
-
-
c
1.1 KHÁI NI M
khi ta
.
1.2 M NG M T CHI U
1.2.1 Khai báo
Kha
Cú pháp:
Trong
-
:
2
BÀI 1:
-
-
songuyen
int songuyen [10];
sothuc
float sothuc [15];
K
daykytu
char daykytu [30];
Cú pháp:
Cách
Cú pháp:
float x[] = {12.1, 7.23, 5.0, 27.6, 87.9, 9.31};
sizeof()
=
nhapmang (int a[ ], int n);
BÀI 1:
1.2.2 Truy c p vào các ph n t
c a m ng
M
theo sau là
Ch
Ví d :
:
float a [5];
:
a[0], a[1], a[2], a[3], a
float t = 10.0;
int i = 1;
a[0] = 4.2;
a[2] = t;
a[i] = (a[0] + a[2]) / 2;
printf("\nGia tri: %f ", a[1]);
scanf("%f", &a[4]);
t = a[4];
a[2 * i + 1] = a[2 * i] + a [2 * i + 2];
float
3
4
BÀI 1:
float x[6] = {12.1, 7.23, 5.0, 27.6, 87.9, 9.31};
x[1]=7.23, x[2]=5.0, x[3]=27.6, x[4]=87.9, x[5]=9.31
1.2.3 Nh p d
li u cho m ng m t chi u
K
int a [100]
:
0
1
2
3
4
....
n-3
n-2
n-1
7
3
9
4
5
....
8
12
2
a[4]
....
a[n-3]
a[n-2]
a[n-1]
a[0]
a[1]
a[2]
a[3]
:
a
a
a[ 2 ] = 9
scanf (
...................................................
a[ nVí
-1 ]);
: Hàm
void NhapMang (int a[], int n){
for (int i = 0 ; i < n ; i++){
printf (
scanf (
:
]);
}
}
1.2.4 Xu t d
K
li u cho m ng m t chi u
uyên:
5
BÀI 1:
int a[100];
s
:
0
1
2
3
4
....
n-3
n-2
n-1
7
3
9
4
5
....
8
12
2
a[4]
....
a[n-3]
a[n-2]
a[n-1]
a[0]
a[1]
a[2]
a[3]
:
a [2] = 9
print
...................................................
a [n-1] = 2
-1]);
:
void XuatMang (int a[], int n){
for (int i =0 ; i
]) ;
}
}
1.2.5 M t vài thu t toán trên m ng m t chi u
Bài toán 1:
:
]
phát 200 bytes
6
BÀI 1:
0
1
2
3
4
....
n-3
n-2
n-1
7
3
9
4
5
....
8
12
2
a[4]
....
a[n-3]
a[n-2]
a[n-1]
a[0]
a[1]
a[2]
a[3]
S
- 1]
ng
for (int i= 0 ; i
S= S + a[i] ;
long TinhTong (int a[ ], int n) {
long s = 0;
for (int i = 0; i < n; i++)
s = s + a[i];
return s;
}
Bài toán 2
a[ ]
0
1
2
3
4
....
n-3
n-2
n-1
7
3
-9
4
5
....
8
12
2
it
for (int i= 0 ; i
if (a[i] < 0)
BÀI 1:
7
.
return a[i];
int AmDau (int a[ ], int n) {
for (int i = 0; i < n; i++)
if (a[i] < 0)
return a[i];
return 1;
}
1.2.6 Truy n tham s
m ng m t chi u cho hàm
T
:
long TinhTong (int a[ ], int n);
Hàm tính
x
int TimX (int a[ ], int n, int x);
1.3 CHU I KÝ T
(M NG M T CHI U CÁC KÝ T )
d
\
t
Chú ý
.
ng có
8
BÀI 1:
1.3.1 Cách khai báo chu i
Khai
char
char Hoten [20];
khai báo
V
char
[
char chuoi[50]
char name []
NG
\
char ten[10] =
\
khi
\
1.3.2 L i khi t o m t chu i
1. Chú ý: Không
này sang
:
char a[4] =
char b[4];
b = a;
/
9
BÀI 1:
2. Không
==, !=, <, >
.
:
if(a==b) // Không so sánh
{
}
1.3.3 Nh p, xu t chu i
N
Cách 1:
-
char s[30];
Cách 2:
g
phím space, tab, new line, Enter thì
.
Cách 1:
Cách 2:
1.4 M NG HAI CHI U
1.4.1 Khai báo
Cú pháp:
g
10
BÀI 1:
-
:
-
dịng
dịng
nhiêu.
nhiêu.
-
g
là bao nhiêu (hay nói cách khác,
.
.
-
Khai
int a [8][14];
Khai
10 hàng, 5
float
float b [10][5];
1.4.2 Truy c p vào các ph n t
c a m ng
M
theo sau là
a[0][0] là
ng có
.
các thao tác
11
BÀI 1:
4 *
, v
0
1
2
3
4
5
6
7
0
[0][0]
[0][1]
[0][2]
[0][3]
[0][4]
[0][5]
[0][6]
[0][7]
1
[1][0]
[1][1]
[1][2]
[1][3]
[1][4]
[1][5]
[1][6]
[1][7]
2
[2][0]
[2][1]
[2][2]
[2][3]
[2][4]
[2][5]
[2][6]
[2][7]
3
[3][0]
[3][1]
[3][2]
[3][3]
[3][4]
[3][5]
[3][6]
[3][7]
1.4.3 Nh p d
li u cho m ng hai chi u
V
uyên,
#define d 30 // d
#define c 30 // c là
int a [d ][c];
d dòng c
m d hàng
:
0
1
2
3
4
5
c-1
0
1
2
3
4
5
6
7
1
10
11
12
13
14
15
17
d-1
20
21
22
23
24
25
27
-
:
a [ 0 ][ 0 ] = 1
scanf (
a[0 ][0]);
a [ 0 ][ 1 ] = 2
scanf (
[0][1]);
a [ 0 ][ 2 ] = 3
scanf (
[0][2]);
...................................................
a [ 0 ][ c-1 ] = 2
scanf (
a[0][c-1 ]);
12
BÀI 1:
p d
for (int j = 0; j < c; j++)
scanf (
;
-
:
a [ 1 ][ 0 ] = 1
scanf (
a [ 1 ][ 1 ] = 2
scanf (
a [ 1 ][ 2 ] = 3
scanf (
[0]);
...................................................
a [ 1 ][ c-1 ] = 2
scanf (
-1 ]);
for (int j = 0; j < c; j++)
scanf (
..................................................................................
-
:
a [ d-1 ][ 0 ] = 1
scanf (
-1 ][0]);
a [ d-1 ][ 1 ] = 2
scanf (
-1 ][1]);
a [ d-1 ][ 2 ] = 3
scanf (
-1 ][2]);
...................................................
a [ d-1 ][ c-1 ] = 2
scanf (
dòng d:
for (int j = 0; j < c; j++)
scanf (
-1][j]) ;
-1][c-1 ]);