0% found this document useful (0 votes)
75 views7 pages

sinh Ma Tran So Roi Ghi Vao File

The document contains code for several programs in C/C++ that perform tasks such as: 1. Generating a random matrix and writing it to a file. 2. Generating random student data including name and age and writing to a file. 3. Reading numbers from a file, checking if they are prime, and printing prime numbers. The code includes functions for generating random matrices, multiplying matrices, sorting arrays, checking for prime numbers, and other tasks.

Uploaded by

KuDuoc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
75 views7 pages

sinh Ma Tran So Roi Ghi Vao File

The document contains code for several programs in C/C++ that perform tasks such as: 1. Generating a random matrix and writing it to a file. 2. Generating random student data including name and age and writing to a file. 3. Reading numbers from a file, checking if they are prime, and printing prime numbers. The code includes functions for generating random matrices, multiplying matrices, sorting arrays, checking for prime numbers, and other tasks.

Uploaded by

KuDuoc
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

//sinh ma tran so roi ghi vao file

#include<alloc.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
//void ktnt(FILE *f);
void main() {
FILE *f;
int **a; // ma tran 2 chieu
int i,j,h,c;
clrscr();
printf("nhap co of ma tran:");
scanf("%d",&h);
scanf("%d",&c);
a=(int**)malloc(h*c*sizeof(int));
randomize();
f=fopen("C:\\input.txt","r");
if (f==NULL) {
printf("file nay khong mo duoc.\nChuong trinh se tao file moi.\n");
fclose(f);
}
for(i=0;i<h;i++)
for(j=0;j<c;j++)
a[i][j]=random(1000);

f=fopen("C:\\input.txt","w");
for(i=0;i<h;i++){
for(j=0;j<c;j++) {
fprintf(f,"%d5",a[i][j]);
}
fprintf(f,"\n");
}
for(i=0;i<h;i++)
for(j=0;j<c;j++)
// ktnt(f);
fclose(f);
free(a);
getch();
}
/*void ktnt(FILE *f) {
int i,k;
f=fopen("C:\\input.txt","r");
if (f==NULL) {
printf("file nay khong mo duoc.\nChuong trinh se tao file moi.\n");
exit(1);
}
fscanf(f,"%d",&k);
printf("so nt la:");
for(i=2;i<sqrt(k);i++){
if ((k%i)==0)
printf("%d ",i);
}
}
*/

//sinh ngau nhien n so nguyen ghi vao file


#pragma -w-;
#include<alloc.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct {
char ten[37];
int tuoi;
}svien;
void tim(FILE *f,svien *sv,char s[]);
void main() {
svien *sv;
FILE *f;
char path[]="C:\\sv.dat";
int i,n;
clrscr();
printf("nhap ? sinh vien:");
scanf("%d",&n);fflush(stdin);
sv=(svien*)malloc(n*sizeof(svien));
randomize();
for(i=0;i<n;i++) {
printf("nhap ten sv thu %d:",i+1);
gets(sv[i].ten);fflush(stdin);
sv[i].tuoi=random(100);fflush(stdin);
}
if ((f=fopen(path,"r"))==NULL) {
fclose(f);
}
f=fopen(path,"a");
for(i=0;i<n;i++)
fprintf(f,"%s %d tuoi.\n",sv[i].ten,sv[i].tuoi);
fclose(f);
free(sv);
}
void tim(FILE *f,svien *sv,char s[37]) {
int i;
int c;
printf("nhap ten sv muon tim:");
gets(s);
c=feof(f);
while(c!=NULL) {
fscanf("%s%d",sv.ten,sv.tuoi);
if (s==sv.ten) printf("%s %d tuoi\n",sv.ten,sv.tuoi);
}

// doc data tu mot file cac so, in ra so nt


#include<alloc.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void kt(int);
void main() {
FILE *f;
int i,j,m,n;
int *a;
char path[]="C:\\input.txt";
clrscr();
f=fopen(path,"r");
if (f==NULL) {
printf("file khong co.");
exit(1);
}
a=malloc(sizeof(f));
i=1;
while(!feof(f)) {
fscanf(f,"%d",&j);
a[i]=j;
kt(a[i]);
i+=1;
}
getch();
}
void kt(int N) {
int i, OK = 1;
for (i=2; i<(int)sqrt(N); i++)
if (N%i == 0)
{
OK = 0;
break;
}
if (OK)
printf("%d ", N);
}

//load data tu mot file


#include<alloc.h>
#include<conio.h>
#include<stdio.h>
void main() {
FILE *f;
void *p;
int i,tam,n,m;
int a[25];
char ch[99];
clrscr();
f=fopen("C:\\bai31.txt","rt");
i=0;
while(!feof(f)) {
fscanf(f,"%d",&tam);
a[i]=tam;
i++;
}
printf("\n\n");
for(n=0;n<i;n++)
printf("%d ",a[n]);
getch();
}

// mang
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void sinhmatran(int **,int,int);
void nhanmatran(int **,int,int,int **,int,int **);
void main() {
int hang,cot,cotb,i,j;
int **a,**b,**c;
clrscr();
printf(" \\\|/// \n");
printf(" \\ - - // \n");
printf(" ( .@ @. ) \n");
printf(" (_) \n");
printf("nhap so hang of mang a:");
scanf("%d",&hang);
printf("nhap so cot of mang a:");
scanf("%d",&cot);
printf("nhap so cot of mang b:");
scanf("%d",&cotb);

sinhmatran(a,hang,cot);
sinhmatran(b,hang,cotb);
nhanmatran(a,hang,cot,b,cotb,c);
for(i=0;i<hang;i++){
for(j=0;j<cot;j++)
printf("%4d",a[i][j]);
printf("\n");
}
printf("\n\n\n");
for(i=0;i<hang;i++){
for(j=0;j<cotb;j++)
printf("%4d",b[i][j]);
printf("\n");
}

getch();
}
void sinhmatran(int **a,int hang,int cot) {
int i,j;
a=(int **)malloc(hang*cot*sizeof(int));
randomize();
for(i=0;i<hang;i++)
for(j=0;j<cot;j++)
a[i][j]=random(101);
}
void nhanmatran(int **a,int hang,int cot,int **b,int cotb,int **c) {
int i,j,k;
for(i=0;i<hang;i++)
for(j=0;j<cot;j++) {
c[i][j]=0.0;
for(k=0;k<cotb;k++)
c[i][j]=a[i][k]*b[k][j];
}
}

SINH VIEN
#include <conio.h>
#include<stdio.h>
#include<alloc.h>
void sx(float *a,int n);
typedef struct {
char ten[31];
int diemp;
int diemc;
} sinhvien;
void main() {
sinhvien *sv;
int i,n,j;
float diemtb,*mdiemtb;
clrscr();
printf("nhap so sinh vien:");
scanf("%d",&n);
fflush(stdin);
// while(getchar() != '\n');
sv=(sinhvien *)malloc(n*sizeof(sinhvien));
for(i=0;i<n;i++) {
printf(" ten thang thu %d:",i+1);
gets(sv[i].ten);
printf(" diem pascal:");
scanf("%d",&sv[i].diemp);
printf(" diem c:");
scanf("%d",&sv[i].diemc);
fflush(stdin);
// while(getchar() != '\n'); // tong \n ra khoi stream
}
mdiemtb=(float *)malloc(n*sizeof(float));
for(i=0;i<n;i++) {
mdiemtb[i]=(float)(sv[i].diemc*2+sv[i].diemp)/3;
sx(mdiemtb,n);
// printf("%s %d %d %0.2f \n",sv[i].ten,sv[i].diemp,sv[i].diemc,mdiemtb[i]);
getch();
}
void sx(float *a,int n) {
int i,j;
float tam;
for(i=0;i<n;i++)
for(j=0;i<n;j++) {
if (a[i]<a[j]) {
tam=a[i];
a[i]=a[j];
a[j]=tam;
}
}
printf("sau khi sap la:");
for(i=0;i<n;i++)
printf("%3.3f",a[i]);
}

/* Bai tap - Chuong trinh thong tin co quan */


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

void main()
int hovaten, chucvu, luong, thuong, thunhap, nopthue;
{
cout<<"Nhap Ho Va Ten"\n;
cin>>(hovaten)\n;
cout<<"Chuc Vu"\n;
cin>>(chucvu)\n;
cout<<"Luong"\n;
cin>>(luong)\n;
cout<<"Thuong"\n;
cin>>(thuong)\n
if (thunhap=luong+thuong==5000000);
cout<<"Thuc lanh:" thunhap;
else cout<<"Thuc lanh:" thunhap*10/100;

getchar();
return 0;
}

// tfim all cac uoc so nt of so N


#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int kt(int);
void main() {
int i,n;
// int *a;
printf("nhap N:");
scanf("%d",&n);
// a=(int*)malloc(n*sizeof(int));// mang chua uoc so
// j=0;
for(i=2;i<n;i++)
if (n%i==0) {
if (kt(i)==0)
printf("%d ",i);
}
getch();
}
int kt(int a) {
int i;
for(i=2;i<a;i++)
if (a%i==0)
return 1;
return 0;
}

// tinh tong hai so of mot so co 2 chu so


#include<conio.h>
#include<stdio.h>
void main() {
int a,b,n,i;
printf("nhap so");
scanf("%d",&n);
a=n/10+n%10;
printf("tong:%d",a);
getch();
}

You might also like