Question & Answer: QUESTION 3 20 (a) 17 Marks] Write a subroutine that receives from the main program a l of real variables and returns t…..

QUESTION 3 20 (a) 17 Marks] Write a subroutine that receives from the main program a l of real variables and returns the geometric and harmonic means of the array. 1-D array of size n Geometric mean = ( χ1.x2 ...-m )1/n Tn Harmonic meanT xn x1 x2 x3 (Do not write the main program. The main program will follow in part (b)).

solve this fortran program clearly

QUESTION 3 20 (a) 17 Marks] Write a subroutine that receives from the main program a l of real variables and returns the geometric and harmonic means of the array. 1-D array of size n Geometric mean = ( χ1.x2 …-m )1/n Tn Harmonic meanT xn x1 x2 x3 (Do not write the main program. The main program will follow in part (b)).

Expert Answer

 

Subroutine of geometric mean:

Geometric mean computed with the formula (x1 * x2 * x3 * x4 ………xn) 1/n with using single dimensional (1-D) array size n real values as

double Mean_Geometric (double * array, int n) /* return result GM is real value and the arguments single                                                                             dimensional array and size n */

{

double GM,ProductArr = 1.0;

int i;

for(i=0; i<n; i++)

{

ProductArr=ProductArr * array[i];

}

GM = pow(ProductArr,(1.0/(double)(n)));/* geometric mean GM value is power of all

Product of 1-D elements    powered by (1/n) */

return GM;

}

Subroutine of Harmonic mean:

Harmonic mean computed with the formula n/(1/x1 + 1/x2 +1/x3 ……..+1/xn) with using single dimensional (1-D) array size n real values as

double Mean_Harmonic (double * array, int n) /* return result HM is real value and the

arguments single dimensional array and size n */

{

double HM = 0;

int i;

for(i=0; i<n; i++)

{

HM+=(1.0)/array[i];

}

HM = (double)(size)*pow(HM,-1.0);

return HM;

}

Still stressed from student homework?
Get quality assistance from academic writers!