ReadDat( )實(shí)現(xiàn)從文件FC.IN中讀取1000個(gè)十進(jìn)制
整數(shù)到數(shù)組xx中; 請(qǐng)編制函數(shù)Compute()分別計(jì)算出xx中奇數(shù)的個(gè)
數(shù)odd, 偶數(shù)的個(gè)數(shù)even, 平均值aver以及方差totfc的值, 最后調(diào)
用函數(shù)WriteDat()把結(jié)果輸出到FC1.OUT文件中。
計(jì)算方差的公式如下:
1 N
totfc = ── ∑ (xx - aver)^2
N i=1
原始數(shù)據(jù)文件存放的格式是: 每行存放10個(gè)數(shù), 并用逗號(hào)隔
開。(每個(gè)數(shù)均大于0且小于等于2000)
注意: 部分源程序存放在PROG1.C中。
請(qǐng)勿改動(dòng)主函數(shù)main()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
/*參考答案*/
#include
#include
#include
#define MAX 1000
int xx[MAX], odd = 0, even = 0 ;
double aver = 0.0 , totfc = 0.0 ;
void WriteDat(void) ;
int ReadDat(void)
{
FILE *fp ;
int i,j;
char c,str[20];
if((fp = fopen("FC.IN", "r")) == NULL) return 1 ;
/***********讀入數(shù)據(jù)并存放到數(shù)組xx中*************/
for(i = 0; i < MAX; i++)
{
j = 0;
while((c = (char) fgetc(fp)) != EOF)
{
if(c == ’,’)
{
str[j] = ’\0’;
break;
}
else if(c != ’\n’ && c != ’\r’)/*去掉回車換行符*/
{
str[j] = c;
++j;
}
}
xx = atoi(str);
if(c == EOF)
break;
}
fclose(fp) ;
return 0 ;
}
void Compute(void)
{
int i;
long count = 0;
for(i = 0; i < MAX; i++)
{
if(xx & 1)
odd++;
else
even++;
count += xx;
}
aver = (double)count/MAX;
for(i = 0; i < MAX; i++)
totfc += (xx - aver)*(xx - aver);
totfc /= MAX;
}
整數(shù)到數(shù)組xx中; 請(qǐng)編制函數(shù)Compute()分別計(jì)算出xx中奇數(shù)的個(gè)
數(shù)odd, 偶數(shù)的個(gè)數(shù)even, 平均值aver以及方差totfc的值, 最后調(diào)
用函數(shù)WriteDat()把結(jié)果輸出到FC1.OUT文件中。
計(jì)算方差的公式如下:
1 N
totfc = ── ∑ (xx - aver)^2
N i=1
原始數(shù)據(jù)文件存放的格式是: 每行存放10個(gè)數(shù), 并用逗號(hào)隔
開。(每個(gè)數(shù)均大于0且小于等于2000)
注意: 部分源程序存放在PROG1.C中。
請(qǐng)勿改動(dòng)主函數(shù)main()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
/*參考答案*/
#include
#include
#include
#define MAX 1000
int xx[MAX], odd = 0, even = 0 ;
double aver = 0.0 , totfc = 0.0 ;
void WriteDat(void) ;
int ReadDat(void)
{
FILE *fp ;
int i,j;
char c,str[20];
if((fp = fopen("FC.IN", "r")) == NULL) return 1 ;
/***********讀入數(shù)據(jù)并存放到數(shù)組xx中*************/
for(i = 0; i < MAX; i++)
{
j = 0;
while((c = (char) fgetc(fp)) != EOF)
{
if(c == ’,’)
{
str[j] = ’\0’;
break;
}
else if(c != ’\n’ && c != ’\r’)/*去掉回車換行符*/
{
str[j] = c;
++j;
}
}
xx = atoi(str);
if(c == EOF)
break;
}
fclose(fp) ;
return 0 ;
}
void Compute(void)
{
int i;
long count = 0;
for(i = 0; i < MAX; i++)
{
if(xx & 1)
odd++;
else
even++;
count += xx;
}
aver = (double)count/MAX;
for(i = 0; i < MAX; i++)
totfc += (xx - aver)*(xx - aver);
totfc /= MAX;
}

