Está en la página 1de 7

BI TP 2 Bi 1: Cho bit kt qu in ra mn hnh ca cc chng trnh sau:

#include <stdio.h> void main() { int a=1, b=2; float c=3, d=4; char e=65; b = ++a; c += d--; ++b -= c; printf("a=%d \nb=%ld \nc=%.3f \nd=%.1f \ne=%d (%c)",a,b,c,d,e,e); } #include <stdio.h> void main() { int a=38, b=10, dem=0, ketqua; while (a * b !=0) { if (a >= b) a = a % b; else b = b % a; dem += 1; } ketqua = a + b; printf("dem=%d, a=%d, b=%d, kq=%d", dem, a, b, ketqua); } #include <stdio.h> void main() { int i,n =10, temp; int mang[10]={1,9,2,8,3,7,4,6,5,0}; unsigned char flag=1; while (flag==1) { flag = 0 ; for (i=1; i<n-1; i++) if (mang[i] > mang[i+1]) { temp = mang[i]; mang[i] = mang[i+1]; mang[i+1] = temp; flag=1; } } for (i=0; i<n; i++) printf ("%3d, ", mang[i]); }

#include <stdio.h> void main() { int j, k=0, m, n=10, x=7; int mang[10]={1,3,4,7,8,12,22,24,30,50}; unsigned char found=0; m=n-1; while (k<=m && found==0) { j=(k+m) /2; if (mang[j]==x) found=1; else if (x>mang[j]) k=j+1; else m=j-1; } k = (found==1 ? j : -1) ; printf("%2d", k); }

Bi 2: Cho bit kt qu in ra mn hnh ca chng trnh sau:


#include <stdio.h> #include <conio.h> #include <math.h> int NT(unsigned int { unsigned int i, for (i=2; i<=k; if (n%i==0) return(1); } n) k=(unsigned int)sqrt(n); i++) return(0);

int main() { unsigned int n=24; int i=2, kt=0; printf("\n%d= ",n); do { if (NT(n)) { printf("%d", n); kt = 1; } else if (n%i==0) { printf("%d*",i); n /= i; } else i++; } while (!(kt)); getch(); }

Bi 3: S dng con tr khai bo mt mng n s nguyn. Vi n v cc phn t ca mng c cho trong file Mang1.in. File Mang1.in c ni dung nh sau: - Dng u tin ghi s n - Dng th 2 ghi n phn t ca mng, mi phn t cch nhau bi 1 vi khong trng - Dng cui cng ghi gi tr x l gi tr cn tm kim. Yu cu c file ly d liu vo mng, sp xp mng theo th t tng dn ri lu mng sp xp vo file Mang1.out. Sau thc hin tm kim gi tr x trong mng. Nu tm thy th ghi cu "Tim thay tai vi tri " vo file Mang1.out, nu khng tm thy th ghi cu Khong tim thay.

#include <stdio.h> #include <conio.h> #include <stdlib.h> void sapxep(int *p, int n) { int tmp; for (int i=0; i<(n-1); i++) for (int j=(n-1); j>i; j--) if (*(p+i)>*(p+j)) { tmp=*(p+i); *(p+i)=*(p+j); *(p+j)=tmp; } } int timkiem(int *p, int x, int n) { int d=0, c=n, i; while (d<=c) { i=(d+c)/2; if (x<*(p+i)) c=i-1; else if (x>*(p+i)) d=i+1; else return i; }; return -1; } int main() {

FILE *fptr; int n,i,x; if ((fptr = fopen("Mang1.in", "r")) == NULL) { printf("Loi mo file"); getch(); return 0; } fscanf(fptr,"%d\n",&n); int *p=(int *) malloc(n*sizeof(int)); i=0; while (i<n) { fscanf(fptr,"%d",p+i); i++; } fscanf(fptr,"%d",&x); fclose(fptr); if ((fptr = fopen("Mang1.out", "w")) == NULL) { printf("Loi tao file"); getch(); return 0; } sapxep(p,n); for (i=0; i<n; i++) { fprintf(fptr,"%d ", *(p+i)); } fprintf(fptr,"\n"); i= timkiem(p,x,n); if (i>=0) fprintf(fptr,"Tim thay %d tai vi tri %d ", x,i); else fprintf(fptr,"Khong tim thay %d",x); fclose(fptr); free(p); }

Bi 4: Vit chng trnh tnh ma trn tng C = A+ B, bit C[i,j] = A[i,j] + B[i,j] , i,j [1,m] Trong , A v B l 2 ma trn vung nxn, mi phn t l s nguyn, c cho trong file Matran.in nh sau : - Dng u tin ghi s n - 2n dng tip theo ghi ln lt cc dng ca ma A v ma trn B (theo th t). Mi dng c n s l gi tr cc phn t ca mi dng, cc s cch nhau bi 1 vo khong trng. Ghi ma trn kt qu C vo file Matran.out

#include <stdio.h> #include <conio.h> #include <stdlib.h> int main() { FILE *fptr; int n; if ((fptr = fopen("Matran.in", "r")) == NULL) { printf("Loi mo file"); getch(); return 0; } fscanf(fptr,"%d",&n); int **A = (int **)malloc(n * for (int i = 0; i < n; i++) *(A+i) = (int *)malloc(n int **B = (int **)malloc(n * for (int i = 0; i < n; i++) *(B+i) = (int *)malloc(n int **C = (int **)malloc(n * for (int i = 0; i < n; i++) *(C+i) = (int *)malloc(n sizeof(int *)); * sizeof(int)); sizeof(int *)); * sizeof(int)); sizeof(int *)); * sizeof(int));

for (int i=0; i<n; i++) for (int j=0; j<n; j++) fscanf(fptr,"%d",(*(A+i)+j)); for (int i=0; i<n; i++) for (int j=0; j<n; j++) fscanf(fptr,"%d",(*(B+i)+j)); fclose(fptr);

for (int i=0; i<n; i++) for (int j=0; j<n; j++) *(*(C+i)+j)=*(*(A+i)+j) + *(*(B+i)+j); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) printf("%2d ",*(*(A+i)+j)); printf("\n"); } printf("\n"); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) printf("%2d ",*(*(B+i)+j)); printf("\n"); } if ((fptr = fopen("Matran.out", "w")) == NULL) { printf("Loi tao file"); getch(); return 0; } printf("\n"); for (int i=0; i<n; i++) { for (int j=0; j<n; j++) { printf("%2d ",*(*(C+i)+j)); fprintf(fptr,"%2d ",*(*(C+i)+j)); } printf("\n"); fprintf(fptr,"\n"); } fclose(fptr); getch(); for (int i = 0; i < n; i++) { free(A+i); free(B+i); free(C+i); } free(A); free(B); free(C); }

Bi 5: Vit chng trnh nhp mt danh sch lin kt cha danh sch sinh vin, mi sinh vin c cc thng tin MSSV (int), HoTen (char[50]), Diem (float). Sau thc hin cc yu cu sau: 1. Lit k danh sch sinh vin ra mn hnh, 2. m tng s sinh vin c trong danh sch (n), 3. Thm mt sinh vin c ni dung (M s SV, H tn, im) vo danh sch ti v tr th i (nu i<=0 th thm vo u danh sch, nu i>n th thm vo cui danh sch),

4. Sp xp danh sch theo th t tng dn ca m sinh vin, 5. Tm v in ra mn hnh thng tin v sinh vin c m s = x, x c nhp t bn phm, 6. Loi b sinh vin c m s = y, y c nhp t bn phm, 7. Tch danh sch sinh vin trn thnh 2 danh sch: Danh sch th nht cha cc sinh vin c im >=5, danh sch cn li cha cc sinh vin c im <5 Bi 6: Cho dy s sau

20 11

30

18

19

14

17

16

15

8. Hy khai bo mt cy tm kim nh phn vi cc nt c kha l cc gi tr ca dy s trn (cc nt khng cn trng info). Con tr qun l cy ny c t tn l T. 9. Hy v cy T cu 1 (bng th) sau khi thm cc gi tr kha theo th t nh trn 10. V li cy T sau khi: a. Xa nt c kha = 3, sau in kt qu duyt cy theo phng php NLR b. Tip tc xa nt c kha = 11, sau in kt qu duyt cy theo phng php LNR c. Tip tc xa nt c kha = 8, sau in kt qu duyt cy theo phng php LRN