I have a function that calculates geometric mean (nth root of product of terms, where n is number of terms), and the only thing I'm having trouble with is figuring out how to keep track of how many terms there were. The only thing I could come up with was a global variable, but I know I shouldn't do that. How can I do it without the global variable?
float count;
float geometricMean(float n) {
char again;
float j;
printf("Input a number: ");
scanf("%f", &j);
printf("Another number? (y/n) ");
scanf(" %c", &again);
if (again == 'y') {
j *= geometricMean(n+1);
if (n==1)
return pow(j, (1/count));
else {
return j;
}
}
else {
count = n;
return j;
}
}