File
/*
===============================================================
Title: Remove the athlete's height data from the text file "High.txt" and calculate the average, variance and standard deviation!
===============================================================
*/
#include <stdio.h>
#include <math.h>
#define HH printf ("\n===================================\n");
void Main ()
{
FILE *FP;
float a[520],x,avr,fc,bzc,t,sum=0.0;
int i,k=0;
Fp=fopen ("D:\\high.txt", "R");
if (fp==null)
printf ("File open failed!\n");
Else
{
printf ("Height data: \ n");
FSCANF (FP, "%f", &x);
while (x!=0)
{
A[k++]=x;
printf ("%.0f", X);
Sum+=x;
FSCANF (FP, "%f", &x);
}
avr=sum/k;
sum=0;
for (i=0;i<k;i++)
sum+= (A[I]-AVR) * (A[I]-AVR);
fc=sum/(k-1);
BZC=SQRT (FC); HH
printf ("Number of Students:%d\n", K);
printf ("Height mean:%.2f\n", AVR);
printf ("Height variance:%.2f\n", FC);
printf ("Height standard deviation:%.2f\n", BZC);
}
hh
Fclose (FP);
}
/*
========================================================================
Evaluation:
Read with fscanf (FP, "%f", &x);
Variance = (X-mean) Number of ^2/groups (number of groups = number of elements-1);
========================================================================
*/
Copyright Notice: Bo Main article can be reproduced non-commercial, but please be sure to indicate the source, because the level is limited, inevitably error, in this disclaimer.
C Language File operation 07--read file data and calculate mean variance standard deviation