Wednesday, November 26, 2014

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

dữ liệu vừa nhập sẽ lưu vào file có địa chỉ  C:\\outputbai41.txt
//code Dung NV
#include <stdio.h>
#include <conio.h>
void nhap(floata[],int n)
{
                int i;
                for (i=0;i<n;i++)
                {
                                printf("\nNhap phan tu thu   %d   :    ",i);
                                scanf("%f",&a[i]);
                }
}
void ghitep(floata[],int n,FILE *v)
{
                int i;
                for (i=0;i<n;i++)
                                if (a[i]>0)
                                                fprintf(v,"   %5.2f   ",a[i]);
}
void main()
{
                int n;
                float a[100];
                FILE *u;
                u=fopen("c:\\outputbai41.txt","wt");
                printf("\nNhap so phan tu: n=    ");
                scanf("%d",&n);
                nhap(a,n);
                ghitep(a,n,u);
                fclose(u);
                printf("\nhoan tat");
                getch();
}

No comments:

Post a Comment