Please help me...haven\'t grasped arrays yet... Develop a C program that process
ID: 3825207 • Letter: P
Question
Please help me...haven't grasped arrays yet...
Develop a C program that processes a file called "fl.txt" and "f2.txt" containing a sequence of real-valued numbers. Your program should calculate and display -Mean of data from f1.txt -Mean of data from f1.txt -Standard Deviation of data from f1.txt -Standard Deviation of data from f1.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 inp 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^499_i = 0 (x _i - m _x) (y _i - m __y) where s _y = squareroot (1/500 sigma^499_i = 0 y^2_i) - (1/500 sigma^499_i = 0 y _i)^2 s _x = squareroot (1/500 sigma^499 _i = 0 x^2_i) - (1/500 sigma^499 _i = 0 x _i) m _y = 1/500 sigma^499 _i = 0 y _i m _x = 1/500 sigma^499 _i = 0 x _i output (code execution): Mean of data in f1.txt is 2.03440 Mean of data in f2.txt is 3.01058 Standard deviation of data in f1.txt is 0.97672 Standard deviation of data in f2.txt is 1.00190 The correlation is 0.25396Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define size 500
void main(){
FILE *infile;
infile = fopen("f1.txt","r");
double z1[size],z2[size],mean1,mean2,std1,std2,corr,tmp;
int length1,length2;
read_file (infile , z1[], length1);
calc_mean (length1 , z1[], &mean1);
calc_std (length1 , z1[], &std1);
fclose(infile);
infile = fopen("f2.txt","r");
read_file (infile , z2[], length2);
calc_mean (length2 , z2[], &mean2);
calc_std (length2 , z2[], &std2);
fclose(infile);
for (int i =0;i <length1; i++)
{
tmp=tmp+((z1[i]-mean1)*(z2[i]-mean2));
corr=tmp/length1*std1*std2;
}
printf("Mean of data in f1.txt is %f ",mean1);
printf("Mean of data in f2.txt is %f ",mean2);
printf("Standard of data in f1.txt is %f ",std1);
printf("Standard of data in f2.txt is %f ",std2);
printf("The correlation is %f ",corr);
}
void read_file (FILE *inp, double z[] ,int length)
{
int value;
int l;
length=0;
while(!feof(inp))
{
fscanf(inp, "%f", &tmp);
z[length]=tmp;
length++;
}
return;
}
void calc_mean (int length ,double z[], double *mean)
{
double sum=0;
for (int i=0;i<length;i++)
{
sum=sum+z[i];
}
mean=sum/length;
}
void calc_std (int length ,double z[], double *std)
{
double sum1=0,sum2=0,tmp;
for (int i=0;i<length;i++)
{
sum1=sum1+z[i];
sum2=sum2+(z[i]*z[i]);
}
tmp=sum2-(sum1*sum1);
std=sqrt(tmp);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.