em đang học kỹ thuật lập trình. và phải lập trình bài toán sắp xếp một mảng theo thứ tự tăng dần bằng hàm, ra vào với file. anh chị nào code được code giùm em được không ạ
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void CreateArray(int ,int );
void Sort(int , int );
void CreateFile(int , int);
void Swap(int *, int *);
int main()
{
int n;
scanf("%d",&n);
int a[n];
CreateArray(a,n);
Sort(a,n);
CreateFile(a,n);
}
void CreateArray(int a[],int n)
{
for(int i = 0 ; i < n ; i ++)
a[i] = 1 + rand()%100;
}
void Sort(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])
Swap(&a[i],&a[j]);
}
void Swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void CreateFile(int , int)
{
FILE *f = fopen("TEST.TXT","w+t");
for(int i = 0 ; i < n ; i ++)
fprintf(f,"%d ",a[i]);
fclose(f);
}
Bạn Tham Khảo nha, hôm bữa k onl k thấy bài của bạn nên up trể tí
Tạo mảng gôm n phần tử sắp xếp xong in ra file
CODE
[AH][/AH]Mã:#include<stdio.h> #include<stdlib.h> #include<time.h> void CreateArray(int ,int ); void Sort(int , int ); void CreateFile(int , int); void Swap(int *, int *); int main() { int n; scanf("%d",&n); int a[n]; CreateArray(a,n); Sort(a,n); CreateFile(a,n); } void CreateArray(int a[],int n) { for(int i = 0 ; i < n ; i ++) a[i] = 1 + rand()%100; } void Sort(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]) Swap(&a[i],&a[j]); } void Swap(int *a, int *b) { int tmp = *a; *a = *b; *b = tmp; } void CreateFile(int , int) { FILE *f = fopen("TEST.TXT","w+t"); for(int i = 0 ; i < n ; i ++) fprintf(f,"%d ",a[i]); fclose(f); }
File ra ko có à .máy em nó còn không hiện ra file vào ấy ạ
trong thư mục ko có file Test.txt
fout=fopen("output.txt","wt");làm như thế cũng ko có số 5 ạ. đấy là em cho thử vào thôi. thực ra chỗ 5 đó là max(x,y) ạ
fout=fopen("output.txt","wt");
sửa lại thành
fout=fopen("output.txt","w+t");
FILE *fout;
fout=fopen("output.txt","w+t");
fprintf(fout,"%d",5);
fclose(fout);
dạ em cảm ơn anh ạ . cuối cùng thì em đã sửa được rồi ạ