Please prepare function ReadDat () to achieve read 1000 decimal integer from IN.DAT file into an array in xx:
Please prepare the Compute function () is calculated respectively in an even number of xx even, the average value AVE1 odd, even-numbered and the average value ave2 of variance totfc, after the function call WriteDat () OUT.DAT outputs the result to a file.
Calculating the variance of the following formula:
N_____2
totfc=1/N∑(xx[i]-ave2)
i = 1 provided N is an even number of, xx [i] is an even number, ave2 an average value even.
The format of the original data files: each line number storage 10, and separated by commas. (Each number greater than 0 and less than or equal to 2000).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 1000
int xx[MAX],odd=0,even=0;
double ave1=0.0,ave2=0.0,totfc=0.0;
void WriteDat(void);
int ReadDat(void) {int i; FILE *fp;
if((fp=fopen(“IN.DAT”,“r”))==NULL) return 1;
/ Compiled function ReadDat () of section *** /
for(i=0;i<MAX;i++)
{ fscanf(fp,"%d,",&xx[i]);
if((i+1)%10==0)
fscanf(fp,"\n"); }
/*******************************************/
fclose(fp);
return 0;
}
void Compute(void)
{ int i,yy[MAX];
for(i=0;i<MAX;i++)
yy[i]=0;
for(i=0;
i<MAX;i++)
if(xx[i]%2==0) { yy[even++]=xx[i]; ave2+=xx[i];}
else { odd++; ave1+=xx[i];}
if(odd==0) ave1=0;
else ave1/=odd;
if(even==0) ave2=0;
else ave2/=even;
for(i=0;i<even;i++)
totfc+=(yy[i]-ave2)*(yy[i]-ave2)/even;
}
void main()
{
int i;
for(i=0;i<MAX;i++)xx[i]=0;
if(ReadDat()){
printf ( "IN.DAT data file can not be opened \ 007 \ n!");
return;
}
Compute();
printf(“OVEN=%d\nAVE1=%f\nAVER2=%f\nTOTFC=%f\n”,even,ave1,ave2,totfc);
WriteDat();
}
void WriteDat(void)
{
FILE *fp;
int i;
fp=fopen(“OUT.DAT”,“w”);
fprintf(fp,"%d\n%f\n%f\n%f\n",even,ave1,ave2,totfc);
fclose(fp);
}