Professional Documents
Culture Documents
Giải-bài-tập-ck-CBKHMT
Giải-bài-tập-ck-CBKHMT
Giải-bài-tập-ck-CBKHMT
}
Bài 2. Tìm nghiệm của phương trình ax2 + bx +c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
main()
{
float a,b,c,x1,x2,dd;
scanf("%f%f%f",&a,&b,&c);
dd=b*b-4*a*c;
if(a==0)
{
printf("phuong trinh bac mot\n");exit(1);
}
if(dd>0)
{
x1=(b-sqrt(dd))/2/a;
x2=(b+sqrt(dd))/2/a;
printf( "nghiem thuc la %f ”,x1 );
}
else
{
printf("phuong trinh co nghiem phuc\n");
printf("%.2f+i%.2f\n",b/2/a,sqrt(-dd)/2/a);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
printf("%.2f-i%.2f\n",b/2/a,sqrt(-dd)/2/a);
}
}
Bài 3. Tính tổng cosx + cos2x + … +cos100x
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
main()
{ int i; float s,x;
scanf("%f",&x);
s=0;
for(i=1;i<=100; i++)s+=cos(x*i);
printf("i= %d S= %f \n",i,s);
printf(" sinx = %f",sin(x));
}
Bài 4. Giải phương trình x5 + ax2 + b = 0 bằng phương pháp lặp.
#include <stdio.h>
#include <math.h>
#include<stdlib.h>
#define NITER 30
int main()
{ double a,b,x,x0,d,dx;
int count=0;
scanf("%lf", &a);
scanf("%lf",&b);
scanf("%lf",&x);
x0=x;
do
{d=b-a*x*x;
if(d<0)
{printf("error");
exit(1);
}
x=pow(d,0.2);
x0=x; dx=fabs((x-x0)/x);
printf("count= %d x0=%.4f dx= %.4f \n", count++,x0,dx);
}
while (count< NITER);
return 0;
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
HÀM SỐ
Bài 5. Tính giai thừa chương trình có hai hàm: hàm factorial() và hàm main()
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double factorial( int n)
{ int count;
double fact =1;
for(count=n; count > 0 ; --count) fact*=(double)count;
return fact;
}
int main ()
{ int i;
for(i=10 ; i <= 20; i++) printf ("i= %d factorial(%d)= %12.3e \n",i,i,factorial(i));
return 0;
}
Bài 6. Tính sin(x)n dùng hàm fun1 và fun2
//sin(x)^n
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double fun1(double, int);
double fun2(double, int);
int main ()
{
int i; double x=2.1;
for(i=1; i<= 5; i++) printf("i=%3d fun1=%12.3e fun2= %12.3e
\n",i,fun1(x,i),fun2(x,i));
return 0;
}
double fun1(double x, int n)
{ int i; double fact = 1;
for(i=1; i<= n; i++) fact*=sin(x);
return fact;
}
double fun2( double x, int n)
{ if ( n== 0) return 1;
else return sin(x)*fun2(x,n-1);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
}
Bài 7. Đọc n số nguyên từ file data.dat. Xác định tổng n số, số lớn nhất và số bé
nhất. In kết quả ra màn hình. Chương trình gồm hai hàm calculate() và main().
Hàm calculate() đọc dữ liệu từ data.dat và tính tổng, max và min.
//C:\\Users\\ADMIN\\Desktop\\bai7.txt
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void calculate( long k)
{
long i,t,tmax,tmin,s; FILE *p;
p=fopen("C:\\Users\\ADMIN\\Desktop\\bai7lol.txt","r +");
s=0;
for(i=0;i< k; i ++)
{
fscanf(p,"%ld",&t);
if ( t==0) tmax=tmin=t;
s+=t; if (t> tmax) tmax=t;
if(t < tmin) tmin=t;
}
printf(" tmax =%d tmin=%d s=%d \n",tmax,tmin,s);
fclose(p);
}
main()
{
calculate(10);
printf("main\n");
return 0;
}
Bài 8 Tìm nghiệm phương trình x5-ex+5=0 trong khoảng [2, 3] bằng phương
pháp vét cạn.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double fun(double x)
{ return x*x - exp(x)+5;
}
void solve(double xo, long n)
{ long i; double dx,x,r,rmin;
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
dx=0.001;
for(i=0; i < n; i++)
{
r = fun(xo+i*dx);
if (i%100==0) printf("x%6.2f r=%6.2f \n",xo+i*dx,r);
if( i== 0) { rmin=r;x=xo;}
if(fabs(r)<rmin) { rmin=fabs(r); x=xo+i*dx;}
}
printf("x=%6.4f fun= %8.6f \n",x,fun(x));
}
main ()
{solve(2,1000);
printf("main\n");
}
Bài 9 Tìm nghiệm phương trình x5-ex+5=0 trong khoảng [2, 3] bằng
phương pháp chia đôi.
#include <stdio.h>
#include<stdlib.h>
#include<math.h>
double fun(double x)
{ return x*x -exp(x)+ 5;
}
void solve(double a, double b)
{ long i; double f1,f2,f12,x;
f1=fun(a);f2=fun(b);i=0;
while (i<100)
{
x=(a+b)/2;f12=fun(x);
if (f12*f1 > 0) a=x; else b=x;
i++;
}
printf("x=%6.4f fun=%f \n",x,fun(x));
}
main()
{ solve(2,3);
printf("main\n");
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
Bài 10 Tính tích phân của hàm f(x) với khoảng [a,b] bằng phương pháp Ole và
Simpson
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double fun(double x)
{ return x*x + x+1;
}
double integrate(double a, double b)
{
long i,n; double dx,x,f;
f=0; n=100;dx=(b-a)/n;
for(i=0; i < n+1; i++)
{ x= a+i*dx;
if(i==0 || i== n) f+=fun(x)/2;
else f+=fun(x); }
return f*dx;
}
double integrate1(double a, double b)
{ long i,n; double f,dx,x;
n=100;dx= (b-a)/n;
f=fun(a)+ 4*fun(a+dx/2);
for(i=1;i< n+1; i++)
{
x=a+i*dx;
if(i==n) f+=fun(x);
else
f+=fun(x) +fun(x+dx) + 4*fun(x+dx/2);
}
return f*dx/6;
}
main()
{
double f1,f2,f3,a,b;
a=0;b=1;f1=(b*b*b/3+b*b/2+b)-(a*a*a/3+a*a/2+a);
f2=integrate(a,b);f3=integrate1(a,b);
printf("f1=%8.6f f2=%8.6f f3=%8.6f\n",f1,f2,f3);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
CON TRỎ
Bài 10 In địa chỉ, giá trị của biến con trỏ.
#include<stdio.h>
void main()
{
int u=5,v;
int *pu,*pv;
pu=&u;
v=*pu;
pv=&v;
printf("\n u= %d &u= %x pu=%x *pu = %d",u,&u,pu,*pu);
printf("\n v= %d &v=%x *pv= %d \n",v,&v,pv,*pv);
}
Bài 10a Dùng hàm với đối con trỏ để trao đổi giá trị của hai biến đơn.
#include <stdio.h>
#include <stdlib.h>
main()
{
double a,b;
scanf("%lf",&a);
scanf("%lf",&b);
swap(a,b);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
}
}
Bài 11a Chương trình dùng tên hàm như đối của một hàm khác.
#include<stdio.h>
void cube(double(*)(double), double, double *);
double fun1(double);
double fun2(double);
main ()
{ double x,res1,res2;
printf("\n x="); scanf("%lf",&x);
cube (fun1,x,&res1);
cube(fun2,x,&res2);
printf("\n x=%8.4f res1= %8.4f res2 = %8.4f \n", x,res1,res2);
}
void cube(double(*fun)(double), double x,double *result)
{ double y;
y=(*fun)(x);
*result= y*y*y;
}
double fun1 (double z)
{ return 3.0*z*z - z;
}
double fun2(double z)
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
Bài 12a Chương trình tính giá trị trung bình và sai phân của ba số.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double a,b,c,x,y;
void calculate()
{
x=(a+b+c)/3;
y=(abs(a-x)+abs(b-x)+abs(c-x))/3;
}
main()
{
scanf("%lf%lf%lf",&a,&b,&c);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
calculate();
printf("x=%f y=%f \n",x,y);
}
MẢNG
Bài 13 Chương trình tính giá trị trung bình và sai phân của các số.
#include <stdio.h>
#include <stdlib.h>
#define nmax 100
main()
{ int n, count;
double sum = 0,d,avg;
double list[nmax];
scanf("%d",&n);
for( count=0; count < n; ++ count)
{ printf("i = %d x=",count+1);
scanf("%lf",&list[count]);
sum+=list[count];
}
avg= sum/(double)n;
printf("\n avg = %5.2f \n",avg);
for( count=0; count < n; ++ count)
{ d= list[count]- avg;
printf("i= %d x= %5.2f d= %5.2f \n ",count+1,list[count],d);
}
}
Bài số 14 In giá trị của giai thừa từ 10 đến 20 trao đổi mảng thông qua con trỏ.
#include <stdio.h>
void factorial(double []);
main()
{ int i;
double fact[21];
factorial(fact);
for(i=10; i<= 20; i++)
printf("i= %d factorial(%d) = %e\n", i,i,fact[i]);
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
}
Bài số 14a Tính tổng các phần tử của matrix sử dụng hàm sum với con trỏ.
/*chương trình tính tổng các phần tử đường chéo của ma trận*/
#include <stdio.h>
void sum(int (*y)[3], int *z, int n)
{
int i, *p;
p=&y[0][0];*z=0;
for(i=0; i<n; i++)*z+=*(p+i*n+i);
}
//********************************************************
main()
{
int x[3][3]=
{
{1,2,3},{11,6,9},{3,7,1},
};
int z;
#include <stdio.h>
#include <stdlib.h>
//*************************************************************
int strlen1(char *s) //Cach1
{
long n;
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
for(n=0; *s!='\0';s++)n++;
return n;
}
//*************************************************************
int strlen2(char *s)//Cach2
{
char *p=s;
while(*p!='\0')p++;
return p-s;
}
main()
{
#include <stdio.h>
#include <stdlib.h>
while((s[i]=t[i])!='\0')i++;
}
t="hello";
strcpy4(s, t);printf("%s\n",s);
printf("main\n");
}
Bài số 15b so sánh hai xâu, strcmp.c
// strcmp1 bài tập so sánh hai xâu s và t, strcmp.c
#include <stdio.h>
#include <stdlib.h>
main()
{
char *s, *t; long k;
t="hellox"; s="hello";
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
qsort(v,0,9);
for(i=0;i<10;i++)printf("i,v %6d %6d\n",i,v[i]);
printf("main\n");
}
void swap(int v[], int i, int j)
{
int temp;
temp=v[i];
v[i]=v[j];
v[j]=temp;
}
void qsort(int v[], int left, int right)
{
int i, last;
if(left >= right)return;
swap(v, left, (left+right)/2);
last=left;
for(i=left+1; i<=right; i++)
if(v[i] < v[left])swap(v, ++last, i);
swap(v, left, last);
qsort(v, left, last-1);
qsort(v, last+1, right);
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
Bài 16a chương trình sắp xếp mảng số nguyên theo thuật toán Hoare (mảng con
trỏ).
// qsort.c phiên bản sử dụng mảng con trỏ
#include <stdio.h>
#include <stdlib.h>
printf("main\n");
}
void swap(int *v[], int i, int j)
{
int *temp;
p=&daytab[0][0];
i=1; j=4; printf("%6d%6d\n", daytab[1][4], *(p+i*13+j));
year=2001;month=3;day=15;day_year=day_of_year(daytab,year,month,day);
printf("day_of_year %6d%6d%6d%6d\n",year,month,day,day_year);
year=2001;day_year=76;month_day(daytab,year,day_year,&month,&day);
printf("month_day %6d%6d%6d%6d\n",year,day_year,day,month,day);
}
Bài 17a chương trình tính thời gian của x4, time.c
//chương trình tính thời gian thực hiện tổng a4 bằng cách nhân trực tiếp hoặc sử dụng
hàm pow(a,4)
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
#include <time.h>
#include <stdio.h>
#include <math.h>
#define n_loop 1000000
main()
{
int i; double a = 11234567890123456.0, b;
clock_t time_1, time_2;
time_1 = clock(); //tính thời gian bắt đầu
for (i = 0; i < n_loop; i++) b = a * a * a * a;
time_2 = clock(); //tính thời gian kết thúc
printf ("cpu time needed to evaluate a*a*a*a: %f microsecs\n",(double)(time_2 -
time_1) /CLOCKS_PER_SEC);
time_1 = clock();
for (i = 0; i < n_loop; i++) b = pow(a, 4.);
time_2 = clock();
printf ("cpu time needed to evaluate pow(a, 4.): %f microsecs\n", (double) (time_2
- time_1) / (double)CLOCKS_PER_SEC);
}
SỐ NGẪU NHIÊN
Bài 18 chương trình tính trung bình và phương sai của 106 số ngẫu nhiên,
random.c
/* tính trung bình và sai phân của 106 số ngẫu nhiên [0, 1] */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#define n_max 10000000
main()
{
int i, seed;
double sum_0, sum_1, mean, var, x;
seed = time(NULL);
srand(seed);
for (i = 1, sum_0 = 0., sum_1 = 0.; i <= n_max; i++)
{
x = (double) rand() / (double) RAND_MAX;
sum_0 += x;
sum_1 += (x - 0.5) * (x - 0.5);
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
//file main.c
#include <stdio.h>
#include <stdlib.h>
void factorial(int, double *);
main()
{
int i; double fact;
for (i = 1; i <6; i++)
{
factorial(i, &fact);
printf("factorial(%d) = %12.3f\n", i, fact);
}
}
#include "factorial.c"
//taofactorial.c
void factorial(int n, double *fact)
{
int count;
for (count = n, *fact = 1; count > 0; --count) *fact*= (double) count;
}
CẤU TRÚC
Bài số 19 chương trình khảo sát dữ liệu kiểu cấu trúc, test_struct.c
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void test_struct1()
{
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
struct point
{ double x; double y;};
struct point pt1={1.2,2.3}, pt2={2.5,3.7};
double rx, ry;
};
#include <stdio.h>
int day_month[13]={0,31,28,31,30,31,30,31,31,30,31,30,32};
typedef struct {int day; int month;} date;
int day_year(date *);
main()
{
date x; int a;
printf("day="); scanf("%d", &a); x.day=a;
printf("month="); scanf("%d",&a); x.month=a;
printf("%d/%d is %d-th day of year\n", x.day, x.month, day_year(&x));
}
int day_year(date *y)
{
int i, day=0;
for(i=1;i<y->month;i++)day+=day_month[i];
return day+y->day;
}
struct treenode
{
int numb, count;
struct treenode *left;
struct treenode *right;
};
typedef struct treenode tnode;
int x[n];
void read_file(int n)
{
int i; FILE *p;
p=fopen("tree.dat","r");
for(i=0; i<n; i++)fscanf(p,"%d",&x[i]);
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362
fclose(p);
}
//************************************************************
**
tnode *talloc()
{
return (tnode*) malloc(sizeof(tnode));
}
tnode *addtree(tnode *p, int q)
{
if(p==NULL)
{
p=talloc();
p->numb=q; p->count=1;
p->left=p->right= NULL;
}
else if(q==p->numb) p->count++;
else if(q<p->numb) p->left=addtree(p->left, q);
else p->right=addtree(p->right, q);
return p;
}
Môn học: Căn bản khoa học máy tính SV: Bùi Xuân Hoàng
GV: TS. Phạm Khắc Hùng MSSV 20196362