Wednesday, November 26, 2014

Câu 4.4: Viết chương trình nhập vào từ bàn phím n số nguyên, hãy ghi các số nguyên dương vào một tệp văn bản

chạy xong chương trình vào tệp có địa chỉ c:\\output44.txt xem kết quả bài toán

#include <stdio.h>
#include <conio.h>
void ghitep(int a[],int n,FILE *l)
{
      int i;
      for(i=0;i<n;i++) if(a[i]>0) fprintf(l,"%d",a[i]);
}
voidxemmang(int a[],intn)
{
      int i;
      printf("\n");
      for (i=0;i<n;i++)
            printf("  %5d  ",a[i]);
      printf("\n");
}
voidnhapdulieu(int a[],intn)
{
      int i;
      for (i=0;i<n;i++)
      {
            printf("\nNhap phan tu thu [ %d ]   :  ",i);
            scanf("%d",&a[i]);
      }
}
void main()
{
      int a[1000],n;
      FILE *u;
      u=fopen("c:\\output44.txt","wt");
      printf("\nNhap so phan tu  n =     ");
      scanf("%d",&n);
      nhapdulieu(a,n);
      xemmang(a,n);
      ghitep(a,n,u);
      fclose(u);
      printf("\nHoan tat");
      getch();

}

No comments:

Post a Comment