-2

I have a program that is looking for a total numbers and what is the middle number in a linked list. The question that I have is why it doesn't print out the values?

Here is the code:

int count(list values){
    if(values == NULL)
        return 0;
    else
        return 1 + count(values->next);
}

void middle(struct node *head){
    int count = 0;
    struct node *mid = head;

    while (head != NULL){
        if(count & 1)
            mid = mid->next;

        count++;
        head = head->next;
    }
}

void traverse(list values){
     if(values->next)
     printf("\n# of the values: %.1f% \nMiddle: %.1f%\n", count, middle);
}

int main(int argc, char *argv[]){
    FILE *input = stdin;

    list values = readNumbers(input);
    traverse(values);
    return 0;
}
edge
  • 11
  • 5
  • Show us the whole code, please. – cadaniluk Sep 06 '15 at 19:00
  • What is the expected and actual output? – Jonathan Wood Sep 06 '15 at 19:00
  • list readNumbers (FILE *file){ list values = NULL, first = NULL; int c; while ((c = getc(file)) != EOF) { int i = 0; list new = (list) malloc(sizeof(struct node)); if (values == NULL) values = new; else { values->next = new; values = new; } values = new; if (first == NULL) first = values; do { values->value[i++] = c; } while ((c = getc(file)) != '\n' && c != EOF); values->value[i] = '\0'; } return first; } – edge Sep 06 '15 at 19:02
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a **[mcve]**. – too honest for this site Sep 06 '15 at 19:11

2 Answers2

0
printf("\n# of the values: %.1f% \nMiddle: %.1f%\n", count, middle);

count should receive a parameter (list ). The compiler will treat count and middle, without parameters as function pointers.

Also note that middle is a void function so what exactly do you want it to print?

Alex Lop.
  • 6,810
  • 1
  • 26
  • 45
0

It's hard to know where to start here. I can't really tell what you are trying to do.

But let's look at this line:

printf("\n# of the values: %.1f% \nMiddle: %.1f%\n", count, middle);

count and middle are functions, but you are not calling those functions here. You are simply passing the address of those functions to printf(), which has no knowledge that these are functions. You need to include parentheses after the function names (count(args) and middle(args) in order to call those functions.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • Does it work if I pass count(values) or middle(values) – edge Sep 06 '15 at 19:10
  • You haven't shown enough of your code to answer that. But it's your code. You're the one who should be testing it. Did you try it? Did you get a compile error? What is the error? – Jonathan Wood Sep 06 '15 at 19:16
  • 1
    I got it for the count.. I'm working on middle now.. I guess that the issue that I'm having now is that middle is the void.. I'll get back if I need more help.. Thanks – edge Sep 06 '15 at 19:20