This program will read two different txt files with random f
This program will read two different txt files with random float numbers. Please follow the problem and add helpful comments.
Develop a C program that processes a file called fl.txt and G.txt containing a sequence of real-valued numbers. Your program should calculate and display Mean of data from fl.txt Mean of data from f2.txt Standard Deviation of data from fl.txt Standard Deviation of data from fl.txt Correlation between the data contained in the files. Length is the number of data points in each of the files. You must use the following user-defined functions in your code #define SIZE 500 void read_file(FILE *inp, double z[], int length); //read_file - read data from the text file pointed to by int and keep all data points in the array z void calc_mean(int length, double z[], double *mean); //calc_mean - find mean of data points in the array z with number of elements = length void calc_std (int length, double z[], double *std); //calc_std find standard deviation of data points in the array z with number of elements = length If x and y are arrays containing the sequence of number that were read then corr (x, y) = sigma_i = 0^499 (xi m_x) (y_i m_y) / 500 s_y s_x where s_y = Squareroot (1 / 500 sigma _i = 0^499 y_i^2 (1 / 500 sigma _i = 0^499 y_i)^2 s_x = Squareroot (1 / 500 sigma _i = 0^499 x_i^2) (1 / 500 sigma _i = 0^499 x_i)^2 m_y = 1 / 500 sigma _i = 0^499 y_i m_x = 1 / 500 sigma _i = 0^499 x_i Output (code execution): Mean of data in f1.txt is 2.03440 Mean of in f2.txt is 3.01058 Standard deviation of data in f1.txt.txt is 0.97672 Standard deviation of data in f2.txt is 1.00190 The correlation is 0.25396Solution
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define size 500
void calc_std(int n,double z[],double *std)
{
double sd=0;
double average=0;
int i;
for(i=0;i<n;i++)
average=average+z[i];
average=average/n;
for(i=0;i<n;i++)
sd=sd+pow((z[i]-average),2);
sd=sqrt(sd/n);
(*std)=sd;
}
void calc_mean(int length,double z[],double *mean)
{
double sum=0,M;
int i;
for(i=0;i<length;i++)
sum = sum+z[i];
(*mean)=sum/length;
}
void read_file(FILE *inp,double z[],int *length)
{
double tmp=0;
int i=0;
while(!feof(inp))
{
if (fscanf(inp, \"%lf\", &tmp) != 1)
break;
z[i++]=tmp;
}
(*length)=i;
fclose(inp);
}
int main(int argc, char **argv)
{
int len1=0,len2=0;
FILE *inp1,*inp2;
inp1= fopen(\"f1.txt\", \"r\");
inp2= fopen(\"f2.txt\", \"r\");
double z[size],mean1,mean2,std1,std2;
read_file(inp1,z,&len1);
calc_mean(len1,z,&mean1);
calc_std(len1,z,&std1);
printf(\"Mean of data in f1.txt is: %lf\ \",mean1);
read_file(inp2,z,&len2);
calc_mean(len2,z,&mean2);
calc_std(len2,z,&std2);
printf(\"Mean of data in f2.txt is: %lf\ \",mean2);
printf(\"Standard Deviation of data in f1.txt is: %lf\ \",std1);
printf(\"Standard Deviation of data in f2.txt is: %lf\ \",std2);
return 0;
}

