Problem 2 (35 points Develop a C program that processes a file called \"fl.txt\"
ID: 3816030 • Letter: P
Question
Problem 2 (35 points 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 fl.ixt 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 inp and keep all data points in the array z void calc mean (int length, double zti, 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 Ifxandyare arrays containing the sequence of number that were read then 499 corr(x,y) 500 s where 499 499 2 499 500X my 500 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<math.h>
#include<stdlib.h>
#define SIZE 500
//To read file f1.txt and f2.txt
void read_file(FILE *inp, double z[], int *length)
{
*length = 0;
int i = 0;
//Checks if file pointer contains null then show error and exit
if (inp == NULL)
{
printf("Error! opening file");
// Program exits if file pointer returns NULL.
exit(1);
}//End of if
//Loops till end of file
while(!feof(inp))
{
//Reads data from file and stores in array
fscanf(inp, "%lf", &z[i]);
//Increase the counter
i++;
}//End of while
//Assigns the counter to length
*length = i;
// Close input file
close(inp);
}//End of function
//Calculates the mean
void cal_mean(int length, double z[], double *mean)
{
int i;
double sum = 0;
//Compute the sum of all elements
for (i = 0; i < length; i++)
{
sum = sum + z[i];
}
//Calculates the mean
*mean = sum / (double)length;
}//End of function
//Calculates standard deviation
void calc_std(int length, double z[], double *std)
{
int i;
double sum = 0, variance, mean;
//Compute the sum of all elements
for (i = 0; i < length; i++)
{
sum = sum + z[i];
}//End of for
//Calculates mean
mean = sum / (double)length;
sum = 0;
//Compute variance and standard deviation
for (i = 0; i < length; i++)
{
sum = sum + pow((z[i] - mean), 2);
}//End of for
//Calculates the variance
variance = sum / (double)length;
//Calculates standard deviation
*std = sqrt(variance);
}//End of function
//Main function
void main()
{
//Declares a double array
double z[SIZE];
int i, len;
double mean1, mean2, average, cv, std1, std2, sum = 0, sum1 = 0;
//Opens f1.txt in read mode
FILE *f1 = fopen("f1.txt", "r");
//Opens f2.txt in read mode
FILE *f2 = fopen("f2.txt", "r");
//Calls the read file function
read_file(f1, z, &len);
//Calls the mean and standard deviation function
cal_mean(len, z, &mean1);
calc_std(len, z, &std1);
// Close input file
close(f1);
read_file(f2, z, &len);
cal_mean(len, z, &mean2);
calc_std(len, z, &std2);
// Close input file
close(f2);
//Calculates the correlation
cv = ((std1 * std2) / (mean1 * mean2));
//Displays the information
printf(" Mean of data in f1.txt is = %.2lf", mean1);
printf(" Standard Deviation of data in f1.txt is = %.2lf", std1);
printf(" Mean of data in f2.txt is = %.2lf", mean2);
printf(" Standard Deviation of data in f2.txt is = %.2lf", std2);
printf(" The correlation is = %.2lf", cv);
}//End of main
Output:
Mean of data in f1.txt is = 42.00
Standard Deviation of data in f1.txt is = 30.59
Mean of data in f2.txt is = 38.60
Standard Deviation of data in f2.txt is = 27.69
The correlation is = 0.52
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.