PLEASE WRITE IT IN C PROGRAM, NOT C++ Modify the program written in question 1 t
ID: 3938270 • Letter: P
Question
PLEASE WRITE IT IN C PROGRAM, NOT C++
Modify the program written in question 1 to use a single function to compute all three averages.
In more detail, write the program, permeability1function.c, whose main carries out the
following steps:
• open the two files samplesin.txt and samples1out.txt.
• read the values from samplesin.txt into the array x and determine the value of n (number
of values in the file).
• call the function averages which computes the arithmetic, geometric and harmonic
averges.
• write the three averages to the file samples1out.txt.
the Code I wrote for the question 1 is :
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
//static Memory Allocation
#define MAXN 100
//function define
float func_arithAve(float x[], int i);
float func_geomeAve(float x[], int i);
float func_harmoAve(float x[], int i);
int main(void){
//Declare variables
float x[MAXN], arithAve, geomeAve, harmoAve;
int i;
FILE * fin = fopen("samplesin.txt","r");
FILE * fp = fopen("samplesout.txt","w");
i = 0;
while (fscanf(fin, "%f", &x[i])!=EOF){
i++; //this counter tracks the number of values in the array
}
//3 functions now
arithAve = func_arithAve(x, i);
geomeAve = func_geomeAve(x, i);
harmoAve = func_harmoAve(x, i);
//output
fprintf(fp, "the arithmetic average = %f ", arithAve);
fprintf(fp,"the geometric average = %f ", geomeAve);
fprintf(fp,"the harmonic average = %f ", harmoAve);
fclose(fin);
fclose(fp);
system("notepad samplesout.txt");
return 0;
}
//define functions
float func_arithAve(float x[], int i){
int a;// use a local counter that works its way up to q-1
float b=0.0;
// Note use FOR loops for the sums
for (a=0; a<=(i-1);a++){
// equations are on the assignment sheet
b = b + x[a];
}
b = b/((float)i);
// return the average values
return b;
}
float func_geomeAve(float x[], int i){
int a;
float c=0.0;
for(a=0; a<=(i-1); a++){
c = c + log(x[a]);
}
c = c/((float)i);
c = exp(c) ;
return c;
}
float func_harmoAve(float x[], int i){
int a;
float d=0.0;
for(a=0; a<=(i-1);a++){
d = d + (1/x[a]);
}
d = ((float)i/d);
return d;
}
Explanation / Answer
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
//static Memory Allocation
#define MAXN 100
//function define
float func_arithAve(float x[], int i);
float func_geomeAve(float x[], int i);
float func_harmoAve(float x[], int i);
int main(void){
//Declare variables
float x[MAXN], arithAve, geomeAve, harmoAve;
int i;
FILE * fin = fopen("samplesin.txt","r");
FILE * fp = fopen("samplesout.txt","w");
i = 0;
while (fscanf(fin, "%f", &x[i])!=EOF){
i++; //this counter tracks the number of values in the array
}
//3 functions now
arithAve = func_arithAve(x, i);
geomeAve = func_geomeAve(x, i);
harmoAve = func_harmoAve(x, i);
//output
fprintf(fp, "the arithmetic average = %f ", arithAve);
fprintf(fp,"the geometric average = %f ", geomeAve);
fprintf(fp,"the harmonic average = %f ", harmoAve);
fclose(fin);
fclose(fp);
system("notepad samplesout.txt");
return 0;
}
//define functions
float func_arithAve(float x[], int i){
int a;// use a local counter that works its way up to q-1
float b=0.0;
// Note use FOR loops for the sums
for (a=0; a<=(i-1);a++){
// equations are on the assignment sheet
b = b + x[a];
}
b = b/((float)i);
// return the average values
return b;
}
float func_geomeAve(float x[], int i){
int a;
float c=0.0;
for(a=0; a<=(i-1); a++){
c = c + log(x[a]);
}
c = c/((float)i);
c = exp(c) ;
return c;
}
float func_harmoAve(float x[], int i){
int a;
float d=0.0;
for(a=0; a<=(i-1);a++){
d = d + (1/x[a]);
}
d = ((float)i/d);
return d;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.