First, the minimum correct prototype for main
is either:
int main(void){/*...*/}
or
int main(int argc, char *argv[]) { /* ... */ }
Although The C99 standard does allow for other implementation-defined signatures, you should use them only if you've read the manual for your compiler and it says you can.
(5.1.2.2.1) It shall be defined with a return type of int and with no parameters ... or with two parameters ... or in some other implementation-defined manner
Additionally, here is a similar conversation citing C11.
No matching function ...
Yes, it is possible to print values from a void
function. But it is also possible, using emulated pass by reference to access and print the value from the calling function. The address of operator ( &
) is used in the calling function when emulating pass by reference in C.
The following is your basic algorithm with changes as described: (read in-line comments for explanation.)
void gl_mean (double x,double *count, double *mean) {
static double p;
static int timesIn = 0; //increment times in to update *count
timesIn++;
//p = x+(*mean * *count)/(*count+1);
p += x;
*count = (double)timesIn;
*mean = p/(*count);//*count must be greater than 0
//printf("%f\n",*mean);
}
int main (void) {
double num=0.0; //random number
double dI=1.0; //Counter - avoid div-by-zero,
double sum=0.0; //Sum
int i=0;
srand(time(0));
//for (gl_mean (num,i,sum);i<10;i++) {, this will not progress through the loops)
// because the initializer part of the for loop
// only gets called once
for (i=0;i<10;i++)//gl_mean (num,i,sum);i<10;i++) {
{
num=rand() %10 + 1; // "%10 + 1" optional to limit range from 1 to 10
gl_mean (num,&dI,&sum);//note the use of address operator to emulate
//passing a value by reference.
//values for dI and sum are updated in function
printf("%d) %f: %f\n",(int)dI, num, sum);
sum += num;
// i = (int)dI; //update loop index with updated value
}
}