Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use simple C language No strings!!! And please do not use cout and cin in

ID: 3866938 • Letter: P

Question


Please use simple C language No strings!!! And please do not use cout and cin instead use printf and scanf. 3. The How of toocic waste through a permeable media (e-g. sol) can be modeled using differential equations (e.g. Darcy's Law). However, the diferential equations are hard to solve when the permeability changes from point to point (as in a real soil). One solution to the problem is to represent the permeability as sone sort of effective average. , x[n-1] stored in the Suppose that we have permeability readings xO], x[1] array x. Three possible averages are coanmonly used, depending on the nature of the toxic low problen; arithmetic average =- geometric average = exp ( log(112) harmonic average Write a program, permeability.c, which computes the three averages for a set of values stored in the file samples.txt. You can assume that theumber of values in the input file is kss than 100 Your ain must use the following steps: les.txt and samplesout.txt. . open the two files sanpl . read the values from samples.txt into an array x and determine the value of n (number of values in the fle) ecall three functions arithnetic, geonetric and harzonic to compute the corresponding averages write the three averages to t he file samplesout.txt arithmetic average should be 20.189125, your harmonic average To check your program, your a should be 15.677883 and your geometric average should be between these two values. / File: permeability.c A set of perneabilities is read fron a file into an array Three functions are called to compute the arithnetic, geonetric and harmonic averags Assignnent:5 Programser Date: Question: 3 DalID: Course: ENGM 1081 includetdio.h> include ath.h> #define MAXX 100 int zain (void)

Explanation / Answer

c code:

#include <stdio.h>
#include <math.h>

float arith(float* arr,int n)
{
float sum = 0;
for (int i = 0; i < n; ++i)
{
sum = sum + arr[i];
}
return sum/n;
}

float geom(float* arr,int n)
{
float sum = 0;
for (int i = 0; i < n; ++i)
{
sum = sum + log(arr[i]);
}
return sum/n;
}


float harm(float* arr,int n)
{
float sum = 0;
for (int i = 0; i < n; ++i)
{
sum = sum + (1.0)/arr[i];
}
return n/sum;
}


int main ( void )
{
static const char filename[] = "samples.txt";
FILE *file = fopen ( filename, "r" );
float arith_avr = 0.0;
float geom_avr = 0.0;
float harm_avr = 0.0;
float array[100];
int n = 0;
if ( file != NULL )
{

char line [128];
while ( fgets ( line, sizeof line, file ) != NULL )
{
float i;
i = atof(line);
array[n] = i;
n = n+1;
}
char sentence[1000];
FILE *fptr;

fptr = fopen("sampleout.txt", "w");
if(fptr == NULL)
{
printf("Error, can not open output file!");
}
arith_avr = arith(array,n);
geom_avr = geom(array,n);
harm_avr = harm(array,n);

fprintf(fptr,"Arithmatic Average = %f ", arith_avr);
fprintf(fptr,"Geometric Average = %f ", geom_avr);
fprintf(fptr,"Harmonic Average = %f ", harm_avr);
fclose(fptr);
fclose ( file );
}
else
{
perror ( filename );
}

return 0;
}

samples.txt

1
2
3
4

sampleout.txt

Arithmatic Average = 2.500000
Geometric Average = 0.794513
Harmonic Average = 1.920000