0

I'm trying to make a moving/running average for double values from 0-1 but i get an error at line 25:

No matching function for call to 'gl_mean'

Is it not possible to call a void function and let it print something ? Because it can't return any value so i have to do the print in the void function... that part with void gl_mean (double x, double *count, double *mean) { ... } was given by the task by the way so i can't change it.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void gl_mean (double x,double *count, double *mean) {


    double p;

    p=x+(*mean**count)/(*count+1);

    printf("%f",p);

}

int main () {


    double num=0;             //random number
    double i=0;               //Counter
    double sum=0;             //Sum

    srand(time(0));

    for (gl_mean (num,i,sum);i<10;i++) {

        num=rand();
        sum += num;
    }
}
daedsidog
  • 1,732
  • 2
  • 17
  • 36
Dunk
  • 3
  • 1
  • What are the types of the arguments that `gl_mean` takes? What are the types of the arguments you pass to `gl_mean`? – Some programmer dude Dec 04 '18 at 12:50
  • they are both double right? – Dunk Dec 04 '18 at 12:52
  • Only the first argument is declared as a `double`, the others are declared as `double *`, i.e. a *pointer* to a `double`. The types `double` and `double *` are not compatible. – Some programmer dude Dec 04 '18 at 12:55
  • Also, reading between the lines of your question (and some guessing), it seems that the teacher wants to teach you how to emulate *pass by reference* in C (which only have pass by value). That is, in the function you should dereference the pointers to *write* some data to where they point, thereby changing the values of `i` and `sum` inside the `main` function. In short, the function should not print anything, only do a calculation and then you do the printing in the `main` function in your loop. – Some programmer dude Dec 04 '18 at 12:56
  • so i would have to declare my variables i and sum as pointers too? double *i=0; double *sum=0; and then i would be able to call the void gl_mean function? – Dunk Dec 04 '18 at 13:00
  • Either you missed something in class or the book you're reading. The solution is the *address-of operator* `&`. – Some programmer dude Dec 04 '18 at 13:00
  • but then i would have the problem that my void function doesn't return anything therefore i don't know how to get my calculation result back to my main function in order to print it – Dunk Dec 04 '18 at 13:01
  • I gave you a hint before. Do some research about *emulating pass by reference in C*. – Some programmer dude Dec 04 '18 at 13:02

1 Answers1

0

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
    }
}
ryyker
  • 22,849
  • 3
  • 43
  • 87
  • Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/184726/discussion-on-answer-by-ryyker-no-matching-function-for-call-to-gl-mean-error). – Samuel Liew Dec 04 '18 at 22:43