// Program to recursively compute the harmonic mean of an array of numbers. // Program takes max 10 inputs,and stop reading the inputsas soon as a negative number is encounterd // // Author:rahule@cse.iitk.ac.in #include float harmonic_mean(int a[],int,int); //function accpeting the array,lower bound,and total number of lements as arguement main() { int a[10],t,i=0,n; float hm; printf("\nEnter the numbers..\n"); for(i=0;i<10;i++) //scans every input till a negative input is encounterd { scanf("%d",&t); if(t<0) break; a[i]=t; } n=i; //calculates total number of elements hm=harmonic_mean(a,0,n); //initialy lower bound is set as 0 printf("\nThe harmonic mean is %f\n ",hm); } float harmonic_mean(int a[],int i,int n) { if(i==n) //base condition.i=n implies every elements has been processed return(0); if(i==0) return(n/((1/((float)a[i]))+harmonic_mean(a,i+1,n))); //while returning to the "main",divide n by calculated sum 1/a[0] + 1/a[1] ...,and return else { return((1/((float)a[i]))+harmonic_mean(a,i+1,n)); //finds 1/a[1] + 1/a[2] + 1/a[3] ... } }