C语言自学一月,搞出了读取文件数据,进行平均数和标准偏差计算,再输出的程序。
就像这样。
#include
#include
#include
/*main program*/
int main()
{
float n[831],ave,sd;
int i;
char in_file_name[FILENAME_MAX], out_file_name[FILENAME_MAX];
FILE *inf, outf;
float ave1(int n,float a[]);
float sd1(int n,float b[],float a1);
printf("please enter the input filename: ");
gets(in_file_name);
printf("please enter the output filename: ");
gets(out_file_name);
inf = fopen(in_file_name, "r");
outf = fopen(out_file_name, "w");
for(i=0; i<832 ; i++)
fscanf(inf, "%f", &n[i]);
fprintf(outf, "\n");
ave=(double)ave1(832,n);
sd=(double)(832,n,ave);
fprintf(outf, "the average nymber = %e\n", ave);
fprintf(outf, "the standard deviative = %e\n", sd);
fclose(inf);
fclose(outf);
return 0;
}
/*verage number*/
float ave1(int n, float a[])
{
int k;
float sum1=0,ave2;
for(k=0;k<832;k++)
sum1=sum1+a[k];
ave2=(sum1/n);
return(ave2);
}
/*standard deviative*/
float sd1(int n,float b[], float a1)
{
int k1;
float sum2=0,ave3,sd1;
for(k1=0;k1<832;k1++)
sum2=sum2+pow((b[k1]-a1),2);
ave3=sum2/n;
sd1=(sqrt(ave3/(n-1));
return(sd1);
}
现在问题来了:一份文件当中有数据13列X64行,第一列表示位置(无用),有效数据为12列X64行=768个,每一个数据代表一个位置。现在有192分文件,要计算各个文件当中相同位置数据的平均值和标准偏差。最后输出结果为两个TXT文件,各表示平均数和标准偏差。
有试过二维数组之类的,但失败太多了,为此浪费太多时间了,求大神帮忙。